Last active
August 29, 2015 14:18
-
-
Save yukpiz/f3739c1ccd2680709950 to your computer and use it in GitHub Desktop.
Convert number to English word with Python code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- encoding:utf-8 -*- | |
#words: 除算用の英単語辞書 | |
words = { | |
1000000000: 'Billion', | |
1000000: 'Million', | |
1000: 'Thousand', | |
100: 'Handred', | |
90: 'Ninety', | |
80: 'Eighty', | |
70: 'Sventy', | |
60: 'Sixty', | |
50: 'Fifty', | |
40: 'Forty', | |
30: 'Thirty', | |
20: 'Twenty', | |
19: 'Nineteen', | |
18: 'Eighteen', | |
17: 'Seventeen', | |
16: 'Sixteen', | |
15: 'Fifteen', | |
14: 'Fourteen', | |
13: 'Thirteen', | |
12: 'Twelve', | |
11: 'Eleven', | |
10: 'Ten', | |
} | |
#values: 1桁以下用の英単語辞書 | |
values = { | |
9: 'Nine', | |
8: 'Eight', | |
7: 'Seven', | |
6: 'Six', | |
5: 'Five', | |
4: 'Four', | |
3: 'Three', | |
2: 'Two', | |
1: 'One', | |
0: 'Zero', | |
} | |
def valueMatch(value): | |
#valueMatch関数は引数のvalueに対応する英単語を返します。 | |
#valueの値が1桁以下の場合は、valuesから値を取り出して返します。 | |
#それ以外の場合は、除算とその結果によって再帰処理を行い | |
#英単語をリストに積み上げていきます。 | |
#除算用の英単語辞書をキーの降順で繰り返す | |
for word in sorted(words, reverse=True): | |
div = 0 #除算結果(切り捨て) | |
out = [] #戻り値用のリスト | |
if values.has_key(value): | |
#1桁以下用にキーがあるなら、 | |
#該当する英単語を返す | |
return values[value] | |
elif value / word != 0: | |
#英単語辞書のキーで割った値が0でないなら | |
div = value / word #辞書のキーで割った商 | |
value = value % word #その余剰 | |
#キーが100以上(Ninety以下の該当を避ける為) | |
#または商に英単語が該当する場合 | |
if word > 99 and values.has_key(div): | |
#Hndred, Thousand, Million, Billionに対して、 | |
#頭に1~9の英単語をつける | |
out.append(values[div]) | |
elif not values.has_key(div): | |
#商が英単語に該当しない場合(商が10以上)、 | |
#商を引数にして再帰する | |
out.append(valueMatch(div)) | |
#Handred, Thousand, Million, Billionをリストに追加 | |
out.append(words[word]) | |
#valueが0でない場合(Zeroの出力を避ける) | |
if value != 0: | |
#余りに英単語が該当する場合 | |
if values.has_key(value): | |
out.append(values[value]) | |
else: | |
#余りが英単語に該当しない場合(0~9でない) | |
#余りを引数にして再帰する | |
out.append(valueMatch(value)) | |
#積み上げた英単語のリストを返す | |
return out | |
def valuePrint(negative, output): | |
#valuePrint関数は英単語のリストを | |
#展開して空白で区切って出力します。 | |
#negative値によって先頭に"Negative"が付加されます。 | |
#引数の英単語リストは多階層になっているので、 | |
#展開して文字列のリストに直す | |
line = [] #英単語文字列のリスト | |
fringe = [output] #展開用のフリンジ | |
#negative値がTrueなら先頭にNegativeをつけておく | |
if negative: | |
line.append("Negative") | |
while len(fringe) > 0: | |
node = fringe.pop(0) | |
#取り出した英単語がリストの場合 | |
if isinstance(node, list): | |
#取り出した状態でフリンジに戻す | |
fringe = node + fringe | |
else: | |
#リストでなかった場合は文字列として、 | |
#英単語文字列のリストに追加する | |
line.append(node) | |
#展開した英単語文字列のリストを | |
#半角空白で区切って標準出力する | |
print " ".join(line) | |
#入力データファイルの読み込み | |
f = open('testdata.in.txt', 'r') | |
#1行目はデータ数を示すので捨てる | |
line_count = int(f.readline()) | |
#2行目移行をデータとして読み込む | |
for line in f.readlines(): | |
#1文字目が"-"の場合、負数と判断する | |
negative = True if line[0] == "-" else False | |
#"-"は空文字で置き換えて消す | |
line = line.replace("-", "") | |
#英単語のリストを生成する | |
output = valueMatch(int(line)) | |
#生成した英単語のリストを標準出力する | |
valuePrint(negative, output) | |
#後処理 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment