Hacker News new | past | comments | ask | show | jobs | submit login

For Python:

1) Here we note that Python's range() is not RH-inclusive.

  [i * 2 for i in range(1, 11)]
2) Ugh, reduce().

  reduce(operator.add, range(1, 1001))
3) any() is awesome.

  wordlist= ["scala", "akka", "play framework", "sbt", "typesafe"]
  tweet = "This is an example tweet talking about scala and sbt."
  any(word in tweet for word in wordList)
4) These are not very idiomatic in Python, but that's alright.

  fileText = open("data.txt").read()
  fileLines = open("data.txt").readlines()
5) Hm, interesting. To make it print correctly, I used a newline join.

  print "\n".join("Happy Birthday %s" % ("dear NAME" if i == 3 else "to You") for i in range(1, 5))
6) Ah, this is where it becomes apparent that Python is not a functional language. On the other hand, this version doesn't hide the actual expense of list allocation, so I guess it evens out. Can somebody come up with a better answer here?

  passed = []; failed = []; [passed.append(i) if i > 60 else failed.append(i) for i in [49, 58, 76, 82, 88, 90]]
7) Not gonna bother. Use lxml.

8) Oh, this is easy.

  min([14, 35, -7, 46, 98])
  max([14, 35, -7, 46, 98])
9) Entire books have been written on this. Python does have multiprocessing.Pool.map(), though it takes a bit of setup.

10) Whew, this took me a second. You'll want to import some of these from itertools, and it needs to be redefined each time, since lambdas can't really define local stuff easily. On the plus side, it's shorter than the Scala flavor, and relies on standard stuff. :3

  g = lambda l=[2]: ifilter(lambda x: (all(x % i for i in l) and (l.append(x) or True)), count(3))
That was fun!



For 6) itertools.groupby() helps a bit:

  import itertools
  passed, failed = (list(v) for k,v in itertools.groupby([49, 58, 76, 82, 88, 90], lambda x: x > 60))


You could do 6 like so for kicks (it's pretty horrible and writing a real partition function is easy neough):

    passed, failed = reduce(lambda r, g: r[g <= 60].append(g) or r, [49, 58, 76, 82, 88, 90], ([], []))


I often wonder why people would use anything other than Python for doing real work. Such a fantastic language! I admit I am somewhat of a fanboy but I feel like it's for a good reason. The language is so easy to read, like reading a sentence. I cannot recall who said this (I want to say Steve Jobs) but its something on the lines of "something is only perfect when there is nothing left to take away" .. The fact that Python is missing so many of the crazy symbols and reads more like natural language while still being able to do so much gives me reason to believe it's one of tue greatest scripting languages around, and certainly clowns on Scala for these examples. I really wonder why people continue to write web apps in PHP for example.


> I cannot recall who said this (I want to say Steve Jobs)

Antoine de Saint-Exupery. Il semble que la perfection soit atteinte non quand il n'y a plus rien à ajouter, mais quand il n'y a plus rien à retrancher. "It seems that perfection is attained not when there is nothing more to add, but when there is nothing more to remove."

But don't let that stop you say "Steve Jobs" if you happen to enjoy saying it :-).


You should use sum for #2:

  sum(range(1,1001))


oh come on, for #2 you don't want to use sum() ? :)


As others have pointed out, sum() is the right thing for (2), and groupby() could be used to build a real partition for (6). Clearly I should have tried this in the morning, after a full night's sleep. :3 More seriously, for (2), (9), and (10), I was trying to answer with similar one-liners that have the same kind of complexity and results, to show that Python's syntax isn't that scary.

Also, I should note that there is a one-liner for using urllib and lxml to grab and parse an XML file, but there's another one-liner using Twisted to do the same thing; it depends on your context. Why do you want the XML? What are you doing with it? That is something important that needs to be considered.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: