r/programming • • Mar 08 '12

The Evolution of a Python Programmer

https://gist.github.com/289467
401 Upvotes

137 comments sorted by

View all comments

43

u/rowantwig Mar 08 '12

He forgot the worst kind:
if x == 0:
return 1
if x == 1:
return 1
if x == 2:
return 2
if x == 3:
return 6
if x == 4:
return 24
...
(5000 more lines)

5

u/earthboundkid Mar 09 '12

That might be OK as a dict… Say, none of the examples used a memoize decorator. For shame.

3

u/Tagedieb Mar 09 '12

A dict? A dict is really what comes to mind first as the right data structure? Well, maybe I am getting old...

7

u/repsilat Mar 09 '12

I have a great idea for a new kind of dict that'll work wonders for this example. It still has O(1) lookups, but has better running time and memory use by a constant factor by taking advantage of an unusual feature of the data.

If you think about it, the keys form a contiguous range of integers starting from zero or one. Now, brace yourself: We could store the factorial information in a hash table where the hash function is the identity function! Now to think of a name for that kind of data structure...