Getting started with python
James Stroud
jstroud at mbi.ucla.edu
Sun Apr 15 23:35:58 EDT 2007
Paul Rubin wrote:
> Steve Holden <steve at holdenweb.com> writes:
>> You'd be worth more if you'd used elif and omitted the continue
>> statements, but for a first solution it's acceptable.
>>
>> For better readability I'd have used
>> if i % 5 == 0
>
> I think I'd be more concerned about getting rid of the i%15 test.
> What if a few more words get added?
>
> def fizzbuzz(n):
> words = ((3, 'Fizz'), (5, 'Buzz'), (7, 'Jazz'), (11, 'Pizzazz'))
> r = ''.join(b for a,b in words if n%a == 0)
> return r or str(n)
>
> for i in xrange(1,101):
> print fizzbuzz(i)
I think that this is somewhat of an over-interpretation of the
specification.
1. This doesn't act according to the specification if you add, for
example, (2, 'Zonk'). Now 30 gives 'ZonkFizzBuzz' and not 'FizzBuzz'
according to the specification.
2. What if additional words don't necessarily follow the pattern you
infer? Nothing in the specification disallows adding (30, 'Whammo').
So, I would keep my original explicit testing:
def fizzbuzz(n):
words = ((30, 'Whammo'), (15, 'FizzBuzz'),
(5, 'Fizz'), (3, 'Buzz'), (2, 'Zonk'))
for k,v in words:
if not k % n:
return v
return n
for i in xrange(1,101):
print fizzbuzz(i)
James
More information about the Python-list
mailing list