Live data from Hacker News

Unpythonic Python

skien.cc

91–100 of 156 posts

Re: Unpythonic Python

#91

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

I like the solution, but I usually keep "The Zen of Python" in mind when writing 'Pythonic' code.

Re: Unpythonic Python

#93
This problem is like Project Euler #1. You don't strictly need to divide. I prefer to skip along the array and put in the new values.

  n,f,b= 100,3,5
  a = [str(i) for i in range(1,n+1)]
  for i in range(n/f):
      a[(i+1)*(f)-1] = 'Fizz'
  for i in range(n/b):
      a[(i+1)*(b)-1] = 'Buzz'
  for i in range(n/(f*b)):
      a[(i+1)*(f*b)-1] = 'FizzBuzz'

Re: Unpythonic Python

#94

The superfluous semicolons on line endings in the C example gave me a chuckle.

He forgot one on the "i += 1" (which would be i++ in a for loop but Python doesn't have that operator). But that whole line is weird, because I think the C example should use a "for" loop, not increment the main counter at the top of a while loop, even if it has to use "for i in range(1, 101)" like in pythonic python. A C programmer like me (who loves python too) would immediately think "for (i = 1; i <= 100; i++)" f…

This is what I originally had but I felt that the Python `while` was actually a bit more true to the way a C for loop is constructed (and `range` seems a very not C thing to do.) It's not perfect!

Re: Unpythonic Python

#95
post #49

The value in FizzBuzz is the iteration process. What's the first step? Well, you probably make a stream of numbers. And then a set of if blocks to test and return strings. Why not keep going? What happens if you want more fizz buzz strings? Does the giant if-or statement seem a little unwieldy? Okay, pull the rules out and see if that's better. Is it easier to test now that it's a function and not a little stateful o…

YAGNI.

Re: Unpythonic Python

#97
post #96

JS version for funsies. I didn't like the idea of specifically checking for simultaneous mod3 and mod5, so I made this. for (i=1; i "); }

Ugly version: for(i=1;i<101;i++)console.log((i%3?"":"fizz")+(i%5?"":"buzz")||i)

Re: Unpythonic Python

#98
Two more variants. The first is precomputed without code-based initialization:

  fizzbuzz = ['FizzBuzz', None, None, 'Fizz', None, 'Buzz', 'Fizz', None,
              None, 'Fizz', 'Buzz', None, 'Fizz', None, None]
  for x in xrange(1, 101):
    print fizzbuzz[x % 15] or x
The second is unique in that there are no conditionals at all. (Not efficient, especially for large numbers)

  fizzbuzz = ['FizzBuzz', 1, 1, 'Fizz    ', 1, 'Buzz    ', 'Fizz    ',
              1, 1, 'Fizz    ', 'Buzz    ', 1, 'Fizz    ', 1, 1]
  for x in xrange(1, 101):
    print str(fizzbuzz[x % 15] * x)[:8]

Re: Unpythonic Python

#99
post #61

Earlier quoted context omitted.

>>> let = lambda File " ", line 1 let = lambda ^ SyntaxError: invalid syntax Am I misunderstanding you?

>>> let = lambda x: 2*x >>> print let(4) 8 Edit: Awesome, downvoted for helping.

That's not what they mean. The usage they are discussing is more like this, I believe. In a functional context, instead of doing:

    print expensive_computation(), expensive_computation()
You can do:

    (lambda x: print x, x)(expensive_computation())
Which would be equivalent to, with an imaginary let syntax:

    (let x be expensive_computation(): print x, x)

Re: Unpythonic Python

#100
post #40

When you write too much Haskell, your Python code starts to look like this: print('\n'.join( 'FizzBuzz' if x%5==0 and x%3==0 else 'Fizz' if x%3==0 else 'Buzz' if x%5==0 else str(x) for x in range(1, 101))) I would really like to have a "let" expression in Python to avoid having to write a new function with a def statement when you could get away with a simple lambda or generator expression.

In the generator case you can define a poor man's let like this:

    def let(x):
        yield x
And use it by "iterating":

    ...
    for x in range(1, 101)
    for divisible_by_5 in let(x % 5 == 0)
Still not as nice as having actual syntax for it.
Post reply on HN