趣文:Python程序员的进化史+python电子书资料分享!

下面代码来自 fmeyer ,列举了各种程序员所写的阶乘算法代码,甚至包括网页设计师的。

001

#新手程序员002

003

def factorial (x):004

005

if x == 0:006

007

return 1008

009

else:010

011

return x * factorial (x - 1)012

013

print factorial (6)014

015

#有一年 Pascal 经验的程序员016

017

def factorial (x):018

019

result = 1020

021

i = 2022

023

while i <= x:024

025

result = result * i026

027

i = i + 1028

029

return result030

031

print factorial (6)032

033

#有一年 C 经验的程序员034

035

def fact (x): #{036

037

result = i = 1;038

039

while (i <= x): #{040

041

result *= i;042

043

i += 1;044

045

#}046

047

return result;048

049

#}050

051

print(fact (6))052

053

#有一年 SICP 经验的程序员054

055

@tailcall056

057

def fact (x, acc=1):058

059

if (x > 1): return (fact ((x - 1), (acc * x)))060

061

else: return acc062

063

print(fact (6))064

065

#有一年 Python 经验的程序员066

067

def Factorial (x):068

069

res = 1070

071

for i in xrange (2, x + 1):072

073

res *= i074

075

return res076

077

print Factorial (6)078

079

#懒惰的 Python 程序员080

081

def fact (x):082

083

return x > 1 and x * fact (x - 1) or 1084

085

print fact (6)086

087

#更懒惰的 Python 程序员088

089

f = lambda x: x and x * f (x - 1) or 1090

091

print f (6)092

093

#专家级 Python 程序员094

095

import operator as op096

097

import functional as f098

099

fact = lambda x: f.foldl (op.mul, 1, xrange (2, x + 1))100

101

print fact (6)102

103

#Python 黑客104

105

import sys106

107

@tailcall108

109

def fact (x, acc=1):110

111

if x: return fact (x.__sub__(1), acc.__mul__(x))112

113

return acc114

115

sys.stdout.write (str (fact (6)) + '')116

117

#专家级程序员118

119

import c_math120

121

fact = c_math.fact122

123

print fact (6)124

125

#英国专家级程序员 (译注:在英式英语中,“数学”的简写,多用“maths”,不是“math"。)126

127

import c_maths128

129

fact = c_maths.fact130

131

print fact (6)132

133

#网页设计师134

135

def factorial (x):136

137

#-------------------------------------------------138

139

#--- 这段代码是从 Math Vault 那弄过来滴---140

141

#--- 计算阶乘 (C)亚瑟·史密斯 1999 年---142

143

#-------------------------------------------------144

145

result = str (1)146

147

i = 1 #谢谢亚当148

149

while i <= x:150

151

#result = result * i #It's faster to use *=152

153

#result = str (result * result + i)154

155

#result = int (result *= i) #??????156

157

result str (int (result) * i)158

159

#result = int (str (result) * i)160

161

i = i + 1162

163

return result164

165

print factorial (6)166

167

#Unix 程序员168

169

import os170

171

def fact (x):172

173

os.system ('factorial ' + str (x))174

175

fact (6)176

177

#Windows 程序员178

179

NULL = None180

181

def CalculateAndPrintFactorialEx (dwNumber,182

183

hOutputDevice,184

185

lpLparam,186

187

lpWparam,188

189

lpsscSecurity,190

191

*dwReserved):192

193

if lpsscSecurity != NULL:194

195

return NULL #Not implemented196

197

dwResult = dwCounter = 1198

199

while dwCounter <= dwNumber:200

201

dwResult *= dwCounter202

203

dwCounter += 1204

205

hOutputDevice.write (str (dwResult))206

207

hOutputDevice.write ('')208

209

return 1210

211

import sys212

213

CalculateAndPrintFactorialEx (6, sys.stdout, NULL, NULL, NULL, NULL, NULL,214

NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)215

216

#企业程序员217

218

def new (cls, *args, **kwargs):219

220

return cls (*args, **kwargs)221

222

class Number (object):223

224

pass225

226

class IntegralNumber (int, Number):227

228

def toInt (self):229

230

return new (int, self)231

232

class InternalBase (object):233

234

def __init__(self, base):235

236

self.base = base.toInt ()237

238

def getBase (self):239

240

return new (IntegralNumber, self.base)241

242

class MathematicsSystem (object):243

244

def __init__(self, ibase):245

246

Abstract247

248

@classmethod249

250

def getInstance (cls, ibase):251

252

try:253

254

cls.__instance255

256

except AttributeError:257

258

cls.__instance = new (cls, ibase)259

260

return cls.__instance261

262

class StandardMathematicsSystem (MathematicsSystem):263

264

def __init__(self, ibase):265

266

if ibase.getBase () != new (IntegralNumber, 2):267

268

raise NotImplementedError269

270

self.base = ibase.getBase ()271

272

def calculateFactorial (self, target):273

274

result = new (IntegralNumber, 1)275

276

i = new (IntegralNumber, 2)277

278

while i <= target:279

280

result = result * i281

282

i = i + new (IntegralNumber, 1)283

284

return result285

286

print StandardMathematicsSystem.getInstance (new (InternalBase,287

new (IntegralNumber, 2))) .calculateFactorial (new (IntegralNumber, 6))

下面代码是 kohashi 给出的,他说是在邮局看到的。

view source

print

?

01

#VBA 程序员02

03

def factorial (x):04

05

if x == 0:06

07

return 108

09

if x == 1:10

11

return x12

13

if x == 2:14

15

return x * (x-1)16

17

if x == 3:18

19

return x * (x-1) * (x-2)20

21

if x == 4:22

23

return x * (x-1) * (x-2) * (x-3)24

25

if x == 5:26

27

return x * (x-1) * (x-2) * (x-3) * (x-4)28

29

if x == 6:30

31

return x * (x-1) * (x-2) * (x-3) * (x-4) * (x-5)32

33

print factorial (6)

最后,想学习Python的小伙伴们!

请关注+私信回复:“学习”就可以拿到一份我为大家准备的Python学习资料!

趣文:Python程序员的进化史+python电子书资料分享!

pytyhon学习资料

趣文:Python程序员的进化史+python电子书资料分享!

python学习资料

相关推荐