案列一:阿拉伯数字【整数】翻译
思路:十亿、百万、千,分别处理。
小于一千的数字,单独使用 three 函数处理,对小于20的数字、其他两位数、三位数分别处理
class Solution:def numberToWords(self, num):to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve ' \'Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split()tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split()def helper(num):if num < 20:return to19[num - 1:num]if num < 100:return [tens[num 10 - 2]] + helper(num % 10)if num < 1000:return [to19[num 100 - 1]] + ['Hundred'] + helper(num % 100)for p, w in enumerate(['Thousand', 'Million', 'Billion'], 1):if num < 1000 ** (p + 1):return helper(num 1000 ** p) + [w] + helper(num % 1000 ** p)return ' '.join(helper(num)) or 'Zero'yc_put=int(input('请输入阿拉伯数字【非小数或负数】:',))yc_out =Solution().numberToWords(yc_put)print("{}阿拉怕数字对应英语为:{}".format(yc_put, yc_out))
案例二:单词复数
思路:1、名词由单数变复数的基本方法如下:
①在单数名词词尾加s。如:map → maps,boy→ boys,horse→ horses, table→ tables.
②s,o,x ,sh,ch结尾的词加es.如:class→classes, box→boxes, hero→heroes, dish→dishes, bench→benches.
[注]:少数以o结尾的词,变复数时只加s。如:photo→photos, piano→pianos.
③以辅音字母加y结尾的名词,变y为i,再加es。如:family→families, city→cities, party→parties.
④以f或fe结尾的名词,变f或fe为v,再加es。如:shelf→shelves, wolf→wolves, life→lives, knife→knives.
def Plural_Words(word):words = list(word)yc_21 = ['s', 'x', 'sh', 'ch']yc_31 = ['by', 'cy', 'dy', 'fy', 'gy', 'hy', 'jy', 'ky', 'ly', 'my', 'ny', 'py', 'qy', 'ry', 'sy', 'ty', 'vy', 'wy', 'xy', 'yy', 'zy']yc_41 = ['f', 'fe']yc_result1 = 'es'yc_result2 = 's'yc_test_1 =word[-1]yc_test_2 = word[-2]+word[-1]while True:if yc_test_1 in yc_21:words_01 = word + yc_result1return words_01elif yc_test_2 in yc_21:words_011 = word + yc_result1return words_011elif yc_test_2 in yc_31:words[-1] = 'i'words = ''.join(words) #字符串转化words = word.replace(' ','') #去掉空格words_31 = words + yc_result1return words_31elif yc_test_1 in yc_41:words[-1] = 'v'words = ''.join(words) #字符串转化words = word.replace(' ','') #去掉空格words_41 = words + yc_result1return words_41elif yc_test_2 in yc_41:words[-2] = 'v'words = ''.join(words) #字符串转化words = word.replace(' ','') #去掉空格words_42 = words + yc_result2return words_42else:words_05 =word + yc_result2return words_05word_yc = input("请输入英语单词的单数:")yc_out =Plural_Words(word_yc)print("*****目前还不支持特殊单词变成复数******")print("*****目前还不支持是语单词真假判断*****")print("{}单词的复数为:{}".format(word_yc, yc_out))
案例三. 整数转罗马数字
罗马数字包含以下七种字符: I
, V
, X
, L
,C
,D
和 M
。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II
,即为两个并列的 1。12 写做 XII
,即为 X
+ II
。27 写做 XXVII
, 即为 XX
+ V
+ II
。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII
,而是 IV
。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX
。这个特殊的规则只适用于以下六种情况:
I
可以放在V
(5) 和X
(10) 的左边,来表示 4 和 9。X
可以放在L
(50) 和C
(100) 的左边,来表示 40 和 90。C
可以放在D
(500) 和M
(1000) 的左边,来表示 400 和 900。
给你一个整数,将其转为罗马数字。
示例 1:
输入: num = 3
输出: "III"
示例 2:
输入: num = 4
输出: "IV"
class Solution:def intToRoman(self, num):yc ={1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC', 50:'L', 40:'XL',10:'X',9:'IX',5:'V', 4:'IV', 1:'I'}res = ''for key in yc:if num//key != 0:yc_1 = num//keyres += yc_1*yc[key]num %=keyreturn resyc_put = int(input("请输入需要转换为罗马数字的阿拉伯数字:"))yc_out = Solution().intToRoman(yc_put)print("阿拉伯数字:{} 转化成罗马数字为:{}".format(yc_put, yc_out))
代码测试结果:
请输入需要转换为罗马数字的阿拉伯数字:2564阿拉伯数字:2564 转化成罗马数字为:MMDLXIV
个人疑问:
1、Python里的字典可以用在哪些情景下
字典是一种可变容器模型,且可存储任意类型对象。
也是一种常用的数据结构,用于存放具有映射关系的数据。
字典的每个键值 key=>value 对用冒号 : 分割,每个对之间用逗号(,)分割,整个字典包括在花括号 {} 中 ,格式如下所示:
d = {key1 : value1, key2 : value2, key3 : value3 }

键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字。
一个简单的字典实例:
dict = {'name': 'runoob', 'likes': 123, 'url': 'www.runoob.com'}

也可如此创建字典:
dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }
对于初学者而言,应牢记字典包含多个 key-value 对,而 key 是字典的关键数据,因此程序对字典的操作都是基于 key 的。基本操作如下:
通过 key 访问 value 。
通过 key 添加 key-value 对。
通过 key 删除 key-value 对。
通过 key 修改 key-value 对。
通过 key 判断指定 key-value 对是否存在。
通过 key 访问 value 使用的也是方括号语法,就像前面介绍的列表和元组一样,只是此时在方括号中放的是 key,而不是列表或元组中的索引。




