Live data from Hacker News

The Evolution of a Python Programmer

gist.github.com

21–30 of 45 posts

Re: The Evolution of a Python Programmer

#21

    #Python hacker
    
    sys.stdout.write(str(fact(6)) + '\n')
The call to str is unneeded. For consistency, it should be

    sys.stdout.write(fact(6).__str__() + "\n")
And if that example want's to be really "hacker"ish, then every function call should actually be a call to the __call__ method of each object.

Re: The Evolution of a Python Programmer

#22
post #10

If you can live with floating point results: from math import gamma def factorial(x): return gamma(x+1) This has the advantage of working correctly for non-integer arguments.

Um, the math module already has a factorial function which performs faster than the gamma function. Why use gamma?

Re: The Evolution of a Python Programmer

#23
post #4

If anyone's interested (I am because I found myself to be a 'Lazier programmer') - Timeit's time for three functions: Lazy Programmer - 0.907521744301 Lazier Programmer - 1.0473810545812512 Using math.factorial - 0.12187403971609001

A 'lazier programmer' who actually checked the optimization of various factorial methods?

There's a joke in there somewhere...

Re: The Evolution of a Python Programmer

#25
post #22
post #10

If you can live with floating point results: from math import gamma def factorial(x): return gamma(x+1) This has the advantage of working correctly for non-integer arguments.

Um, the math module already has a factorial function which performs faster than the gamma function. Why use gamma?

I didn't know about the factorial function.

Re: The Evolution of a Python Programmer

#26
A true hackerish version perhaps:

    bc = [124, 0, 0, 114, 37, 0, 116, 0, 0, 124, 0, 0, 106, 2, 0, 100, 1, 0, 131, 1,
          0, 124, 1, 0, 106, 1, 0, 124, 0, 0, 131, 1, 0, 131, 2, 0, 83, 124, 1, 0,
          83]
    fact = type(lambda:0)(type((lambda:0).func_code)(2, 2, 7, 0,
                   ''.join(map(chr, bc)), (None, 1), ('fact', '__mul__', '__sub__'),
                   ('x', 'acc'), "n/a", "fact", 0, ""),
              globals(), "fact", (1,))

Re: The Evolution of a Python Programmer

#27
related educational stuff: here's three different ways to do lazy sequences (infinite seqs) in python: generators, closures, and classes. provides implementations of lazy-map and lazy-filter for each style. (the generator implementations are equivalent to those in itertools.) uses fib instead of fac. https://github.com/dustingetz/sandbox/blob/master/etc/lazy.p...

we can use these ideas to elegantly solve the second greplin challenge question: "find the smallest prime fibonacci, X, greater than 227,000; compute sum of prime divisors of X+1"

  pred = lambda x: x > 227000 and is_prime(x)
  X = take(lazy_filter(pred, fib_gen()), 1)[0]
  print sum(filter(is_prime, divisors(X+1)))
https://github.com/dustingetz/sandbox/blob/master/etc/grepli...

Re: The Evolution of a Python Programmer

#28

Ha - I like the [English] expert programmer. When I started coding I spent one evening looking for a maths.h bug...

I've had the reverse experience. My exposure to programming language libraries and American media has resulted in me accidentally saying "math" sometimes.

Re: The Evolution of a Python Programmer

#30

Half of those are incorrect implementations that junk the stack and most of the rest are idiotic rebaked jokes from the late 90s. Nowhere near to http://www.willamette.edu/~fruehr/haskell/evolution.html which is not only enlightening but actually funny when it tries to; or even to http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.... which was funny when it started and still is the original. Can we stop mak…

Oooo the next xkcd. I'd like to see that.
Post reply on HN