v*******C 发帖数: 28 | 1 convert integer to english words
e.g.,
123 -> "One hundred and twenty three"
1234567 -> "One million two hundred and thirty four thousand five hundred
and sixty seven"
12345 -> "twelve thousand three hundred and forty five"
目测悲剧了。。。只能发面经攒人品了。继续刷题 | j**********3 发帖数: 3211 | | h*********8 发帖数: 73 | | v*******C 发帖数: 28 | 4 不是, 是new grad, 几分钟前刚面完。
【在 j**********3 的大作中提到】 : are you senior?
| k***a 发帖数: 1199 | 5 目测这个题就是纯考察coding,紧张没写好?
【在 v*******C 的大作中提到】 : convert integer to english words : e.g., : 123 -> "One hundred and twenty three" : 1234567 -> "One million two hundred and thirty four thousand five hundred : and sixty seven" : 12345 -> "twelve thousand three hundred and forty five" : 目测悲剧了。。。只能发面经攒人品了。继续刷题
| I**********a 发帖数: 1183 | 6 这题。。。让我自己念,我都吃不准and加在啥地方,汗。。
【在 k***a 的大作中提到】 : 目测这个题就是纯考察coding,紧张没写好?
| l******n 发帖数: 30 | | j**********3 发帖数: 3211 | 8 wish lz good luck!
【在 v*******C 的大作中提到】 : 不是, 是new grad, 几分钟前刚面完。
| l******n 发帖数: 30 | 9 试着写了一下,超麻烦,还没考虑单复数
def read_number(x):
m = {
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: 'forty',
50: 'fifty',
60: 'sixty',
70: 'seventy',
80: 'eighty',
90: 'nighty'
}
if 0 <= x <= 19:
return m[x]
elif x % 10 == 0 and x < 100:
return m[x]
elif x < 100:
return m[x - x % 10] + ' ' +m[x % 10]
elif x < 1000:
if x % 100 != 0:
return m[x / 100] + ' hundred and ' + read_number(x % 100)
else:
return m[x / 100] + ' hundred'
elif x < 1000000:
if x % 1000 != 0:
if x % 1000 < 100:
return read_number(x / 1000) + ' thousand and ' + read_
number(x % 1000)
else:
return read_number(x / 1000) + ' thousand ' + read_number(x
% 1000)
else:
return read_number(x / 1000) + ' thousand'
elif x < 1000000000:
if x % 1000000 != 0:
if x % 1000000 < 1000:
return read_number(x / 1000000) + ' million and ' + read_
number(x % 1000000)
else:
return read_number(x / 1000000) + ' million ' + read_number(
x % 1000000)
else:
return read_number(x / 1000000) + ' million' | k***a 发帖数: 1199 | 10 corner case不多吧,假定int32,最多到billion, 读出三位数,用billion,million,
thousand拼起来就行了
【在 l******n 的大作中提到】 : 目测edge case挺多
| | | l******n 发帖数: 30 | 11 其实主要在加不加 and 上
1000 = one thousand (无 and, 且 0 不需要读)
1122 = one thousand one hundred and twenty two (不需要 and)
1011 = one thousand and eleven (需要 and)
1001 = one thousand and one (需要 and)
【在 k***a 的大作中提到】 : corner case不多吧,假定int32,最多到billion, 读出三位数,用billion,million, : thousand拼起来就行了
| f**********e 发帖数: 288 | | j***y 发帖数: 1640 | 13 这种题 在电面里面做? 只能说 这些IT 公司被惯坏了, 65k 的 H1B 名额其实太多了
。 | s********l 发帖数: 998 | 14 最烦这种题了
我说话 从来都不这么念。。。
直接念数字多好~ | x******0 发帖数: 1025 | | e********3 发帖数: 229 | 16 抛个砖.有错哪里可以优化请吃出.
public class IntegerToEnglishWord {
public static void main(String[] args) {
IntegerToEnglishWord itew = new IntegerToEnglishWord();
System.out.println(itew.integerToEnglishWord(123456789));
}
public String integerToEnglishWord(long num) {
String res = "";
if (num == 0) {
return "zero";
}
boolean neg = false;
if (num < 0) {
num = -num;
neg = true;
}
Map map = new HashMap();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
map.put(9, "nine");
map.put(10, "ten");
map.put(11, "eleven");
map.put(12, "twelve");
map.put(13, "thirteen");
map.put(14, "forteen");
map.put(15, "fifteen");
map.put(16, "sixteen");
map.put(17, "seventeen");
map.put(18, "eighteen");
map.put(19, "nineteen");
map.put(20, "twenty");
map.put(30, "thirty");
map.put(40, "forty");
map.put(50, "fifty");
map.put(60, "sixty");
map.put(70, "seventy");
map.put(80, "eighty");
map.put(90, "ninety");
int i = 0;
boolean needAnd = false;
while(num > 0) {
int lastThree = (int) (num % 1000);
String cur = "";
int hundred = lastThree / 100;
if (hundred > 0) {
cur += map.get(hundred) + " hundred";
lastThree %= 100;
}
if (lastThree > 0) {
if (map.containsKey(lastThree)) {
if (!cur.isEmpty()) {
cur += " and " + map.get(lastThree);
} else {
cur = map.get(lastThree);
}
} else {
int ten = lastThree / 10 * 10;
if (!cur.isEmpty()) {
cur += " and " + map.get(ten);
} else {
cur = map.get(lastThree);
}
lastThree %= 10;
cur += " " + map.get(lastThree);
}
}
if (cur.isEmpty()) {
if (!res.isEmpty()) {
needAnd = true;
}
} else {
if (res.isEmpty()) {
res = cur;
} else {
if (needAnd) {
res = "and " + res;
}
String suffix = "";
if (i == 1) {
suffix = " thousand ";
} else if (i == 2) {
suffix = " million ";
} else {
suffix = " billion ";
}
res = cur + suffix + res;
}
if (num % 1000 < 100) {
needAnd = true;
} else {
needAnd = false;
}
}
i ++;
num /= 1000;
}
if (neg) {
res = "negative " + res;
}
return res;
}
} | v*******C 发帖数: 28 | 17 哎,让我念,我都念不好,加上紧张,面试一半时间在coding,一半时间在调bug了
【在 k***a 的大作中提到】 : 目测这个题就是纯考察coding,紧张没写好?
| h**p 发帖数: 211 | 18 这题总代码会挺长,因为要建几个额外的array当map用,中间的实际代码量会很小
从后往前,每次只取3个,同时传入参数决定是空/thousand/mil/bil。如果每次最后2
位不是00,那是肯定要加and的
还有ls有位说单复数的同学,肯定是小学英文没学好,只有three hundred,没有three
hundreds | l******n 发帖数: 30 | 19 好吧,那我支票都写错鸟
2
three
【在 h**p 的大作中提到】 : 这题总代码会挺长,因为要建几个额外的array当map用,中间的实际代码量会很小 : 从后往前,每次只取3个,同时传入参数决定是空/thousand/mil/bil。如果每次最后2 : 位不是00,那是肯定要加and的 : 还有ls有位说单复数的同学,肯定是小学英文没学好,只有three hundred,没有three : hundreds
| d**********o 发帖数: 279 | 20 这个就用stack 很方便的。 虽然我做出来了, 不过面试也挂了。 另外一个公司。 | | | F****n 发帖数: 3271 | 21 美国英语不加and
【在 l******n 的大作中提到】 : 其实主要在加不加 and 上 : 1000 = one thousand (无 and, 且 0 不需要读) : 1122 = one thousand one hundred and twenty two (不需要 and) : 1011 = one thousand and eleven (需要 and) : 1001 = one thousand and one (需要 and)
| n****5 发帖数: 81 | 22 用 C 写了一下,用的递归来处理商和余数。用的unsigned int所以假定输入小于1百万
X1百万
#include
#include
const char* tens[] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "
Seventy", "Eighty", "Ninety"};
const char* lt20[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "
Sixteen", "Seventeen", "Eighteen", "Nineteen"};
void num2str(unsigned int num)
{
if (num == 0)
return;
else if (num < 20)
printf("%s", lt20[num]);
else if (num < 100) {
unsigned int q = num / 10;
assert(q >= 2);
printf("%s", tens[q-2]);
unsigned int r = num % 10;
if (r)
printf(" %s", lt20[r]);
}
else if (num < 1000) {
unsigned int q = num / 100;
assert ((q >= 1) && (q < 10));
num2str(q);
printf(" hundred");
unsigned int r = num % 100;
if (r) {
printf(" and ");
num2str(r);
}
}
else if (num < 1000000) {
unsigned int q = num / 1000;
assert ((q >= 1) && (q < 1000));
num2str(q);
printf(" thousand");
unsigned int r = num % 1000;
if (r) {
printf(" ");
num2str(r);
}
}
else {
unsigned int q = num / 1000000;
assert (q < 1000000);
num2str(q);
printf(" mil");
unsigned int r = num % 1000000;
if (r) {
printf(" ");
num2str(r);
}
}
}
int main()
{
unsigned int tests[] = {0, 8, 10, 15, 18, 30, 38, 128, 1000, 128350,
1000000, 1284518, 21000000};
unsigned int numTests = sizeof(tests)/sizeof(unsigned int);
for (unsigned int i=0; i
printf("%u: ", tests[i]);
if (tests[i] < 20)
printf("%s\n", lt20[tests[i]]);
else {
num2str(tests[i]);
printf("\n");
}
}
return 0;
} | r****t 发帖数: 10904 | 23 可能是无递归的最简解法,因为 python int 不限大小,可随便上至 octillion,
whatever
import locale
eng = { '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': 'forteen',
'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': 'nighty'
, '00': 'and', }
exponent = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
'quintillion', 'sextillion', 'septillion', 'octillion']
def engof3digit(numstr):
''' englist of 3 digits
'''
assert len(numstr) <= 3
last2 = numstr[-2:]
if last2 == '00':
ret = ''
else:
try:
ret = eng[last2]
except KeyError:
ten, one = last2
ret = '%s %s' % (eng[ten + '0'], eng[one])
try:
hundred = numstr[-3]
if hundred == '0':
ret = 'and %s' % ret
else:
ret = '%s hundred %s' % (eng[hundred], ret)
except IndexError:
pass
return ret.strip()
def num2eng(num):
locale.setlocale(locale.LC_ALL, 'en_US')
numstr = locale.format("%d", num, grouping=True)
sections = numstr.split(',')
words = [engof3digit(s) for s in sections]
pairs = zip(words[::-1], exponent)[::-1]
english = ' '.join(['%s %s' % (e, m) for e, m in pairs])
print numstr, english
return english
num2eng(123)
num2eng(103)
num2eng(10345)
num2eng(1345034)
num2eng(1300505562780)
$ python number2eng.py
123 one hundred twenty three
103 one hundred and three
10,345 ten thousand three hundred forty five
1,345,034 one million three hundred forty five thousand and thirty four
1,300,505,562,780 one trillion three hundred billion five hundred and five
million five hundred sixty two thousand seven hundred eighty |
|