作者beatitude (...)
站内Python
标题Re: [闲聊] 数字翻译成英文
时间Fri Apr 13 22:17:27 2012
感谢分享, 你写得漂亮很多 :)
说老实话我看到题目时没甚麽头绪
我的策略是:
1.如果能实作出atom的部分(个位,十位,百位等),
可以累积建构更高层的经验
2.先前建构的底层可以在建构高层时作为模块来使用
所以最後写得比较冗长...
Paul Graham 的说法可见:
http://www.paulgraham.com/hundred.html
http://www.paulgraham.com/avg.html
我的理解是, 令人炫目的复杂度可由一层层底层支撑高层来获得
每往上一层都有一些部件被模组化, 提供更高阶的抽象, 从而获得更强大的能力
complexity atom
蚁群对环境的适应力 --> 个别蚂蚁
大脑与认知世界的同构 --> 神经元
高阶语言 --> 机器语言指令集
cPython --> C
※ 引述《darkgerm (黑骏)》之铭言:
: ※ 引述《beatitude (...)》之铭言:
: : 前阵子看到一个公司的面试考题: http://i.imgur.com/ETTIS.png
: : 觉得有趣就做了一下
: : 看到比较抽象的函数建构在比较基础的函数之上,
: : 最终得到可以翻译任意长度数字的能力,
: : 还满震惊的...
: : 让我想到 Paul Graham 谈论 Lisp 时的论点
: 什麽论点?可以分享一下吗>"<
: 另外你的程式有小 bug 喔
: input output
: 0 ''
: 100 'one hundred, '
: 我自己也写了一个版本,希望大家也可以帮我测测看有没有 bug
: 也希望能抛砖引玉,看到其他更多更好的做法
: number = ['','one','two','three','four','five',
: 'six','seven','eight','nine','ten',
: 'eleven','twelve','thirteen','fourteen','fifteen',
: 'sixteen','seventeen','eighteen','nineteen']
: number10 = ['','ten','twenty','thirty','forty','fifty',
: 'sixty','seventy','eighty','ninty']
: def convert(n): # -> string
: if(n==0): return 'zero'
: result = ''
: if n>=1000:
: result += convert(n/1000) + ' thousand, '
: n %= 1000
: if n>=100:
: result += number[n/100] + ' hundred, '
: n %= 100
: if n>=20:
: result += number10[n/10] + ' '
: n %= 10
: result += number[n]
: # delete the trail ',' and ' '
: if result[-1] == ' ':
: result = result[:-1]
: if result[-1] == ',':
: result = result[:-1]
: return result
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.251.153.93