alg0rith

Evolution of a Python Programmer

Jun 5th, 2013
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #Newbie programmer
  2. def factorial(x):
  3. if x == 0:
  4. return 1
  5. else:
  6. return x * factorial(x - 1)
  7. print factorial(6)
  8.  
  9.  
  10. #First year programmer, studied Pascal
  11. def factorial(x):
  12. result = 1
  13. i = 2
  14. while i <= x:
  15. result = result * i
  16. i = i + 1
  17. return result
  18. print factorial(6)
  19.  
  20.  
  21. #First year programmer, studied C
  22. def fact(x): #{
  23. result = i = 1;
  24. while (i <= x): #{
  25. result *= i;
  26. i += 1;
  27. #}
  28. return result;
  29. #}
  30. print(fact(6))
  31.  
  32.  
  33. #First year programmer, SICP
  34. @tailcall
  35. def fact(x, acc=1):
  36. if (x > 1): return (fact((x - 1), (acc * x)))
  37. else: return acc
  38. print(fact(6))
  39.  
  40.  
  41. #First year programmer, Python
  42. def Factorial(x):
  43. res = 1
  44. for i in xrange(2, x + 1):
  45. res *= i
  46. return res
  47. print Factorial(6)
  48.  
  49.  
  50. #Lazy Python programmer
  51. def fact(x):
  52. return x > 1 and x * fact(x - 1) or 1
  53. print fact(6)
  54.  
  55.  
  56. #Lazier Python programmer
  57. f = lambda x: x and x * f(x - 1) or 1
  58. print f(6)
  59.  
  60.  
  61. #Python expert programmer
  62. fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)
  63. print fact(6)
  64.  
  65.  
  66. #Python hacker
  67. import sys
  68. @tailcall
  69. def fact(x, acc=1):
  70. if x: return fact(x.__sub__(1), acc.__mul__(x))
  71. return acc
  72. sys.stdout.write(str(fact(6)) + '\n')
  73.  
  74.  
  75. #EXPERT PROGRAMMER
  76. from c_math import fact
  77. print fact(6)
  78.  
  79.  
  80. #BRITISH EXPERT PROGRAMMER
  81. from c_maths import fact
  82. print fact(6)
  83.  
  84.  
  85. #Web designer
  86. def factorial(x):
  87. #-------------------------------------------------
  88. #--- Code snippet from The Math Vault ---
  89. #--- Calculate factorial (C) Arthur Smith 1999 ---
  90. #-------------------------------------------------
  91. result = str(1)
  92. i = 1 #Thanks Adam
  93. while i <= x:
  94. #result = result * i #It's faster to use *=
  95. #result = str(result * result + i)
  96. #result = int(result *= i) #??????
  97. result = str(int(result) * i)
  98. #result = int(str(result) * i)
  99. i = i + 1
  100. return result
  101. print factorial(6)
  102.  
  103.  
  104. #Unix programmer
  105. import os
  106. def fact(x):
  107. os.system('factorial ' + str(x))
  108. fact(6)
  109.  
  110.  
  111. #Windows programmer
  112. NULL = None
  113. def CalculateAndPrintFactorialEx(dwNumber,
  114. hOutputDevice,
  115. lpLparam,
  116. lpWparam,
  117. lpsscSecurity,
  118. *dwReserved):
  119. if lpsscSecurity != NULL:
  120. return NULL #Not implemented
  121. dwResult = dwCounter = 1
  122. while dwCounter <= dwNumber:
  123. dwResult *= dwCounter
  124. dwCounter += 1
  125. hOutputDevice.write(str(dwResult))
  126. hOutputDevice.write('\n')
  127. return 1
  128. import sys
  129. CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  130.  
  131.  
  132. #Enterprise programmer
  133. def new(cls, *args, **kwargs):
  134. return cls(*args, **kwargs)
  135.  
  136. class Number(object):
  137. pass
  138.  
  139. class IntegralNumber(int, Number):
  140. def toInt(self):
  141. return new (int, self)
  142.  
  143. class InternalBase(object):
  144. def __init__(self, base):
  145. self.base = base.toInt()
  146.  
  147. def getBase(self):
  148. return new (IntegralNumber, self.base)
  149.  
  150. class MathematicsSystem(object):
  151. def __init__(self, ibase):
  152. Abstract
  153.  
  154. @classmethod
  155. def getInstance(cls, ibase):
  156. try:
  157. cls.__instance
  158. except AttributeError:
  159. cls.__instance = new (cls, ibase)
  160. return cls.__instance
  161.  
  162. class StandardMathematicsSystem(MathematicsSystem):
  163. def __init__(self, ibase):
  164. if ibase.getBase() != new (IntegralNumber, 2):
  165. raise NotImplementedError
  166. self.base = ibase.getBase()
  167.  
  168. def calculateFactorial(self, target):
  169. result = new (IntegralNumber, 1)
  170. i = new (IntegralNumber, 2)
  171. while i <= target:
  172. result = result * i
  173. i = i + new (IntegralNumber, 1)
  174. return result
  175.  
  176. print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))
Advertisement
Add Comment
Please, Sign In to add comment