One of the solutions in the comments I found quite pythonic and concise. Somehow people have it in their heads that "Pythonic" means long-winded. And yes, you have to read the code and think for a second to understand it, but that's no crime. [(not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or x for x in range(1, 101)]
Things taking longer to read and understand is perhaps the central crime of unmaintainable code, no matter how concise otherwise.
Unpythonic Python
111–120 of 156 posts
Re: Unpythonic Python
#112Earlier quoted context omitted.
The description is declarative, there is nothing implied about "moving on".
The particular form of the description at issue can be interpreted (arguably, is most naturally interpreted ) to direct a different outcome than is usually expected from FizzBuzz, to wit, it directs that on numbers divisible by 15 "Fizz", "Buzz", and "FizzBuzz" all should be printed, rather than just the last.
Re: Unpythonic Python
#113Earlier quoted context omitted.
Things taking longer to read and understand is perhaps the central crime of unmaintainable code, no matter how concise otherwise.
I think this code is easier to read than the more verbose 12-line version given in the article. It takes longer to read per line, but less time total.
Re: Unpythonic Python
#114https://gist.github.com/seanjensengrey/d053e7fa709e0699e291 If free version. t = {} t[0,0] = lambda x: x t[1,0] = lambda x: "Fizz" t[0,1] = lambda x: "Buzz" t[1,1] = lambda x: "FizzBuzz" def tests(x): return (x % 3 == 0, x % 5 == 0) for x in range(1,101): print t[tests(x)](x)
Good idea. Enhancing: If, lambda, logical operators except == and string concatenation free version (Python 3) for x in range( 1, 101 ): print( [ x,'Buzz','Fizz','FizzBuzz' ][ (x%3==0)*2 + (x%5==0) ] )
int i;
char* a[] = { 0, "Buzz", "Fizz", "FizzBuzz" };
char* f[] = { "%s\n", "%d\n" };
for ( i = 1; i Re: Unpythonic Python
#115Enumerable.Range(1, 100).ToList().ForEach(a =>{if (a%5 == 0 && a%3 == 0){Console.WriteLine("FizzBuzz");}else if (a%3 == 0){Console.WriteLine("Fizz");}else if (a%5 == 0){Console.WriteLine("Buzz");}else{Console.WriteLine(a);}});
Re: Unpythonic Python
#116 x = dict()
x['a'] = 'string'
x['b'] = list()
x['b'].append('foo')
x['b'].append('bar')Re: Unpythonic Python
#117Earlier quoted context omitted.
A minor quibble, but in Python 3 you can print in a lambda because print is no longer a keyword.
As a non-Python 3 user, I just opened up the python 3 REPL to try something I always find myself wanting to do in tiny little python scripts: map(print, some_list) Unfortunately, it returned a map object, which I guess is also new in Python 3 (I assume it's a lazy application device).
def do(gen):
for x in gen:
pass
It evaluates all elements of a generator with storing them in memory. If you don't mind generating some garbage, you can just use the list function instead.Re: Unpythonic Python
#118Doesn't even touch on my personal pet peeve, people who don't use list and dict literals. I assume they're former Java programmers who got ahold of enough python knowledge to be dangerous. e.g: x = dict() x['a'] = 'string' x['b'] = list() x['b'].append('foo') x['b'].append('bar')
Re: Unpythonic Python
#119words = ( (3, 'Fizz'), (5, 'Buzz') )
def fizzbuzz(num): for value, word in words: if i % value == 0: yield word
for i in range(1, 101): print ''.join(fizzbuzz(i)) or i
Re: Unpythonic Python
#120Earlier quoted context omitted.
I think this code is easier to read than the more verbose 12-line version given in the article. It takes longer to read per line, but less time total.
It's not just more complexity per line, it's also a higher level of complexity, using language-specific features that people who aren't fluent in python wouldn't be familiar with (multiplying a string by a boolean)