Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Evolution of a Python programmer

Name: Anonymous 2007-05-25 5:34 ID:e87L90K/


#Newbie programmer
def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x - 1)
print factorial(6)


#First year programmer, studied Pascal
def factorial(x):
    result = 1
    i = 2
    while i <= x:
        result = result * i
        i = i + 1
    return result
print factorial(6)


#First year programmer, studied C
def fact(x): #{
    result = i = 1;
    while (i <= x): #{
        result *= i;
        i += 1;
    #}
    return result;
#}
print(fact(6))


#First year programmer, SICP
@tailcall
def fact(x, acc=1):
    if (x > 1): return (fact((x - 1), (acc * x)))
    else:       return acc
print(fact(6))


#First year programmer, Python
def Factorial(x):
    res = 1
    for i in xrange(2, x + 1):
        res *= i
    return res
print Factorial(6)


#Lazy Python programmer
def fact(x):
    return x > 1 and x * fact(x - 1) or 1
print fact(6)


#Lazier Python programmer
f = lambda x: x and x * f(x - 1) or 1
print f(6)


#Python expert programmer
import operator as op
import functional as f
fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact(6)


#Python hacker
import sys
@tailcall
def fact(x, acc=1):
    if x: return fact(x.__sub__(1), acc.__mul__(x))
    return acc
sys.stdout.write(str(fact(6)) + '\n')


#EXPERT PROGRAMMER
import c_math
fact = c_math.fact
print fact(6)


#ENGLISH EXPERT PROGRAMMER
import c_maths
fact = c_maths.fact
print fact(6)


#Web designer
def factorial(x):
    #-------------------------------------------------
    #--- Code snippet from The Math Vault          ---
    #--- Calculate factorial (C) Arthur Smith 1999 ---
    #-------------------------------------------------
    result = str(1)
    i = 1 #Thanks Adam
    while i <= x:
        #result = result * i  #It's faster to use *=
        #result = str(result * result + i)
           #result = int(result *= i) #??????
        result str(int(result) * i)
        #result = int(str(result) * i)
        i = i + 1
    return result
print factorial(6)


#Unix programmer
import os
def fact(x):
    os.system('factorial ' + str(x))
fact(6)


#Windows programmer
NULL = None
def CalculateAndPrintFactorialEx(dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 *dwReserved):
    if lpsscSecurity != NULL:
        return NULL #Not implemented
    dwResult = dwCounter = 1
    while dwCounter <= dwNumber:
        dwResult *= dwCounter
        dwCounter += 1
    hOutputDevice.write(str(dwResult))
    hOutputDevice.write('\n')
    return 1
import sys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)


#Enterprise programmer
def new(cls, *args, **kwargs):
    return cls(*args, **kwargs)

class Number(object):
    pass

class IntegralNumber(int, Number):
    def toInt(self):
        return new (int, self)

class InternalBase(object):
    def __init__(self, base):
        self.base = base.toInt()

    def getBase(self):
        return new (IntegralNumber, self.base)

class MathematicsSystem(object):
    def __init__(self, ibase):
        Abstract

    @classmethod
    def getInstance(cls, ibase):
        try:
            cls.__instance
        except AttributeError:
            cls.__instance = new (cls, ibase)
        return cls.__instance

class StandardMathematicsSystem(MathematicsSystem):
    def __init__(self, ibase):
        if ibase.getBase() != new (IntegralNumber, 2):
            raise NotImplementedError
        self.base = ibase.getBase()

    def calculateFactorial(self, target):
        result = new (IntegralNumber, 1)
        i = new (IntegralNumber, 2)
        while i <= target:
            result = result * i
            i = i + new (IntegralNumber, 1)
        return result

print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))

Name: Anonymous 2009-03-13 6:44

>>158
>>> import timeit
>>> timeit.Timer("200 >> 1").timeit()
0.1076511120826809
>>> timeit.Timer("200 / 2").timeit()
0.219191824659279


In my book, twice as fast hardly constitutes ``isn't any faster''.

Name: Anonymous 2009-03-13 7:00

>>161
>>> timeit.Timer("long(200) >> 1").timeit()
3.8013210296630859
>>> timeit.Timer("long(200) / 2").timeit()
4.186870813369751

in my book 1.1 times as fast isn't twice as fast.

Name: Anonymous 2009-03-13 7:32

>>162
Your ``benchmark'' is biased, since you do not only count the actual division/shift, but also the function call long. Since calling functions in Python is a costly operation, you'd naturally see suboptimal results.

>>> timeit.Timer("200L >> 1").timeit()
0.19257593154907227
>>> timeit.Timer("200L / 2").timeit()
0.70366883277893066


Oh, look, the shift operator is now actually 3.5 times as fast as the division operator.

Name: Anonymous 2009-03-13 8:19

