Skip to content

Instantly share code, notes, and snippets.

@eastlondoner
Last active May 22, 2019 15:44
Show Gist options
  • Save eastlondoner/943032ecc89efea01eaf35d2a855f6fc to your computer and use it in GitHub Desktop.
Save eastlondoner/943032ecc89efea01eaf35d2a855f6fc to your computer and use it in GitHub Desktop.
Generates a csv of the numbers one to a million as both an integer and representation as English words
import math
def num2words(num):
nums_20_90 = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
nums_0_19 = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', "Nine", 'Ten', 'Eleven',
'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
nums_dict = {100: 'hundred', 1000: 'thousand', 1000000: 'million'}
if num < 20:
return nums_0_19[num]
if num < 100:
return nums_20_90[int(num / 10 - 2)] + ('' if num % 10 == 0 else ' ' + nums_0_19[num % 10])
# find the largest key smaller than num
maxkey = max([key for key in nums_dict.keys() if key <= num])
return num2words(int(num / maxkey)) + ' ' + nums_dict[maxkey] + (
'' if num % maxkey == 0 else (' and ' if maxkey==100 else ', ') + num2words(num % maxkey))
i = 0
with open('numbers.csv', 'w') as f:
print('value,text', file=f)
for x in range(1, int(math.pow(10, 6)) + 1):
print(str(x) + ',' + num2words(x), file=f)
i += 1
print("Printed " + str(i) + " numbers to numbers.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment