鱼C论坛

 找回密码
 立即注册
楼主: zltzlt

[已解决]Python:每日一题 332

[复制链接]
 楼主| 发表于 2020-2-14 19:28:25 | 显示全部楼层

可以了

28 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 20:24:32 | 显示全部楼层
认真看老爷们答题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 20:54:34 | 显示全部楼层
本帖最后由 TJBEST 于 2020-2-14 21:03 编辑

这题考验的是耐心和英文基本功
  1. def fun332(num):
  2.     def get3Num(string):
  3.         num = int(string)
  4.         if num == 0:
  5.             return ''
  6.         elif num>0 and num <10:
  7.             return Dec[num]
  8.         elif num>9 and num <20:
  9.             return Teen[num - 10]
  10.         elif num >19 and num <100:
  11.             temp = TenTimes[num//10] + ' ' + get3Num(string[-1])
  12.             if temp[-1] == ' ':
  13.                 return temp[:-1]
  14.             else:
  15.                 return temp
  16.         else:
  17.             temp =  Dec[num//100] + ' '+ 'Hundred' + ' '+ get3Num(string[1:])
  18.             if temp[-1] == ' ':
  19.                 return temp[:-1]
  20.             else:
  21.                 return temp
  22.    
  23.     if num == 0:
  24.         return 'Zero'
  25.    
  26.     Dec = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine']
  27.     Teen = ['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
  28.     TenTimes = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
  29.     Others = ['','Thousand','Million','Billion']

  30.     string = str(num)
  31.     M = len(string)
  32.    
  33.     seg = (M - 1) // 3
  34.     op = M - seg * 3
  35.    
  36.     result = get3Num(string[0:op]) + ' ' + Others[seg]
  37.     for index in range(0,seg):
  38.         temp = get3Num(string[(op + 3* index):(op + 3*index + 3)])
  39.         if temp != '':
  40.             result += ' ' + temp + ' ' + Others[seg - 1 - index]
  41.         else:
  42.             pass
  43.     if result[-1] == ' ':
  44.         return result[:-1]
  45.     else:
  46.         return result
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-14 21:56:22 | 显示全部楼层
TJBEST 发表于 2020-2-14 20:54
这题考验的是耐心和英文基本功

36 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 22:37:23 | 显示全部楼层

那么,百度翻译?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 23:57:45 | 显示全部楼层
我就是要加 And 你来咬我呀~~
  1. dic = {2:['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'],
  2.        3:['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'],
  3.        1:['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
  4.         'Seventeen','Eighteen','Nineteen'],
  5.        0:['','Thousand','Million','Billion']}
  6. def solve(n):
  7.     num = [int(x)for x in str(n)]
  8.     temp = [num[(i-3 if i-3>=0 else 0):i] for i in range(len(num),0,-3)]
  9.     res = ''
  10.     for i in range(len(temp)):
  11.         while len(temp[i])<3:
  12.             temp[i].insert(0,0)
  13.         trs = [dic[3][temp[i][0]]]
  14.         if temp[i][1] == 1:
  15.             trs.append(dic[1][temp[i][2]])
  16.         else:
  17.             trs.extend([dic[2][temp[i][1]],dic[3][temp[i][2]]])
  18.         if trs[0]:
  19.             if trs[1] or trs[2]:
  20.                 trs.insert(1,'And')
  21.             trs.insert(1,'Hundred')
  22.         elif (not i) and (trs[1] or trs[2]):
  23.             trs.insert(1,'And')
  24.         while '' in trs:
  25.             trs.remove('')
  26.         if trs:trs.append(dic[0][i])
  27.         if res:
  28.             res = ' '.join(trs)+' '+res
  29.         else:
  30.             res = ' '.join(trs)
  31.     return res if res else 'Zero'
  32. if __name__ == '__main__':
  33.     print('示例1 输出:',repr(solve(123)))
  34.     print('示例2 输出:',repr(solve(12345)))
  35.     print('示例3 输出:',repr(solve(1234567)))
  36.     print('示例4 输出:',repr(solve(1234567891)))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 23:58:54 | 显示全部楼层
我的天
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 23:59:36 | 显示全部楼层
首先把它差分程一个个数字,然后再一一对应,我的天,太难做了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 00:00:33 | 显示全部楼层
TJBEST 发表于 2020-2-14 18:20
楼主 你这个不对举例

实例1:

示例3的小横线补上口牙!
百位的后面不是还要加 And 么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 06:31:47 | 显示全部楼层
import locale;

NUMBER_CONSTANT = {0:"Zero ", 1:"One", 2:"Two", 3:"Three", 4:"Four", 5:"Five", 6:"Six", 7:"Seven",
                8:"Eight", 9:"Nine", 10:"Ten", 11:"Eleven", 12:"Twelve", 13:"Thirteen",
                14:"Fourteen", 15:"Fifteen", 16:"Sixteen", 17:"Seventeen", 18:"Eighteen", 19:"Nineteen" };
IN_HUNDRED_CONSTANT = {2:"Twenty", 3:"Thirty", 4:"Forty", 5:"Fifty", 6:"Sixty", 7:"Seventy", 8:"Eighty", 9:"Ninety"}
BASE_CONSTANT = {0:" ", 1:"Hundred", 2:"Thousand", 3:"Million", 4:"Billion"};
def F332(number):
    if str(number).isnumeric():
        if str(number)[0] == '0' and len(str(number)) > 1:
            return translateNumberToEnglish(int(number[1:]));
        if int(number) < 20:
            return NUMBER_CONSTANT[int(number)];
        elif int(number) < 100:
            if str(number)[1] == '0':
                return IN_HUNDRED_CONSTANT[int(str(number)[0])];
            else:
                return IN_HUNDRED_CONSTANT[int(str(number)[0])] + " " + NUMBER_CONSTANT[int(str(number)[1])];
        else:
            locale.setlocale(locale.LC_ALL, "English_United States.1252");
            strNumber = locale.format_string("%d"    , number, grouping=True);
            numberArray = str(strNumber).split(",");
            stringResult = "";
            groupCount = len(numberArray) + 1;
            for groupNumber in numberArray:
                if groupCount > 1 and groupNumber[0:] != "000":
                    stringResult += str(f(str(groupNumber))) + " ";
                else:
                    break;
                groupCount -= 1;
                if groupCount > 1:
                    stringResult += BASE_CONSTANT[groupCount] + " ";
            endPoint = len(stringResult) - len(" hundred");
            return stringResult;
               
    else:
        print("please input a number!");

def f(number):
    if str(number).isnumeric() and len(number) < 4:
        if len(number) < 3:
            return F332(int(number));
        elif len(number) == 3 and number[0:] == "000":
            return " ";
        elif len(number) == 3 and number[1:] == "00":
            return NUMBER_CONSTANT[int(number[0])] + "  " + BASE_CONSTANT[1];
        else:   
            return NUMBER_CONSTANT[int(number[0])] + "  " + BASE_CONSTANT[1] + "  " + F332((number[1:]));
    else:
        print("number must below 1000");
print(F332(123))
print(F332(12345))
print(F332(1234567))
print(F332(1234567891))
print(F332(2147483648))

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 09:27:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 09:51:52 | 显示全部楼层
没有含小数吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 12:48:37 | 显示全部楼层
  1. num = input('请输入数字进行翻译:')

  2. while num:
  3.     if num.isdigit == False:
  4.         print('输入错误!请重新输入:')
  5.     if int(num) >= 2147483648 or int(num) < 0:
  6.         print('输入错误!请重新输入:')
  7.     else:
  8.         break

  9. n_list = list(str(num))

  10. def read_1(n_list):
  11.     dict1 = {
  12.         '1' : 'One',
  13.         '2' : 'Two',
  14.         '3' : 'Three',
  15.         '4' : 'Four',
  16.         '5' : 'Five',
  17.         '6' : 'Six',
  18.         '7' : 'Seven',
  19.         '8' : 'Eight',
  20.         '9' : 'Nine'
  21.         }
  22.    
  23.     n1 = n_list[-1]
  24.     return dict1[n1]

  25. def read_2(n_list):
  26.     dict2 = {
  27.         '1' : 'Ten',
  28.         '11' : 'Eleven',
  29.         '12' : 'Twelve',
  30.         '13' : 'Thirteen',
  31.         '14' : 'Fourteen',
  32.         '15' : 'Fifteen',
  33.         '16' : 'Sixteen',
  34.         '17' : 'Seventeen',
  35.         '18' : 'Eighteen',
  36.         '19' : 'Nineteen',
  37.         '2' : 'Twenty',
  38.         '3' : 'Thirty',
  39.         '4' : 'Forty',
  40.         '5' : 'Fifty',
  41.         '6' : 'Sixty',
  42.         '7' : 'Seventy',
  43.         '8' : 'Sighty',
  44.         '9' : 'Ninety'
  45.     }
  46.     n1 = n_list[-1]
  47.     n2 = n_list[-2]
  48.     number = n2 + n1
  49.     if number in dict2 == True:
  50.         result = dict2[number]
  51.     else:
  52.         result = dict2[n2] + ' ' + read_1(n1)
  53.     return result
  54.    
  55. def read_3(n_list):
  56.     n3 = n_list[-3]
  57.     return read_1(n3) + ' Hundred ' + read_2(n_list[-2] + n_list[-1])

  58. def read(n_list):
  59.     l = len(n_list)
  60.     num3 = n_list[l-3:]
  61.     num6 = n_list[l-6:l-3]
  62.     num9 = n_list[l-9:l-6]
  63.     t = 'Thousand'
  64.     m = 'Millon'
  65.     b = 'Billon'
  66.     if l == 1:
  67.         if '0' in n_list:
  68.             return '0'
  69.         else:
  70.             return read_1(n_list)
  71.     elif l == 2:
  72.         return read_2(n_list)
  73.     elif l == 3:
  74.         return read_3(n_list)
  75.     elif l == 4:
  76.         num3 = n_list[l-3:]
  77.         return read_1(n_list[0]) +' '+ t + ' '+ read_3(num3)
  78.     elif l == 5:
  79.         return read_2(n_list[:2]) + ' '+ t + ' '+ read_3(num3)
  80.     elif l == 6:
  81.         return read_3(num6) + ' '+ t + ' '+ read_3(num3)
  82.     elif l == 7:
  83.         return read_1(n_list[0]) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
  84.     elif l == 8:
  85.         return read_2(n_list[:2]) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
  86.     elif l == 9:
  87.         return read_3(num9) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
  88.     elif l == 10:
  89.         return read_1(n_list[0]) + ' '+ b + ' '+ read_3(num9) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)

  90. english = read(n_list)
  91. print('翻译结果是:')
  92. print(english)
复制代码
楼主看这个行不行

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 15:09:05 | 显示全部楼层

百度可以哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 16:51:39 | 显示全部楼层
做一个叫'单位'的列表,在做一个叫'数字'的列表,接着做一个函数定义确认不就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-15 16:55:34 | 显示全部楼层
阴阳神万物主 发表于 2020-2-14 23:57
我就是要加 And 你来咬我呀~~

输入 1000010,多了一个空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-15 16:56:23 | 显示全部楼层
塔利班 发表于 2020-2-14 18:47
101怎么整One Hundred And One ?要加And的么

不需要,加也行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-15 16:58:20 | 显示全部楼层
ouyunfu 发表于 2020-2-15 06:31
import locale;

NUMBER_CONSTANT = {0:"Zero ", 1:"One", 2:"Two", 3:"Three", 4:"Four", 5:"Five", 6: ...

locale.Error: unsupported locale setting
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-15 17:01:29 | 显示全部楼层
白咕咕 发表于 2020-2-15 12:48
楼主看这个行不行

输入 12345,结果有误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 18:41:07 | 显示全部楼层
zltzlt 发表于 2020-2-15 16:55
输入 1000010,多了一个空格

这样应该就没有多的空格了。
  1. dic = {2:['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'],
  2.        3:['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'],
  3.        1:['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
  4.         'Seventeen','Eighteen','Nineteen'],
  5.        0:['','Thousand','Million','Billion']}
  6. def solve(n):
  7.     num = [int(x)for x in str(n)]
  8.     temp = [num[(i-3 if i-3>=0 else 0):i] for i in range(len(num),0,-3)]
  9.     res = ''
  10.     for i in range(len(temp)):
  11.         while len(temp[i])<3:
  12.             temp[i].insert(0,0)
  13.         trs = [dic[3][temp[i][0]]]
  14.         if temp[i][1] == 1:
  15.             trs.append(dic[1][temp[i][2]])
  16.         else:
  17.             trs.extend([dic[2][temp[i][1]],dic[3][temp[i][2]]])
  18.         if trs[0]:
  19.             if trs[1] or trs[2]:
  20.                 trs.insert(1,'And')
  21.             trs.insert(1,'Hundred')
  22.         elif (not i) and (trs[1] or trs[2]):
  23.             trs.insert(1,'And')
  24.         while '' in trs:
  25.             trs.remove('')
  26.         if trs:
  27.             if i:trs.append(dic[0][i])
  28.             if res:
  29.                 res = ' '.join(trs)+' '+res
  30.             else:
  31.                 res = ' '.join(trs)
  32.     return res if res else 'Zero'
  33. if __name__ == '__main__':
  34.     print('示例1 输出:',repr(solve(123)))
  35.     print('示例2 输出:',repr(solve(12345)))
  36.     print('示例3 输出:',repr(solve(1234567)))
  37.     print('示例4 输出:',repr(solve(1234567891)))
  38.     print('之前多空格的 输出:',repr(solve(1000010)))
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 00:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表