Live data from Hacker News

Unpythonic Python

skien.cc

111–120 of 156 posts

Re: Unpythonic Python

#111
post #60

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.

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

#112

Earlier 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.

The description is underspecified, no surprise there, but that's different from claiming it's incorrect. As far as its purpose as an interview question goes noticing this interpretation would probably be seen as a good sign.

Re: Unpythonic Python

#113
post #60

Earlier 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.

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)

Re: Unpythonic Python

#114
post #108

https://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) ] )

And also C based on the same idea as my Python (but this has to ? for 0s):

    int i;
    char* a[] = { 0, "Buzz", "Fizz", "FizzBuzz" };
    char* f[] = { "%s\n", "%d\n" };
    for ( i = 1; i 

Re: Unpythonic Python

#115
C#

Enumerable.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
Doesn'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

#117
post #18

Earlier 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).

I use this function to force evaluation of elements in a generator:

    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

#118
post #116

Doesn'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')

Literals are much more readable in any language I've used (or at least can think of right now).

Re: Unpythonic Python

#119
Nobody ever seems to go for the general solution:

words = ( (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

#120

Earlier 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)

By that logic, no one should write anything in idiomatic French because anyone who isn't fluent in French wouldn't be able to read it.
Post reply on HN