# Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 3.96 KB  |  hits: 11  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
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. import operator as op
63. import functional as f
64. fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
65. print fact(6)
66.
67.
68. #Python hacker
69. import sys
70. @tailcall
71. def fact(x, acc=1):
72.     if x: return fact(x.__sub__(1), acc.__mul__(x))
73.     return acc
74. sys.stdout.write(str(fact(6)) + '\n')
75.
76.
77. #EXPERT PROGRAMMER
78. import c_math
79. fact = c_math.fact
80. print fact(6)
81.
82.
83. #ENGLISH EXPERT PROGRAMMER
84. import c_maths
85. fact = c_maths.fact
86. print fact(6)
87.
88.
89. #Web designer
90. def factorial(x):
91.     #-------------------------------------------------
92.     #--- Code snippet from The Math Vault          ---
93.     #--- Calculate factorial (C) Arthur Smith 1999 ---
94.     #-------------------------------------------------
95.     result = str(1)
96.     i = 1 #Thanks Adam
97.     while i <= x:
98.         #result = result * i  #It's faster to use *=
99.         #result = str(result * result + i)
100.            #result = int(result *= i) #??????
101.         result str(int(result) * i)
102.         #result = int(str(result) * i)
103.         i = i + 1
104.     return result
105. print factorial(6)
106.
107.
108. #Unix programmer
109. import os
110. def fact(x):
111.     os.system('factorial ' + str(x))
112. fact(6)
113.
114.
115. #Windows programmer
116. NULL = None
117. def CalculateAndPrintFactorialEx(dwNumber,
118.                                  hOutputDevice,
119.                                  lpLparam,
120.                                  lpWparam,
121.                                  lpsscSecurity,
122.                                  *dwReserved):
123.     if lpsscSecurity != NULL:
124.         return NULL #Not implemented
125.     dwResult = dwCounter = 1
126.     while dwCounter <= dwNumber:
127.         dwResult *= dwCounter
128.         dwCounter += 1
129.     hOutputDevice.write(str(dwResult))
130.     hOutputDevice.write('\n')
131.     return 1
132. import sys
133. CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
134.
135.
136. #Enterprise programmer
137. def new(cls, *args, **kwargs):
138.     return cls(*args, **kwargs)
139.
140. class Number(object):
141.     pass
142.
143. class IntegralNumber(int, Number):
144.     def toInt(self):
145.         return new (int, self)
146.
147. class InternalBase(object):
148.     def __init__(self, base):
149.         self.base = base.toInt()
150.
151.     def getBase(self):
152.         return new (IntegralNumber, self.base)
153.
154. class MathematicsSystem(object):
155.     def __init__(self, ibase):
156.         Abstract
157.
158.     @classmethod
159.     def getInstance(cls, ibase):
160.         try:
161.             cls.__instance
162.         except AttributeError:
163.             cls.__instance = new (cls, ibase)
164.         return cls.__instance
165.
166. class StandardMathematicsSystem(MathematicsSystem):
167.     def __init__(self, ibase):
168.         if ibase.getBase() != new (IntegralNumber, 2):
169.             raise NotImplementedError
170.         self.base = ibase.getBase()
171.
172.     def calculateFactorial(self, target):
173.         result = new (IntegralNumber, 1)
174.         i = new (IntegralNumber, 2)
175.         while i <= target:
176.             result = result * i
177.             i = i + new (IntegralNumber, 1)
178.         return result
179.
180. print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))