zltzlt
发表于 2020-2-15 18:43:15
阴阳神万物主 发表于 2020-2-15 18:41
这样应该就没有多的空格了。
可以了,38 ms
fan1993423
发表于 2020-2-15 18:45:36
这道题如果有第三方库做这个的就比较好了,不行的话爬虫是最好的解决办法,不仅数字,其他文字都可以翻译
Windypper
发表于 2020-2-15 21:15:25
本帖最后由 Windypper 于 2020-2-15 21:23 编辑
# !/usr/bin/env python
# -*- coding: utf-8 -*-
def input_num():
try:
num = int(input('Please input a positive integer: '))
if num < 1e15:
return num
else:
print('Integer must be less than 1E+15 and greater than 0')
input_num()
except ValueError:
print('Input Error\n\n')
input_num()
cardinal = {0: '', 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', 20: 'twenty', 30: 'thirty', 40: 'forty',
50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety',
}
thousands = {1: '', 2: 'Thousand', 3: 'Million', 4: 'Billion', 5: 'Trillion'}
def num_eng(num, loop=0):
num_text = ''
loop += 1
if num:
tens = num % 100# extract tens digit and unit digit
if tens <= 20:# the situation tens number less than 20
tens_text = cardinal
else:# the situation tens number greater than 20
ten_digit = cardinal# extract tens digit
unit_digit = cardinal# extract unit digit
if ten_digit and unit_digit:# if both tens digit and unit digit are non-zero, add a hyphen between
tens_text = f"{ten_digit}-{unit_digit}"
else:
tens_text = f"{ten_digit}{unit_digit}"
if tens and loop > 1:# if the number is greater than one thousand, add a space after unit digit
tens_text += ' '
hundreds = num // 100 % 10# extract hundred digit
if not hundreds:# if hundreds digit is zero, add nothing to the string
hundreds_text = ''
else:
hundreds_text = f'{cardinal} hundred '
# make string to be titlecased
tens_text = tens_text.title()
hundreds_text = hundreds_text.title()
# add 'and' before tens digit in two situations:
# 1. both tens digit and hundreds digits are non-zero
# 2. the last tens digit when the number is greater than one thousand
if tens and hundreds or num // 1000 and tens and loop == 1:
hundreds_text += 'and '
# add 'and' before hundreds digit if it is the last one
if num // 1000 and hundreds and not tens and loop == 1:
hundreds_text = 'and ' + hundreds_text
# recurse to higher magnitude
num_text = num_eng(num // 1000, loop)
return num_text + hundreds_text + tens_text + thousands + ' '
else:
if loop == 1:
num_text = 'Zero'
return num_text
if __name__ == '__main__':
num = input_num()
print(num_eng(num))
不是應該加上and嗎
試了下我這個好像沒問題
zltzlt
发表于 2020-2-15 21:22:05
Windypper 发表于 2020-2-15 21:15
不是應該加上and嗎
試了下我這個好像沒問題
解答错误
输入:1000000
输出:One Million Thousand
预期结果:One Million
Windypper
发表于 2020-2-15 21:28:47
本帖最后由 Windypper 于 2020-2-15 21:34 编辑
zltzlt 发表于 2020-2-15 21:22
解答错误
输入:1000000
# !/usr/bin/env python
# -*- coding: utf-8 -*-
def input_num():
try:
num = int(input('Please input a non-negative integer: '))
if 0 <= num < 1e15:
return num
else:
print('Integer must be non-negative and less than 1E+15')
input_num()
except ValueError:
print('Input Error\n\n')
input_num()
cardinal = {0: '', 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', 20: 'twenty', 30: 'thirty', 40: 'forty',
50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety',
}
thousands = {1: '', 2: 'Thousand', 3: 'Million', 4: 'Billion', 5: 'Trillion'}
def num_eng(num, loop=0):
num_text = ''
loop += 1
if num:
tens = num % 100# extract tens digit and unit digit
if tens <= 20:# the situation tens number less than 20
tens_text = cardinal
else:# the situation tens number greater than 20
ten_digit = cardinal# extract tens digit
unit_digit = cardinal# extract unit digit
if ten_digit and unit_digit:# if both tens digit and unit digit are non-zero, add a hyphen between
tens_text = f"{ten_digit}-{unit_digit}"
else:
tens_text = f"{ten_digit}{unit_digit}"
if tens and loop > 1:# if the number is greater than one thousand, add a space after unit digit
tens_text += ' '
hundreds = num // 100 % 10# extract hundred digit
if not hundreds:# if hundreds digit is zero, add nothing to the string
hundreds_text = ''
else:
hundreds_text = f'{cardinal} hundred '
# make string to be titlecased
tens_text = tens_text.title()
hundreds_text = hundreds_text.title()
# add 'and' before tens digit in two situations:
# 1. both tens digit and hundreds digits are non-zero
# 2. the last tens digit when the number is greater than one thousand
if tens and hundreds or num // 1000 and tens and loop == 1:
hundreds_text += 'and '
# add 'and' before hundreds digit if it is the last one
if num // 1000 and hundreds and not tens and loop == 1:
hundreds_text = 'and ' + hundreds_text
# recurse to higher magnitude
num_text = num_eng(num // 1000, loop)
if tens or hundreds:
return num_text + hundreds_text + tens_text + thousands + ' '
else:
return num_text
else:
if loop == 1:
num_text = 'Zero'
return num_text
if __name__ == '__main__':
num = input_num()
print(num_eng(num))
修改了下低級錯誤……
zltzlt
发表于 2020-2-15 21:30:13
Windypper 发表于 2020-2-15 21:28
修改了下低級錯誤……
可以了,35 ms
kinkon
发表于 2020-2-16 00:08:07
本帖最后由 kinkon 于 2020-2-16 22:59 编辑
加上美元美分单位,如果不符合要求再改掉
def parseFirst(s):
a = [ "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" ]
return a
def parseTeen(s):
a = [ "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen","Seventeen", "Eighteen", "Nineteen" ]
return a
def parseTen(s):
a = [ "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" ]
return a) - 1]
def transTwo(s):
value = ""
if (len(s) > 2):
s = s
elif (len(s) < 2):
s = s + "0"
if (int(s) < 10):
value = parseFirst(s)
elif (int(s) < 20):
value = parseTeen(s)
elif (int(s) % 10 == 0) and (int(s) < 100):
value = parseTen(s)
else:
value = parseTen(s) + "-" + parseFirst(s)
return value
def parseMore(s):
a = [ "", "Thousand", "Million", "Billion" ]
return a
def transThree(s):
value = ""
if (int(s) < 100):
value = transTwo(s)
elif (int(s) % 100 == 0):
value = parseFirst(s) + " Hundred"
else:
value = parseFirst(s) + " Hundred And " + transTwo(s)
return value
def f332(x):
if(x <= 0):
return "Zero Cents Only"
x = str(x)
z = x.find(".")
lstr = ""
rstr = ""
if (z > -1):
lstr = x[::-1]
rstr = x
else:
lstr = x[::-1]
if (len(lstr) % 3) == 1:
lstr += "00"
elif (len(lstr) % 3) == 2:
lstr += "0"
lm = ""
a = [""]*(len(lstr)//3)
for i in range(len(lstr)//3):
a = lstr[::-1]
if (a != "000"):
if (i != 0) and lm:
lm = transThree(a) + " " + parseMore(i) + " " + lm
elif (i != 0):
lm = transThree(a) + " " + parseMore(i)
else:
lm = transThree(a)
xs = ""
if (z > -1):
trans = transTwo(rstr)
if(trans == ""):
xs = ""
else:
xs = "And " + trans + " Cents "
return lm + " " + xs + "Only"
print(f332(1000000))
print(f332(214748364890))
print(f332(23.32))
840613937
发表于 2020-2-16 06:36:26
Tommy010302
发表于 2020-2-16 08:19:09
萌新刚来,先看看大佬答题
白咕咕
发表于 2020-2-16 12:21:11
本帖最后由 白咕咕 于 2020-2-16 12:25 编辑
num = input('请输入数字进行翻译:')
while num:
if num.isdigit == False:
print('输入错误!请重新输入:')
if int(num) >= 2147483648 or int(num) < 0:
print('输入错误!请重新输入:')
else:
break
n_list = list(str(num))
def read_1(n_list):
dict1 = {
'1' : 'One',
'2' : 'Two',
'3' : 'Three',
'4' : 'Four',
'5' : 'Five',
'6' : 'Six',
'7' : 'Seven',
'8' : 'Eight',
'9' : 'Nine'
}
n1 = n_list[-1]
return dict1
def read_2(n_list):
dict2 = {
'11' : 'Eleven',
'12' : 'Twelve',
'13' : 'Thirteen',
'14' : 'Fourteen',
'15' : 'Fifteen',
'16' : 'Sixteen',
'17' : 'Seventeen',
'18' : 'Eighteen',
'19' : 'Nineteen',
}
dict22 = {
'0' : '',
'1' : 'Ten',
'2' : 'Twenty',
'3' : 'Thirty',
'4' : 'Forty',
'5' : 'Fifty',
'6' : 'Sixty',
'7' : 'Seventy',
'8' : 'Eighty',
'9' : 'Ninety'
}
number = n_list
n1 = n_list[-1]
n2 = n_list[-2]
if n2 == '1':
result = dict2
else:
result = dict22 + ' ' + read_1(n1)
return result
def read_3(n_list):
n3 = n_list[-3]
return read_1(n3) + ' Hundred ' + read_2(n_list[-2] + n_list[-1])
def read(n_list):
l = len(n_list)
if l >= 3:
num3 = n_list + n_list + n_list
if l >= 6:
num6 = n_list + n_list + n_list
if l >= 9:
num9 = n_list + n_list + n_list
t = 'Thousand'
m = 'Millon'
b = 'Billon'
if l == 1:
if '0' in n_list:
return '0'
else:
return read_1(n_list)
elif l == 2:
return read_2(n_list)
elif l == 3:
return read_3(n_list)
elif l == 4:
num3 = n_list
return read_1(n_list) +' '+ t + ' '+ read_3(num3)
elif l == 5:
return read_2(n_list+n_list) + ' '+ t + ' '+ read_3(num3)
elif l == 6:
return read_3(num6) + ' '+ t + ' '+ read_3(num3)
elif l == 7:
return read_1(n_list) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
elif l == 8:
return read_2(n_list+ n_list) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
elif l == 9:
return read_3(num9) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
elif l == 10:
return read_1(n_list) + ' '+ b + ' '+ read_3(num9) + ' '+ m + ' '+ read_3(num6) + ' '+ t + ' '+ read_3(num3)
english = read(n_list)
print('翻译结果是:')
print(english)楼主这样应该可以了
一个账号
发表于 2020-2-16 20:42:55
_2_ 发表于 2020-2-15 09:56
101 -> One Zero One
是这样吗? @zltzlt
@zltzlt 请删掉 34#
zltzlt
发表于 2020-2-16 20:44:17
一个账号 发表于 2020-2-16 20:42
@zltzlt 请删掉 34#
{:10_277:}
zltzlt
发表于 2020-2-16 21:02:26
kinkon 发表于 2020-2-16 00:08
加上美元美分单位,如果不符合要求再改掉
单词拼写错误,Tweleve
zltzlt
发表于 2020-2-16 21:04:02
840613937 发表于 2020-2-16 06:36
虽然是几天前的题了,但是没办法,刚知道这里还有题做的,哈哈哈
花了我一个晚上才做好,我太难了
解答错误
输入:1000000
输出:"One Million Thousand"
预期结果:"One Million"
zltzlt
发表于 2020-2-16 21:07:04
白咕咕 发表于 2020-2-16 12:21
楼主这样应该可以了
Million
840613937
发表于 2020-2-16 21:46:54
zltzlt
发表于 2020-2-16 21:48:20
840613937 发表于 2020-2-16 21:46
现在可以了吗
可以了,40 ms
840613937
发表于 2020-2-16 21:50:19
kinkon
发表于 2020-2-16 23:02:24
zltzlt 发表于 2020-2-16 21:02
单词拼写错误,Tweleve
改好了,我的英文是数学老师教的{:10_266:}
graceasyi
发表于 2020-2-18 11:26:36
好难,终于做出来了,不确定要不要加and,于是按照网上的说法,在百倍与十位之间加了and
def fun332(num):
if num >= 0 and num < 2147483648:
unit = ['Hundred ', ' Thousand ', ' Million ', ' Billion ']
numbers = {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 ', 20: 'Twenty ', 30: 'Thirty ',
40: 'Fourty ', 50: 'Fifty ', 60: 'Sixty ', 70: 'Seventy ', 80: 'Eighty ', 90: 'Ninety '}
list1 =
list1.reverse()
l = len(list1)
gn = l//3+1 if l%3 else l//3
glist = []
for i in range(gn):
glist.append(list1)
temp = []
for gl in glist:
temp.append(num2en(gl, numbers, unit))
result = ''
for i in range((len(temp)-1), 0, -1):
if temp:
result += temp + unit
else:
result += temp
return result + temp
else:
print("请输入小于2147483648的正整数!")
return None
def num2en(gl, numbers, unit):
result = ''
if len(gl) == 1:
result += numbers]
elif len(gl) == 2:
result = num2en2(gl, numbers)
else:
n2 = num2en2(gl[:2], numbers)
if gl:
if n2:
result = numbers]+unit+'And '+n2
else:
result = numbers]+unit
else:
result = 'And '+ n2 if n2 else ''
return result.strip()
def num2en2(gl2, numbers):
result = ''
if gl2 == 0:
result = numbers*10]
else:
if gl2 == 1:
result = numbers*10 + gl2]
else:
result = numbers*10] + numbers]
if 'Zero' in result:
result = result.replace('Zero', '')
return result.strip()
print(fun332(1400000))