Python: Choice of the one obvious way to do it
I just worked on a new Rosetta Code task on Fibonacci-like sequences where the solution works by giving the initial conditions and being returned something that can thencreate members of the defined series. You create a series generator factory then use the series generator returned. (The above uses the word generator in its non programmingspecific sense).
Currently we have three versions of answers to the task in Python:
Python: function returning a function
addnum = len(start)
memo = start[:]
def fibber(n):
try:
return memo[n]
except IndexError:
ans = sum(fibber(i) for i in range(n-addnum, n))
memo.append(ans)
return ans
return fibber
>>> fibo = fiblike([1,1])
>>> [fibo(i) for i in range(10)]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> lucas = fiblike([2,1])
>>> [lucas(i) for i in range(10)]
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
>>> for n, name in zip(range(2,11), 'fibo tribo tetra penta hexa hepta octo nona deca'.split()) :
fibber = fiblike([1] + [2**i for i in range(n-1)])
print('n=%2i, %5snacci -> %s ...' % (n, name, ' '.join(str(fibber(i)) for i in range(15))))
n= 2, fibonacci -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...
n= 3, tribonacci -> 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...
n= 4, tetranacci -> 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...
n= 5, pentanacci -> 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...
n= 6, hexanacci -> 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...
n= 7, heptanacci -> 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...
n= 8, octonacci -> 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ...
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ...
>>>
Currently we have three versions of answers to the task in Python:
Python: function returning a function
CODE:
>>> def fiblike(start):addnum = len(start)
memo = start[:]
def fibber(n):
try:
return memo[n]
except IndexError:
ans = sum(fibber(i) for i in range(n-addnum, n))
memo.append(ans)
return ans
return fibber
>>> fibo = fiblike([1,1])
>>> [fibo(i) for i in range(10)]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> lucas = fiblike([2,1])
>>> [lucas(i) for i in range(10)]
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
>>> for n, name in zip(range(2,11), 'fibo tribo tetra penta hexa hepta octo nona deca'.split()) :
fibber = fiblike([1] + [2**i for i in range(n-1)])
print('n=%2i, %5snacci -> %s ...' % (n, name, ' '.join(str(fibber(i)) for i in range(15))))
n= 2, fibonacci -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...
n= 3, tribonacci -> 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...
n= 4, tetranacci -> 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...
n= 5, pentanacci -> 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...
n= 6, hexanacci -> 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...
n= 7, heptanacci -> 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...
n= 8, octonacci -> 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ...
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ...
>>>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732040/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- One simple way to draw canvas, wxml2canvasCanvasXML
- One more way regarding germany niubian how to remain difficult forREMAI
- James Gleick:It's obvious to go to the future!Go
- Freedom of Choice
- SAP UI5控制元件資料繫結的三種模式 - One Way, Two Way和OneTime實現原理比較UI控制元件模式
- python & MySQLdb(one)PythonMySql
- np.random.choicerandom
- 《Learn python the hard way》Exercise 48: Advanced User InputPython
- The Way to HappinessAPP
- The Way To Go --- 切片Go
- oracle 豎表,橫表(a stupid way,want the smart way)Oracle
- Linux AS4.0下登入提示“Do you want to choose a different one? [n] ” 處理方法Linux
- python 編輯器提示 do not use bare exceptPython
- Python機器學習筆記——One Class SVMPython機器學習筆記
- Just Do IT
- 演算法之路 - Way to Algorithm演算法Go
- POJ 2891 Strange Way to Express IntegersExpress
- Learning Django: the hard way (1)Django
- 擁抱極限程式設計(Do do XP)程式設計
- Do more, fasterAST
- tomcat one connection one thread one request one threadTomcatthread
- New way to deploy SSL VPN in LinuxLinux
- The Best Way to Export an SVG from SketchExportSVG
- Get table and index DDL in the easy way(轉)Index
- Objectify: A Better Way to Build Rails ApplicationsObjectUIAIAPP
- 推薦 | 通往AGI之路(Way to AGI)
- CF 2B The least round way(DP)AST
- Do we need HACMP?ACM
- How do you pronounce IT?
- Do not trust anybody!Rust
- 善用 do-whileWhile
- oracle expert one on one (chm ebook)Oracle
- EXPERT ONE-ON-ONE ORACLE總結Oracle
- Python 為什麼不設計 do-while 迴圈結構?PythonWhile
- a-better-way-to-find-literal-sqls-in-oracle-10g/SQLOracle
- 提問的智慧How To Ask Questions The Smart Way
- nohup not working, another way to get the script run in the background
- JDJ釋出的READER CHOICE,居然沒有HIBERNATE!