>>161-163
all those benchmarks are biased, because the division or shift is such a small part of the function:
>>> timeit.Timer("factorial.factorial_div(10000)","import factorial").timeit(100)
13.16028904914856
>>> timeit.Timer("factorial.factorial_shift(10000)","import factorial").timeit(100)
13.174507141113281

Name: Anonymous 2009-03-13 8:44

>>164
Oh, we were actually talking about factorials? I didn't read the thread, so I didn't notice.

Name: Anonymous 2009-03-13 9:18

>>165
This is /prog/, every thread is about factorials.

Name: Anonymous 2009-03-13 15:23

>>164
how is the version with shift actually slower sometimes?

Name: Anonymous 2009-03-13 16:04

>>167
because the division or shift is such a small part of the function

Name: Anonymous 2009-03-13 19:31

>>157
>>157
>>160
Yes, I said bitwise operations would be faster for calculating the lg of an integer. Not simply that shifting is faster than dividing by two. Try actually thinking next time, you can find the lg in a constant number of shifts every time.

Name: Anonymous 2009-03-14 3:16

>>169
What is lg?

Name: Anonymous 2009-03-14 3:22

you can find the lg in a constant number of shifts every time.
no, you can't. you can't find the lg of an arbitrary-sized integer in a constant number of shifts.

Name: Anonymous 2009-03-14 4:07

>>171
there aren't any significant architectures which store integers as arbitrary-size.

Name: Anonymous 2009-03-14 5:00

>>171
Very convincing counter argument, good job finding a technological niche for which the described solution will not work. I'm sure your boss will be sure to give you a raise on monday when you recommend your company switch to your new amazing arbitrary-size integer architecture.

Name: Anonymous 2009-03-14 5:32

>>172-173
post your code that can find the binary logarithm of any long in python in a constant number of shifts.
that's the only way you're going to convince anyone that it's possible.

Name: Anonymous 2009-03-14 6:35

SEXHORSE

Name: Anonymous 2009-03-14 10:07

>>174
What are you retarded? Longs in python ARE arbitrary length- which was exactly what the quoted posts said it would NOT work for. Lets move past this however and discuss practicality. Who in their right mind that is writing a factorial function would make it accept a long parameter, just incase somebody wants to calculate the factorial of 2^31? Hell, I'll tell you what else. If someone, for a practical reason needed to calculate the factorial of a number greater than or equal to 2^31, they would most certainly NOT be using python. Get off your theoretical python programming high horse and actually think about how something would be implemented before you ask for an actual implementation.

Name: Anonymous 2009-03-14 10:23

>>176
pathetic python programming

Name: Anonymous 2009-03-14 11:36

>>176
anyone calculating factorials higher than 20! probably wouldn't be using python.

Name: Anonymous 2009-03-14 11:50

>>178
I use pythong for EVERYTHING.

Name: Anonymous 2009-03-14 12:33

>>179
when you post on /prog/, you use PHP

Name: Anonymous 2009-03-14 14:08

I lol'd at the windows one.

Name: Anonymous 2009-03-14 14:12

>>180
World4ch's Shiichan is a mod_python port.

Name: Anonymous 2009-03-14 17:37

>>182
[Citation needed]

Name: Anonymous 2009-03-14 17:39

>>183
^ "World4ch's Shiichan is a mod_python port." World4chan. March 14, 2009. http://dis.4chan.org/read/prog/1180084983/182. Retrieved on March 14.

Name: Anonymous 2009-03-21 19:40

Python programmers do not evolve. They will remain shit-eaters for all eternity, hoping that their species one day dies off so the world can have their peace of them.

Name: Anonymous 2009-03-21 19:47

>>184
LOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL so clever! IHBT

Name: Anonymous 2009-03-21 21:17

>>186
YBHT

Name: Anonymous 2009-03-21 22:13

>>187
No shit.

Name: RMT-SP 2009-03-31 0:40

こんにちは
rmt-spゲームショップでございます
いつもお世話になっております
デカロン--DEKARON激安販売中!
ご注文はこちらへ
http://www.rmt-sp.com/MoneyList-50.aspx
もしお必要でしたら、是非利用してください
どうぞよろしくお願いいたします
************************************
RMT超低価格!
法人運営だから安心、安全100%!
丁寧な対応、簡単な注文、安心な取引!
営業時間:年中無休!24時間営業
ホームページ: http://www.rmt-sp.com
PCメール:rmt_sp01@yahoo.co.jp
MSN: rmt_sp@hotmail.co.jp
************************************

Name: Anonymous 2009-03-31 5:32

HEY GUISE!

fak=lambda x:x*[lambda c:1,lambda c:fak(c-1)][x!=1](x)

Name: Anonymous 2009-03-31 6:31

Name: Anonymous 2009-03-31 13:08

Windows Developer is so true

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List