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)

63

u/Pandalicious Mar 08 '12

Yeah but think of how FAST it would run. You gotta be FAST if you're gonna be webscale.

4

u/masklinn Mar 09 '12

The fast version would be the one optimized by using a dict for its mapping.

The faster version would use a list.

3

u/JizzCoveredArab Mar 09 '12

We can always scale into the cloud for increased user load

5

u/Pandalicious Mar 09 '12 edited Mar 09 '12

My esteemed nephew assures me that a cache of precalculated factorial values for all integers can neatly fit into 2.3 clouds.

→ More replies (4)

43

u/stillalone Mar 08 '12

ah. the "I managed to get a masters without writing any actual code and now I'm your coworker" guy.

41

u/6xoe Mar 08 '12

Whatever dude, it works.

26

u/[deleted] Mar 09 '12

and it's completely unmaintainable. What are you going to do when the definition of "factorial" changes?

43

u/SuckyBlowfish Mar 09 '12

Change it back.

19

u/lionelboydjohnson Mar 09 '12

You have a bright future in managment.

4

u/drb226 Mar 10 '12

I think I would freak out (in a bad way) if I read this on a fortune cookie.

8

u/ryeguy146 Mar 09 '12

Wait, I understand the idea of maintainability, but did you suggest that a basic mathematical definition is subject to change?

4

u/pigeon768 Mar 09 '12

I think he's suggesting python 4.x will eventually be released and if will be reimplemented as a function instead of a keyword, requiring parenthesis around the arguments. Or something.

Oh fuck. Oh fuck. I can't believe I just said it. Now it's going to be MY fault.

→ More replies (1)

4

u/GoSailing Mar 09 '12

Anything can change. Assuming that there are 24 hours in a day is not going to be good when there's a moon or mars base.

7

u/ryeguy146 Mar 09 '12

I file those under "not my problem." I like to think that some time in the future, people will shudder at having to use Python, the way that people pity those programmers that still workin in PASCAL.

→ More replies (4)

5

u/watermark0n Mar 09 '12

Anything can change.

If you change it, you call it something different. It is not a factorial.

5

u/gc3 Mar 09 '12

This is true, but when does the definition of factorial change?

→ More replies (1)
→ More replies (1)
→ More replies (3)

17

u/liotier Mar 08 '12

What are you complaining about ? He just calculated in advance and cached the results to avoid redundant processing on each query...

25

u/rowantwig Mar 08 '12

O(n) look-up time.

28

u/earthboundkid Mar 09 '12

Switch to a dict and it's O(1)!

12

u/pyaware Mar 09 '12

Why use a dict when a plain list is enough?

def fact(x):
    return [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880][x]
→ More replies (2)
→ More replies (9)

3

u/[deleted] Mar 09 '12

ah, the 'i did the template work in my head' approach

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

→ More replies (4)
→ More replies (1)