Created
February 10, 2010 12:36
-
-
Save suguby/300267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sprint_lib | |
class TailRecurseException: | |
def __init__(self, args, kwargs): | |
self.args = args | |
self.kwargs = kwargs | |
def tailcall(g): | |
""" | |
This function decorates a function with tail call | |
optimization. It does this by throwing an exception | |
if it is it's own grandparent, and catching such | |
exceptions to fake the tail call optimization. | |
This function fails if the decorated | |
function recurses in a non-tail context. | |
""" | |
def func(*args, **kwargs): | |
f = sys._getframe() | |
if f.f_back and f.f_back.f_back \ | |
and f.f_back.f_back.f_code == f.f_code: | |
raise TailRecurseException(args, kwargs) | |
else: | |
while 1: | |
try: | |
return g(*args, **kwargs) | |
except TailRecurseException, e: | |
args = e.args | |
kwargs = e.kwargs | |
func.__doc__ = g.__doc__ | |
return func | |
#Newbie programmer | |
def factorial_1(x): | |
if x == 0: | |
return 1 | |
else: | |
return x * factorial_1(x - 1) | |
def Newbie(param): | |
return factorial_1(param) | |
#First year programmer, studied Pascal | |
def factorial_2(x): | |
result = 1 | |
i = 2 | |
while i <= x: | |
result = result * i | |
i = i + 1 | |
return result | |
def First_year_programmer_studied_Pascal(param): | |
return factorial_2(param) | |
#First year programmer, studied C | |
def fact_3(x): #{ | |
result = i = 1; | |
while (i <= x): #{ | |
result *= i; | |
i += 1; | |
#} | |
return result; | |
#} | |
def First_year_programmer_studied_C(param): | |
return fact_3(param) | |
#First year programmer, SICP | |
@tailcall | |
def fact_4(x, acc=1): | |
if (x > 1): return (fact_4((x - 1), (acc * x))) | |
else: return acc | |
def First_year_programmer_SICP(param): | |
return fact_4(param) | |
#First year programmer, Python | |
def Factorial_5(x): | |
res = 1 | |
for i in xrange(2, x + 1): | |
res *= i | |
return res | |
def First_year_programmer_Python(param): | |
return Factorial_5(param) | |
#Lazy Python programmer | |
def fact_6(x): | |
return x > 1 and x * fact_6(x - 1) or 1 | |
def Lazy_Python_programmer(param): | |
return fact_6(param) | |
#Lazier Python programmer | |
f_7 = lambda x: x and x * f_7(x - 1) or 1 | |
def Lazier_Python_programmer(param): | |
return f_7(param) | |
#Python expert programmer | |
#import operator as op_8 | |
#import functional as f_8 | |
#fact_8 = lambda x: f_8.foldl(op_8.mul, 1, xrange(2, x + 1)) | |
#def Python_expert_programmer(param): | |
# return fact_8(param) | |
#Python hacker | |
import sys | |
@tailcall | |
def fact_9(x, acc=1): | |
if x: return fact_9(x.__sub__(1), acc.__mul__(x)) | |
return acc | |
def Python_hacker(param): | |
#sys.stdout.write(str(fact(param)) + '\n') | |
return fact_9(param) | |
#EXPERT PROGRAMMER | |
#import c_math | |
#fact_10 = c_math.fact | |
#def EXPERT_PROGRAMMER(param): | |
# return fact_10(param) | |
#ENGLISH EXPERT PROGRAMMER | |
#import c_maths | |
#fact_11 = c_maths.fact | |
#def ENGLISH_EXPERT_PROGRAMMER(param): | |
# return fact_11(param) | |
#Web designer | |
def factorial_12(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 | |
def Web_designer(param): | |
return factorial_12(param) | |
##Unix programmer | |
#import os | |
#def fact_13(x): | |
# os.system('factorial ' + str(x)) | |
#def Unix_programmer(param): | |
# fact_13(param) | |
#Windows programmer | |
NULL = None | |
def CalculateAndPrintFactorialEx_14(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 | |
def Windows_programmer(param): | |
CalculateAndPrintFactorialEx_14(param, sys.stderr, 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 | |
def Enterprise_programmer(param): | |
return StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, param)) | |
param = 6 | |
sprint_lib.test_perf( | |
[ | |
Newbie, | |
First_year_programmer_studied_Pascal, | |
First_year_programmer_studied_C, | |
First_year_programmer_SICP, | |
First_year_programmer_Python, | |
Lazy_Python_programmer, | |
Lazier_Python_programmer, | |
# Python_expert_programmer, | |
Python_hacker, | |
# EXPERT_PROGRAMMER, | |
# ENGLISH_EXPERT_PROGRAMMER, | |
Web_designer, | |
# Unix_programmer, | |
# Windows_programmer, | |
Enterprise_programmer, | |
], | |
param | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment