Created
August 4, 2020 15:03
-
-
Save drbh/1cefeb188b08dc1a9f4143542e6d7592 to your computer and use it in GitHub Desktop.
Get the numbers 0 to 50 as cardinal, ordinal, english and ints
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
import string | |
# mostly copied form here | |
# https://www.mathsisfun.com/numbers/cardinal-ordinal-chart.html | |
nth = { | |
1: "First", | |
2: "Second", | |
3: "Third", | |
4: "Fourth", | |
5: "Fifth", | |
6: "Sixth", | |
7: "Seventh", | |
8: "Eighth", | |
9: "Ninth", | |
10: "Tenth", | |
11: "Eleventh", | |
12: "Twelfth", | |
13: "Thirteenth", | |
14: "Fourteenth", | |
15: "Fifteenth", | |
16: "Sixteenth", | |
17: "Seventeenth", | |
18: "Eighteenth", | |
19: "Nineteenth", | |
20: "Twentieth", | |
21: "Twenty First", | |
22: "Twenty Second", | |
23: "Twenty Third", | |
24: "Twenty Fourth", | |
25: "Twenty Fifth", | |
25: "Twenty Fifth", | |
26: "Twenty Sixth", | |
27: "Twenty Seventh", | |
28: "Twenty Eighth", | |
29: "Twenty Ninth", | |
30: "Thirtieth", | |
31: "Thirty First", | |
32: "Thirty Second", | |
33: "Thirty Third", | |
34: "Thirty Fourth", | |
35: "Thirty Fifth", | |
36: "Thirty Sixth", | |
37: "Thirty Seventh", | |
38: "Thirty Eighth", | |
39: "Thirty Ninth", | |
40: "Fortieth", | |
41: "Forty First", | |
42: "Forty Second", | |
43: "Forty Third", | |
44: "Forty Fourth", | |
45: "Forty Fifth", | |
46: "Forty Sixth", | |
47: "Forty Seventh", | |
48: "Forty Eighth", | |
49: "Forty Ninth", | |
50: "Fiftieth", | |
} | |
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4]) | |
def english(n): | |
pieces = [] | |
if n < 0: | |
pieces.append("negative") | |
thousand_powers(-n, 0, pieces) | |
else: | |
thousand_powers(n, 0, pieces) | |
return " ".join(pieces) | |
thousand_words = ["", "thousand", "million", "billion"] | |
ten_words = ["", "ten", "twenty", "thirty", "forty", | |
"fifty", "sixty", "seventy", "eighty", "ninety"] | |
unit_words = ["zero", "one", "two", "three", "four", "five", "six", "seven", | |
"eight", "nine", "ten", "eleven", "twelve", "thirteen", | |
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", | |
"nineteen"] | |
def ten_powers(n, pieces): | |
if n < 20: | |
pieces.append(unit_words[n]) | |
else: | |
tens, units = divmod(n, 10) | |
pieces.append(ten_words[tens]) | |
if units != 0: | |
pieces.append(unit_words[units]) | |
def hundred_powers(n, pieces): | |
hundreds, units = divmod(n, 100) | |
if hundreds == 0: | |
ten_powers(units, pieces) | |
else: | |
ten_powers(hundreds, pieces) | |
pieces.append("hundred") | |
if units != 0: | |
ten_powers(units, pieces) | |
def thousand_powers(n, thousand_index, pieces): | |
millions, mod_millions = divmod(n, 1000000) | |
hundreds, units = divmod(mod_millions, 100) | |
if hundreds < 100 and hundreds % 10 != 0: | |
if millions != 0: | |
thousand_powers(millions, thousand_index + 2, pieces) | |
hundred_powers(mod_millions, pieces) | |
if thousand_index != 0: | |
pieces.append(thousand_words[thousand_index]) | |
else: | |
thousands, units = divmod(n, 1000) | |
if thousands == 0: | |
if thousand_index == 0: | |
hundred_powers(units, pieces) | |
elif units != 0: | |
hundred_powers(units, pieces) | |
pieces.append(thousand_words[thousand_index]) | |
else: | |
thousand_powers(thousands, thousand_index + 1, pieces) | |
if units != 0: | |
hundred_powers(units, pieces) | |
if thousand_index != 0: | |
pieces.append(thousand_words[thousand_index]) | |
items = [] | |
for i in range(50): | |
value = i | |
engli = english(i) | |
ordi = ordinal(i) | |
dct = { | |
"value": value, | |
"ordinal": ordi, | |
"engli": engli, | |
"cardinal": nth.get(i), | |
} | |
items += [dct] | |
import pandas as pd | |
df = pd.DataFrame(items) | |
df.to_csv("~/Downloads/0-50-mappings.csv", index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment