【机试题(实现语言:python3)】学英语-递归或字符串

题目描述
Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:

如22:twenty two,123:one hundred and twenty three。

说明:

数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;

输出格式为twenty two;

非法数据请返回“error”;

关键字提示:and,billion,million,thousand,hundred。

本题含有多组输入数据。

输入描述:

输入一个long型整数

输出描述:

输出相应的英文写法

示例1
输入

2356

输出

two thousand three hundred and fifty six

递归写法:

def num_to_words(num):
    to19 = 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen seventeen eighteen nineteen'.split()
    tens = 'twenty thirty forty fifty sixty seventy eighty ninety'.split()
 
    def words(n):
        if n < 20: return to19[n - 1:n]
        if n < 100: return [tens[n // 10 - 2]] + words(n % 10)
        if n < 1000:
            return [to19[n // 100 - 1]] + ['hundred'] + ['and'] + words(n % 100)
        for p, w in enumerate(('thousand', 'million', 'billion'), 1):
            if n < 1000 ** (p + 1):
                return words(n // 1000 ** p) + [w] + words(n % 1000 ** p)
 
    return " ".join(words(num)) or 'Zero'
 
 
while True:
    try:
        num = int(input())
        print(num_to_words(num))
    except:
        break

遍历写法:

def func():
    while True:
        try:
            input1 = input()
            num1 = ['zero','one','two','three','four','five','six',
                   'seven','eight','nine','ten','eleven','twelve',
                   'thirteen','fourteen','fifteen','sixteen',
                   'seventeen','eithteen','nineteen']
            num2 = ['twenty','thirty','forty','fifty','sixty','seventy',
                   'eighty','ninety']
            num3 = ['hundred','thousand','million','billion','and']
            len1 = len(input1)
            #print(not input1.isdigit())
            if len1>9 or not input1.isdigit():
                print("error")
                break
                
            str1 = input1
            list1 = []
            end = len(input1)
            for i in input1:
                if end >=0:
                    start = end-3
                    if start<0:
                        start = 0
                    list1.append(str1[start:end])
                    end -=3
            list1 = list1[::-1]
            res = []
            for i in list1:
                #print("i:",i)
                val = []
                if len(i)==1:
                    val.append(num1[int(i)])
                elif len(i)==2:
                    if i[0]=='1':
                        val.append(num1[int(i)])
                    elif i[1]=='0': 
                        val.append(num2[int(i[0])-2])
                    else:
                        val.append(num2[int(i[0])-1]+num1[int(i[1])])
                elif len(i)==3:
                    v = ''
                    for k,j in enumerate(i):
                        #print("j:",j)
                        if j =='0':
                            continue
                        if k==0:
                            v = num1[int(j)]
                            val.append(v)
                            v = num3[0]
                            val.append(v)
                            v = num3[-1]
                            val.append(v)
                        elif k == 1:
                            if i[k] =='1':
                                v = num1[int(i[k:k+2])]
                                val.append(v)
                                break
                            v = num2[int(j)-2]
                            val.append(v)
                        elif k==2:
                            v = num1[int(j)]
                            val.append(v)
                if val!=[]:
                    res.append(val)   
            #print(res)
            #print(len(res))
            if len(res)==2:
                res[0].append(num3[1])
            elif len(res)==3:
                res[0].append(num3[2])
                res[1].append(num3[1])
            elif len(res)==4:
                res[0].append(num3[3])
                res[1].append(num3[2])
                res[2].append(num3[1])
            print(' '.join(map(' '.join,res)))
        except Exception as e:
            #print(e)
            break
if __name__ == '__main__':
    func()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值