Live data from Hacker News

Unpythonic Python

skien.cc

41–50 of 156 posts

Re: Unpythonic Python

#41
post #5
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

In the comments, someone gave this nice solution: for x in range(100): print x%3/2*'Fizz'+x%5/4*'Buzz' or x+1

That's more like it! First time I heard about Fizzbuzz, I solved it in PHP. I stay amazed at the 100 lines solutions.

for ($i = 1; $i

    $str = str_repeat('Fizz', !($i%3)&1) . str_repeat('Buzz', !($i%5)&1);

    echo $str ?  "$str\n" : "$i\n";

}

Re: Unpythonic Python

#42

Earlier quoted context omitted.

Actually, most Python I write (influence from reading experienced programmers) tends to the last one, although it's semi-jokingly: def fizzbuzz(n): return 'FizzBuzz' if n % 3 == 0 and n % 5 == 0 else None def fizz(n): return 'Fizz' if n % 3 == 0 else None def buzz(n): return 'Buzz' if n % 5 == 0 else None def fizz_andor_maybenot_buzz(n): print fizzbuzz(n) or fizz(n) or buzz(n) or str(n) map(fizz_andor_maybenot_buzz,…

I use defaultdict all the time. Very often when describing graphs. g = defaultdict(dict) Allows you to do g[node1][node2] = edge_weight without checking if node1 exists, and if not, saying g[node1] = {} Also a neat trick (of dubious use) is: def auto_tree(): return defaultdict(auto_tree) Gives you infinitely nested defaultdicts.

defaultdict is really multipurpose, quick trees is only one of the neat tricks. I like the following definition :)

    >>> Tree = lambda: defaultdict(Tree)

Re: Unpythonic Python

#44
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.

"let = lambda" would be the same thing, wouldn't it?

Re: Unpythonic Python

#45
post #24
post #14

Earlier quoted context omitted.

I generally don't particularly like giving punctuation meaning that, well, isn't punctuating. The bottleneck when writing code is almost never keypresses. I think CoffeeScript gets it probably the most right of languages I've worked with, in that the choice of punctuation makes intuitive sense... "Hey! It's an Arrow. It goes from here to here". FWIW, I don't like the word "lambda" either... Naming things with one let…

I cannot agree enough. Someone give me a call when a Haskell-like language with readable syntax appears. I really like the ideas behind Haskell, but the syntax is so unfriendly that I cannot be bothered to really start using it. Less special characters, please.

There really aren't that many special characters (certainly no requirement for non-ascii) in the core language - perhaps the sin is allowing almost unlimited user defined operators in code, which leads to monstrosities like ekmett's lens infix operators: http://hackage.haskell.org/package/lens-4.1.2/docs/Control-L...

As a lisper, I prefer names and prefix operators - there's a few exceptions where infix operators add improvements to the language, but I dislike having user-defined operators (they make searching a pain, although Hayoo eases some of that).

Re: Unpythonic Python

#46
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Shorter and more pythonic, but not one per line: ' '.join(["FizzBuzz" if n%15==0 else "Fizz" if n%3==0 else "Buzz" if n%5==0 else str(n) for n in range(1,101)])

join on '\n' instead of ''. Problem solved.

Re: Unpythonic Python

#47
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

The jury is still out on whether this is a good thing or not.

IMHO, the objective of the crippled `lambda` really is to make the programmer refactor into reusable functions instead of having λs littered everywhere, which would hurt maintainability.

Re: Unpythonic Python

#48
post #44
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.

"let = lambda" would be the same thing, wouldn't it?

Yes, but in an incredibly verbose way. And a simple let->lambda conversion can't express things that Scheme's let* or letrec expressions let you do (in Haskell, a "let" or "where" expression is a "letrec").

Re: Unpythonic Python

#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 object? Do you want your fizzbuzz function to concatenate the strings or use explicit replacement? Can you make a switch for that? And so on.

That stuff only scratches the surface. I'm sure there's some brain burning fizz-buzz interviewers out there. It really puts you on the spot to deal with a stateful program.

One thing that seems really weird to me is this: given the first set of rules, why does almost everyone write the program that is the least extensible? Is it the way the question is phrased or scar tissue from imperative programming?

Re: Unpythonic Python

#50
post #35

A highly Pythonic, Easier to Ask Forgiveness than Permission[1] version: FIZZ=3 BUZZ=5 cache = {} for i in range(FIZZ-1, FIZZ*BUZZ, FIZZ): cache[i] = 'Fizz' for i in range(BUZZ-1, FIZZ*BUZZ, BUZZ): try: cache[i] += 'Buzz' except KeyError: cache[i] = 'Buzz' for i in range(100): try: print cache[i%(FIZZ*BUZZ)] except KeyError: print i+1 [1] https://docs.python.org/2/glossary.html#term-eafp A generator version: from ite…

I like these. Another generator version:

  def fbgen(text, divisor):
      while True:
          for i in range(1, divisor):
              yield ""
          yield text

  def derrangedBuzz(max):
      fizzer = fbgen("Fizz", 3)
      buzzer = fbgen("Buzz", 5)

      for i in range(1, max + 1):
          s = fizzer.next() + buzzer.next()
          
          if s == "":
              print i
          else:
              print s
Post reply on HN