Live data from Hacker News

Unpythonic Python

skien.cc

151–156 of 156 posts

Re: Unpythonic Python

#151

There's no honest attempt to explain the philosophy behind every language with real-world examples in Python. It's just childish jokes. It's the equivalent of saying "Ze fish in le river" is "Frenchish English". Ha ha, stupid French people, right? It's easy to make fun of programming languages like this, but what does that teach us? Aside from to mock what is different than what we use right now?

As any French person will tell you, it should be "Ze fish in la river".

Re: Unpythonic Python

#152
post #131
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.

And when you write C carefully, the C code looks like this (thanks seanjensengrey and rhth54656 for ideas): int i; static char* a[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" }; for ( i = 1; i The loosely similar Python: for i in range( 1, 101 ): print( [ i,'Fizz','Buzz','FizzBuzz' ][ (i%5==0)*2 + (i%3==0) ] )

Taking that a little further,

  const char *fb[][2] = {{"%d\n", "Fizz\n"}, {"Buzz\n", "FizzBuzz\n"}};
  int i;
  for (i = 1; i 
(It's a pity we have to specify the second dimension manually . . .)

Re: Unpythonic Python

#153

Earlier quoted context omitted.

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.

Well, that example is a bit different. You're ignoring who the audience is. When writing code, the audience is usually at least people on the same team, if not the person writing in the first place. If writing in French for people who are not fluent, I think it would be a good idea to avoid idiomatic language.

It's not different. The audience in this case is people who are fluent in Python. Saying that code sample is bad is like saying Baudelaire is bad because only people fluent in French can read it.

Re: Unpythonic Python

#154
i loved this; studying multiple implementations of the same recipe, even if most are not intended to be "exemplary" but rather cautionary, is a great way to learn.

This reminds me of a similar effort called "evolution of a python programmer" and published in this gist:

https://gist.github.com/ghoseb/25049

something like 30 different implementations of factorial; my favorites are the 'expert programmer' and the "web designer"

Re: Unpythonic Python

#155

Earlier quoted context omitted.

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.

If it can be satisfied in a way that is materially different from what was intended, then it is both under-specified and incorrect. This is a not-uncommon source of errors.

Natural language can be used with precision, and this is an important skill for engineers. Being able to identify ambiguity and inconsistency is an important part of that skill so yes, noticing it within a question should count in favor of the candidate. Dismissing it as being pedantic is the wrong response, because if you are working on something critical (secure communications software, for example) you need to handle ideas with precision.

Re: Unpythonic Python

#156

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

[concat $ replicate (fromEnum $ x `mod` 3 == 0) "Fizz" ++ replicate (fromEnum $ x `mod` 5 == 0) "Buzz" | x or

let f = (replicate . fromEnum . (==0)) .: mod in [concat $ f x 3 "Fizz" ++ f x 5 "Buzz" | x <- [1..101]]

Post reply on HN