鱼C论坛

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

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

[复制链接]
 楼主| 发表于 2020-2-15 18:43:15 | 显示全部楼层
阴阳神万物主 发表于 2020-2-15 18:41
这样应该就没有多的空格了。

可以了,38 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 18:45:36 | 显示全部楼层
这道题如果有第三方库做这个的就比较好了,不行的话爬虫是最好的解决办法,不仅数字,其他文字都可以翻译
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 21:15:25 | 显示全部楼层
本帖最后由 Windypper 于 2020-2-15 21:23 编辑
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def input_num():
  4.     try:
  5.         num = int(input('Please input a positive integer: '))
  6.         if num < 1e15:
  7.             return num
  8.         else:
  9.             print('Integer must be less than 1E+15 and greater than 0')
  10.             input_num()
  11.     except ValueError:
  12.         print('Input Error\n\n')
  13.         input_num()


  14. cardinal = {0: '', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six',
  15.             7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve',
  16.             13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen',
  17.             18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty',
  18.             50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety',
  19.             }
  20. thousands = {1: '', 2: 'Thousand', 3: 'Million', 4: 'Billion', 5: 'Trillion'}


  21. def num_eng(num, loop=0):
  22.     num_text = ''
  23.     loop += 1
  24.     if num:
  25.         tens = num % 100  # extract tens digit and unit digit
  26.         if tens <= 20:  # the situation tens number less than 20
  27.             tens_text = cardinal[tens]
  28.         else:  # the situation tens number greater than 20
  29.             ten_digit = cardinal[tens - tens % 10]  # extract tens digit
  30.             unit_digit = cardinal[tens % 10]  # extract unit digit
  31.             if ten_digit and unit_digit:  # if both tens digit and unit digit are non-zero, add a hyphen between
  32.                 tens_text = f"{ten_digit}-{unit_digit}"
  33.             else:
  34.                 tens_text = f"{ten_digit}{unit_digit}"
  35.         if tens and loop > 1:  # if the number is greater than one thousand, add a space after unit digit
  36.             tens_text += ' '

  37.         hundreds = num // 100 % 10  # extract hundred digit
  38.         if not hundreds:  # if hundreds digit is zero, add nothing to the string
  39.             hundreds_text = ''
  40.         else:
  41.             hundreds_text = f'{cardinal[hundreds]} hundred '

  42.         # make string to be titlecased
  43.         tens_text = tens_text.title()
  44.         hundreds_text = hundreds_text.title()

  45.         # add 'and' before tens digit in two situations:
  46.         # 1. both tens digit and hundreds digits are non-zero
  47.         # 2. the last tens digit when the number is greater than one thousand
  48.         if tens and hundreds or num // 1000 and tens and loop == 1:
  49.             hundreds_text += 'and '
  50.         # add 'and' before hundreds digit if it is the last one
  51.         if num // 1000 and hundreds and not tens and loop == 1:
  52.             hundreds_text = 'and ' + hundreds_text

  53.         # recurse to higher magnitude
  54.         num_text = num_eng(num // 1000, loop)
  55.         return num_text + hundreds_text + tens_text + thousands[loop] + ' '

  56.     else:
  57.         if loop == 1:
  58.             num_text = 'Zero'
  59.         return num_text

  60. if __name__ == '__main__':
  61.     num = input_num()
  62.     print(num_eng(num))
复制代码


不是應該加上and嗎
試了下我這個好像沒問題

评分

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

查看全部评分

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

使用道具 举报

 楼主| 发表于 2020-2-15 21:22:05 | 显示全部楼层
Windypper 发表于 2020-2-15 21:15
不是應該加上and嗎
試了下我這個好像沒問題

解答错误

输入:1000000
输出:One Million Thousand
预期结果:One Million
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 21:28:47 | 显示全部楼层
本帖最后由 Windypper 于 2020-2-15 21:34 编辑
zltzlt 发表于 2020-2-15 21:22
解答错误

输入:1000000

  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def input_num():
  4.     try:
  5.         num = int(input('Please input a non-negative integer: '))
  6.         if 0 <= num < 1e15:
  7.             return num
  8.         else:
  9.             print('Integer must be non-negative and less than 1E+15')
  10.             input_num()
  11.     except ValueError:
  12.         print('Input Error\n\n')
  13.         input_num()


  14. cardinal = {0: '', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six',
  15.             7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve',
  16.             13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen',
  17.             18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty',
  18.             50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety',
  19.             }
  20. thousands = {1: '', 2: 'Thousand', 3: 'Million', 4: 'Billion', 5: 'Trillion'}


  21. def num_eng(num, loop=0):
  22.     num_text = ''
  23.     loop += 1
  24.     if num:
  25.         tens = num % 100  # extract tens digit and unit digit
  26.         if tens <= 20:  # the situation tens number less than 20
  27.             tens_text = cardinal[tens]
  28.         else:  # the situation tens number greater than 20
  29.             ten_digit = cardinal[tens - tens % 10]  # extract tens digit
  30.             unit_digit = cardinal[tens % 10]  # extract unit digit
  31.             if ten_digit and unit_digit:  # if both tens digit and unit digit are non-zero, add a hyphen between
  32.                 tens_text = f"{ten_digit}-{unit_digit}"
  33.             else:
  34.                 tens_text = f"{ten_digit}{unit_digit}"
  35.         if tens and loop > 1:  # if the number is greater than one thousand, add a space after unit digit
  36.             tens_text += ' '

  37.         hundreds = num // 100 % 10  # extract hundred digit
  38.         if not hundreds:  # if hundreds digit is zero, add nothing to the string
  39.             hundreds_text = ''
  40.         else:
  41.             hundreds_text = f'{cardinal[hundreds]} hundred '

  42.         # make string to be titlecased
  43.         tens_text = tens_text.title()
  44.         hundreds_text = hundreds_text.title()

  45.         # add 'and' before tens digit in two situations:
  46.         # 1. both tens digit and hundreds digits are non-zero
  47.         # 2. the last tens digit when the number is greater than one thousand
  48.         if tens and hundreds or num // 1000 and tens and loop == 1:
  49.             hundreds_text += 'and '
  50.         # add 'and' before hundreds digit if it is the last one
  51.         if num // 1000 and hundreds and not tens and loop == 1:
  52.             hundreds_text = 'and ' + hundreds_text

  53.         # recurse to higher magnitude
  54.         num_text = num_eng(num // 1000, loop)
  55.         if tens or hundreds:
  56.             return num_text + hundreds_text + tens_text + thousands[loop] + ' '
  57.         else:
  58.             return num_text

  59.     else:
  60.         if loop == 1:
  61.             num_text = 'Zero'
  62.         return num_text


  63. if __name__ == '__main__':
  64.     num = input_num()
  65.     print(num_eng(num))
复制代码


修改了下低級錯誤……

评分

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

查看全部评分

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

使用道具 举报

 楼主| 发表于 2020-2-15 21:30:13 | 显示全部楼层
Windypper 发表于 2020-2-15 21:28
修改了下低級錯誤……

可以了,35 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-16 00:08:07 | 显示全部楼层
本帖最后由 kinkon 于 2020-2-16 22:59 编辑

加上美元美分单位,如果不符合要求再改掉

  1. def parseFirst(s):
  2.     a = [ "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" ]
  3.     return a[int(s)]
  4. def parseTeen(s):
  5.     a = [ "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen","Seventeen", "Eighteen", "Nineteen" ]
  6.     return a[int(s) - 10]
  7. def parseTen(s):
  8.     a = [ "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" ]
  9.     return a[int(s[0:1]) - 1]
  10. def transTwo(s):
  11.         value = ""
  12.         if (len(s) > 2):
  13.                 s = s[0:2]
  14.         elif (len(s) < 2):
  15.                 s = s + "0"
  16.         if (int(s) < 10):
  17.                 value = parseFirst(s)
  18.         elif (int(s) < 20):
  19.                 value = parseTeen(s)
  20.         elif (int(s) % 10 == 0) and (int(s) < 100):
  21.                 value = parseTen(s)
  22.         else:
  23.                 value = parseTen(s[0:1]) + "-" + parseFirst(s[1:])
  24.         return value

  25. def parseMore(s):
  26.         a = [ "", "Thousand", "Million", "Billion" ]
  27.         return a[s]
  28. def transThree(s):
  29.         value = ""
  30.         if (int(s) < 100):
  31.                 value = transTwo(s[1:])
  32.         elif (int(s) % 100 == 0):
  33.                 value = parseFirst(s[0:1]) + " Hundred"
  34.         else:
  35.                 value = parseFirst(s[0:1]) + " Hundred And " + transTwo(s[1:])
  36.         return value
  37.        
  38. def f332(x):
  39.         if(x <= 0):
  40.                 return "Zero Cents Only"
  41.         x = str(x)
  42.         z = x.find(".")
  43.         lstr = ""
  44.         rstr = ""
  45.         if (z > -1):
  46.                 lstr = x[0:z][::-1]
  47.                 rstr = x[z+1:]
  48.         else:
  49.                 lstr = x[::-1]
  50.         if (len(lstr) % 3) == 1:
  51.                 lstr += "00"
  52.         elif (len(lstr) % 3) == 2:
  53.                 lstr += "0"
  54.         lm = ""
  55.         a = [""]*(len(lstr)//3)
  56.         for i in range(len(lstr)//3):
  57.                 a[i] = lstr[3 * i: 3 * i + 3][::-1]
  58.                 if (a[i] != "000"):
  59.                         if (i != 0) and lm:
  60.                                 lm = transThree(a[i]) + " " + parseMore(i) + " " + lm
  61.                         elif (i != 0):
  62.                             lm = transThree(a[i]) + " " + parseMore(i)
  63.                         else:
  64.                                 lm = transThree(a[i])

  65.         xs = ""
  66.         if (z > -1):
  67.                 trans = transTwo(rstr)
  68.                 if(trans == ""):
  69.                         xs = ""
  70.                 else:
  71.                         xs = "And " + trans + " Cents "
  72.         return lm + " " + xs + "Only"

  73. print(f332(1000000))
  74. print(f332(214748364890))
  75. print(f332(23.32))
复制代码

评分

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

查看全部评分

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

使用道具 举报

头像被屏蔽
发表于 2020-2-16 06:36:26 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-16 08:19:09 | 显示全部楼层
萌新刚来,先看看大佬答题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-16 12:21:11 | 显示全部楼层
本帖最后由 白咕咕 于 2020-2-16 12:25 编辑
  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.         '11' : 'Eleven',
  28.         '12' : 'Twelve',
  29.         '13' : 'Thirteen',
  30.         '14' : 'Fourteen',
  31.         '15' : 'Fifteen',
  32.         '16' : 'Sixteen',
  33.         '17' : 'Seventeen',
  34.         '18' : 'Eighteen',
  35.         '19' : 'Nineteen',
  36.     }
  37.     dict22 = {
  38.         '0' : '',
  39.         '1' : 'Ten',
  40.         '2' : 'Twenty',
  41.         '3' : 'Thirty',
  42.         '4' : 'Forty',
  43.         '5' : 'Fifty',
  44.         '6' : 'Sixty',
  45.         '7' : 'Seventy',
  46.         '8' : 'Eighty',
  47.         '9' : 'Ninety'
  48.     }
  49.     number = n_list
  50.     n1 = n_list[-1]
  51.     n2 = n_list[-2]
  52.     if n2 == '1':
  53.         result = dict2[number]
  54.     else:
  55.         result = dict22[n2] + ' ' + read_1(n1)
  56.     return result
  57.    
  58. def read_3(n_list):
  59.     n3 = n_list[-3]
  60.     return read_1(n3) + ' Hundred ' + read_2(n_list[-2] + n_list[-1])

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

  96. english = read(n_list)
  97. print('翻译结果是:')
  98. print(english)
复制代码
楼主这样应该可以了


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

使用道具 举报

发表于 2020-2-16 20:42:55 | 显示全部楼层
_2_ 发表于 2020-2-15 09:56
101 -> One Zero One
是这样吗? @zltzlt

@zltzlt 请删掉 34#
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-16 20:44:17 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-16 21:02:26 | 显示全部楼层
kinkon 发表于 2020-2-16 00:08
加上美元美分单位,如果不符合要求再改掉

单词拼写错误,Tweleve
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-16 21:04:02 | 显示全部楼层
840613937 发表于 2020-2-16 06:36
虽然是几天前的题了,但是没办法,刚知道这里还有题做的,哈哈哈
花了我一个晚上才做好,我太难了

解答错误

输入:1000000
输出:"One Million Thousand"
预期结果:"One Million"
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-16 21:07:04 | 显示全部楼层
白咕咕 发表于 2020-2-16 12:21
楼主这样应该可以了

Million

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

使用道具 举报

头像被屏蔽
发表于 2020-2-16 21:46:54 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-16 21:48:20 | 显示全部楼层

可以了,40 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2020-2-16 21:50:19 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-16 23:02:24 | 显示全部楼层
zltzlt 发表于 2020-2-16 21:02
单词拼写错误,Tweleve

改好了,我的英文是数学老师教的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-18 11:26:36 | 显示全部楼层
好难,终于做出来了,不确定要不要加and,于是按照网上的说法,在百倍与十位之间加了and
  1. def fun332(num):
  2.     if num >= 0 and num < 2147483648:
  3.         unit = ['Hundred ', ' Thousand ', ' Million ', ' Billion ']
  4.         numbers = {0: 'Zero ', 1: 'One ', 2: 'Two ', 3: 'Three ', 4: 'Four ', 5: 'Five ',
  5.         6: 'Six ', 7: 'Seven ', 8: 'Eight ', 9: 'Nine ', 10: 'Ten ', 11: 'Eleven ',
  6.         12: 'Twelve ', 13: 'Thirteen ', 14: 'Fourteen ', 15: 'Fifteen ', 16: 'Sixteen ',
  7.         17: 'Seventeen ', 18: 'Eighteen ', 19: 'Nineteen ', 20: 'Twenty ', 30: 'Thirty ',
  8.         40: 'Fourty ', 50: 'Fifty ', 60: 'Sixty ', 70: 'Seventy ', 80: 'Eighty ', 90: 'Ninety '}

  9.         list1 = [int(n) for n in str(num)]
  10.         list1.reverse()
  11.         l = len(list1)
  12.         gn = l//3+1 if l%3 else l//3
  13.         glist = []
  14.         for i in range(gn):
  15.             glist.append(list1[i*3:i*3+3])
  16.         temp = []
  17.         for gl in glist:
  18.             temp.append(num2en(gl, numbers, unit))
  19.         result = ''
  20.         for i in range((len(temp)-1), 0, -1):
  21.             if temp[i]:
  22.                 result += temp[i] + unit[i]
  23.             else:
  24.                 result += temp[i]
  25.         return result + temp[0]
  26.     else:
  27.         print("请输入小于2147483648的正整数!")
  28.         return None
  29.         

  30. def num2en(gl, numbers, unit):
  31.     result = ''
  32.     if len(gl) == 1:
  33.         result += numbers[gl[0]]
  34.     elif len(gl) == 2:
  35.         result = num2en2(gl, numbers)
  36.     else:
  37.         n2 = num2en2(gl[:2], numbers)
  38.         if gl[2]:
  39.             if n2:
  40.                 result = numbers[gl[2]]+unit[0]+'And '+n2
  41.             else:
  42.                 result = numbers[gl[2]]+unit[0]
  43.         else:
  44.             result = 'And '+ n2 if n2 else ''
  45.             
  46.     return result.strip()


  47. def num2en2(gl2, numbers):
  48.     result = ''
  49.     if gl2[0] == 0:
  50.         result = numbers[gl2[1]*10]
  51.     else:
  52.         if gl2[1] == 1:
  53.             result = numbers[gl2[1]*10 + gl2[0]]
  54.         else:
  55.             result = numbers[gl2[1]*10] + numbers[gl2[0]]
  56.     if 'Zero' in result:
  57.             result = result.replace('Zero', '')
  58.     return result.strip()


  59. print(fun332(1400000))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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