zltzlt
发表于 2020-2-14 19:28:25
William4869 发表于 2020-2-14 19:27
继续
可以了{:10_275:}
28 ms
AINIDEREN
发表于 2020-2-14 20:24:32
认真看老爷们答题
TJBEST
发表于 2020-2-14 20:54:34
本帖最后由 TJBEST 于 2020-2-14 21:03 编辑
这题考验的是耐心和英文基本功{:5_109:}
def fun332(num):
def get3Num(string):
num = int(string)
if num == 0:
return ''
elif num>0 and num <10:
return Dec
elif num>9 and num <20:
return Teen
elif num >19 and num <100:
temp = TenTimes + ' ' + get3Num(string[-1])
if temp[-1] == ' ':
return temp[:-1]
else:
return temp
else:
temp =Dec + ' '+ 'Hundred' + ' '+ get3Num(string)
if temp[-1] == ' ':
return temp[:-1]
else:
return temp
if num == 0:
return 'Zero'
Dec = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine']
Teen = ['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
TenTimes = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
Others = ['','Thousand','Million','Billion']
string = str(num)
M = len(string)
seg = (M - 1) // 3
op = M - seg * 3
result = get3Num(string) + ' ' + Others
for index in range(0,seg):
temp = get3Num(string[(op + 3* index):(op + 3*index + 3)])
if temp != '':
result += ' ' + temp + ' ' + Others
else:
pass
if result[-1] == ' ':
return result[:-1]
else:
return result
zltzlt
发表于 2020-2-14 21:56:22
TJBEST 发表于 2020-2-14 20:54
这题考验的是耐心和英文基本功
36 ms{:10_256:}{:10_256:}
阴阳神万物主
发表于 2020-2-14 22:37:23
zltzlt 发表于 2020-2-14 18:41
不可行……
那么,百度翻译?
阴阳神万物主
发表于 2020-2-14 23:57:45
我就是要加 And 你来咬我呀~~
dic = {2:['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'],
3:['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'],
1:['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
'Seventeen','Eighteen','Nineteen'],
0:['','Thousand','Million','Billion']}
def solve(n):
num =
temp = for i in range(len(num),0,-3)]
res = ''
for i in range(len(temp)):
while len(temp)<3:
temp.insert(0,0)
trs = ]]
if temp == 1:
trs.append(dic])
else:
trs.extend(],dic]])
if trs:
if trs or trs:
trs.insert(1,'And')
trs.insert(1,'Hundred')
elif (not i) and (trs or trs):
trs.insert(1,'And')
while '' in trs:
trs.remove('')
if trs:trs.append(dic)
if res:
res = ' '.join(trs)+' '+res
else:
res = ' '.join(trs)
return res if res else 'Zero'
if __name__ == '__main__':
print('示例1 输出:',repr(solve(123)))
print('示例2 输出:',repr(solve(12345)))
print('示例3 输出:',repr(solve(1234567)))
print('示例4 输出:',repr(solve(1234567891)))
悲哀的猫
发表于 2020-2-14 23:58:54
我的天
悲哀的猫
发表于 2020-2-14 23:59:36
首先把它差分程一个个数字,然后再一一对应,我的天,太难做了{:10_266:}
阴阳神万物主
发表于 2020-2-15 00:00:33
TJBEST 发表于 2020-2-14 18:20
楼主 你这个不对举例
实例1:
示例3的小横线补上口牙!
百位的后面不是还要加 And 么?
ouyunfu
发表于 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' and len(str(number)) > 1:
return translateNumberToEnglish(int(number));
if int(number) < 20:
return NUMBER_CONSTANT;
elif int(number) < 100:
if str(number) == '0':
return IN_HUNDRED_CONSTANT)];
else:
return IN_HUNDRED_CONSTANT)] + " " + NUMBER_CONSTANT)];
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 != "000":
stringResult += str(f(str(groupNumber))) + " ";
else:
break;
groupCount -= 1;
if groupCount > 1:
stringResult += BASE_CONSTANT + " ";
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 == "000":
return " ";
elif len(number) == 3 and number == "00":
return NUMBER_CONSTANT)] + "" + BASE_CONSTANT;
else:
return NUMBER_CONSTANT)] + "" + BASE_CONSTANT + "" + F332((number));
else:
print("number must below 1000");
print(F332(123))
print(F332(12345))
print(F332(1234567))
print(F332(1234567891))
print(F332(2147483648))
nl2020
发表于 2020-2-15 09:27:14
{:10_266:}
kinkon
发表于 2020-2-15 09:51:52
没有含小数吗?
白咕咕
发表于 2020-2-15 12:48:37
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 = {
'1' : 'Ten',
'11' : 'Eleven',
'12' : 'Twelve',
'13' : 'Thirteen',
'14' : 'Fourteen',
'15' : 'Fifteen',
'16' : 'Sixteen',
'17' : 'Seventeen',
'18' : 'Eighteen',
'19' : 'Nineteen',
'2' : 'Twenty',
'3' : 'Thirty',
'4' : 'Forty',
'5' : 'Fifty',
'6' : 'Sixty',
'7' : 'Seventy',
'8' : 'Sighty',
'9' : 'Ninety'
}
n1 = n_list[-1]
n2 = n_list[-2]
number = n2 + n1
if number in dict2 == True:
result = dict2
else:
result = dict2 + ' ' + 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)
num3 = n_list
num6 = n_list
num9 = 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[:2]) + ' '+ 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[:2]) + ' '+ 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)楼主看这个行不行
scanhack
发表于 2020-2-15 15:09:05
最后的魁拔 发表于 2020-2-14 18:49
为啥这个就可以呢
百度可以哦
陈尚涵
发表于 2020-2-15 16:51:39
做一个叫'单位'的列表,在做一个叫'数字'的列表,接着做一个函数定义确认不就好了{:9_237:}?
zltzlt
发表于 2020-2-15 16:55:34
阴阳神万物主 发表于 2020-2-14 23:57
我就是要加 And 你来咬我呀~~
输入 1000010,多了一个空格
zltzlt
发表于 2020-2-15 16:56:23
塔利班 发表于 2020-2-14 18:47
101怎么整One Hundred And One ?要加And的么
不需要,加也行
zltzlt
发表于 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
zltzlt
发表于 2020-2-15 17:01:29
白咕咕 发表于 2020-2-15 12:48
楼主看这个行不行
输入 12345,结果有误
阴阳神万物主
发表于 2020-2-15 18:41:07
zltzlt 发表于 2020-2-15 16:55
输入 1000010,多了一个空格
这样应该就没有多的空格了。
dic = {2:['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'],
3:['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'],
1:['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
'Seventeen','Eighteen','Nineteen'],
0:['','Thousand','Million','Billion']}
def solve(n):
num =
temp = for i in range(len(num),0,-3)]
res = ''
for i in range(len(temp)):
while len(temp)<3:
temp.insert(0,0)
trs = ]]
if temp == 1:
trs.append(dic])
else:
trs.extend(],dic]])
if trs:
if trs or trs:
trs.insert(1,'And')
trs.insert(1,'Hundred')
elif (not i) and (trs or trs):
trs.insert(1,'And')
while '' in trs:
trs.remove('')
if trs:
if i:trs.append(dic)
if res:
res = ' '.join(trs)+' '+res
else:
res = ' '.join(trs)
return res if res else 'Zero'
if __name__ == '__main__':
print('示例1 输出:',repr(solve(123)))
print('示例2 输出:',repr(solve(12345)))
print('示例3 输出:',repr(solve(1234567)))
print('示例4 输出:',repr(solve(1234567891)))
print('之前多空格的 输出:',repr(solve(1000010)))