5 ms·
Hold up, "idiomatic Python" and no generator comprehensions?! def fizzbuzz(i): outputs = [(3, 'Fizz'), (5, 'Buzz'), (7, 'Bar')] return ''.join(
by blixt 9y ago
Hold up, "idiomatic Python" and no generator comprehensions?!
def fizzbuzz(i):
outputs = [(3, 'Fizz'), (5, 'Buzz'), (7, 'Bar')]
return ''.join(word for x, word in outputs if i % x == 0) or str(i)
- KirinDave 9y agoIt was over 5 years ago and these are more accepted now.
- blixt 9y agoSorry it wasn't meant very seriously, just me expressing a caricature of idiomatic Python :) So disclaimer: good old `for` loops are often a better choice unless you're doing a simple modification or filtering of another sequence.
- svara 9y agoReally? I actually liked that solution, I think it's both readable and clever. The rule priority is defined by the order of your list of tuples ('fizzbuzz' vs. 'buzzfizz'), so easy to change. That's superior to the solution above with sorted(outputs.keys()).
- blixt 9y agoI would say my solution is borderline. It requires maybe one or two extra reads to grok in its entirety due to multiple conditionals, one of which is in a loop and the other outside. Here's a more readable version for someone looking at it the first time, or who is not too used to Python: OUTPUTS = [(3, 'Fizz'), (5, 'Buzz'), (7, 'Bar')] def fizzbuzz(i): result = '' for x, word in OUTPUTS: if i % x == 0: result += word return result or str(i) Even the final `or` could be broken out to an `if not`, but in this case it's so simple I prefer putting it together, much like a coalescing operator. And then for shits and giggles we can say screw it and put it all in one line: print '\n'.join(''.join(word for x, word in [(3, 'Fizz'), (5, 'Buzz'), (7, 'Bar')] if i % x == 0) or str(i) for i in xrange(1, 101))
- paultopia 9y agoHeh, I actually like blixt's solution better than mine too. :-)
- chicago_wade 9y agoHere's mine. Almost exactly the same as yours but written a tiny bit different. Also I wrote the complete program. #!/usr/bin/env python3 # Word of factor wof = { 3: 'Fizz', 5: 'Buzz', 7: 'Bar' } def fizz (i): out = ''.join([wof[k] for k in sorted(wof.keys()) if not(i % k)]) return out or str(i) for i in range(1, 101): print(fizz(i)) Some notes on my version: 1. I used dicts like the parent to yours. Tuples are almost just as fine and you do get a little bit nicer code in the part that you say word for x, word in outputs where our say wof[k] for k in sorted(wof.keys()) However, when I wrote mine I imagined myself sitting in front of an interactive interpreter session. Now let's say that we've each typed in the code we have here and then the interviewer says "now make it say Bazinga when the number is divisible by 19, and keep the order like with FizzBuzzBar". Since my wof is global, I can add to it directly wof[19] = 'Bazinga' But let's pretend that yours was global as well outputs.append((19, 'Bazinga')) So aside from the global / not global we are on equal ground thus far. But then the interviewer says "now make it say Fnord when the number is divisible by 13, and keep the order still". Then once again I can simply wof[13] = 'Fnord' Whereas if you did outputs.append((13, 'Fnord')) then now you are out of order. So you have to do some extra steps to ensure it's in order. Either you have to redeclare the whole thing (imagine there were one million pairs of numbers and strings!), or you have to sort it, or you have to find where you are going to insert and split it in two and join it around. Not saying that mine is in any way better, just felt like sharing how I thought about this :) 2. (Wow note #1 was looong, sorry 'bout that.) I prefer splitting up my code like out = ''.join([wof[k] for k in sorted(wof.keys()) if not(i % k)]) return out or str(i) as opposed to your all-at-once return ''.join(word for x, word in outputs if i % x == 0) or str(i) In my opinion splitting it exactly the way I did is more readable. Again, not a critique against what you wrote. Esp. since you said it yourself that your code was a bit tongue-in-cheek. 3. Even when split like I did there, I actually don't think my out = ''.join([wof[k] for k in sorted(wof.keys()) if not(i % k)]) is all that nice either. One thing, which others have pointed out about this often is that too much is happening in one line but also there is another issue. We all make typos or logic errors every now and then. In my exprience it's much more difficult to debug list comprehensions compared to... well whatever we call that which is not list comprehensions. Both with regards to error messages and with regards to following what's going on. I maintain that this is true even if you have a habit of using a lot of list comprehensions.