Last active
August 29, 2015 14:08
-
-
Save kulicuu/edb4f2a1caf32745ccba to your computer and use it in GitHub Desktop.
translate arabic to roman numerals :: the boring and un-elegant way
This file contains 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
c= console.log | |
c "another arabic to roman numeral translator: \n" | |
digitiser= (integra)-> | |
if integra >= 10000 then c "error, we're only taking numbers less than 10,000" | |
strang= integra.toString() | |
rayy= strang.split '' | |
arq= {} | |
switch strang.length | |
when 4 | |
arq= | |
thousands: rayy[0] | |
hundreds: rayy[1] | |
tens: rayy[2] | |
ones: rayy[3] | |
when 3 | |
arq= | |
thousands: 0 | |
hundreds: rayy[0] | |
tens: rayy[1] | |
ones: rayy[2] | |
when 2 | |
arq= | |
thousands: 0 | |
hundreds: 0 | |
tens: rayy[0] | |
ones: rayy[1] | |
when 1 | |
arq= | |
thousands: 0 | |
hundreds: 0 | |
tens: 0 | |
ones: rayy[0] | |
return arq | |
parse_thousands= (thousands)-> | |
num= parseInt thousands | |
if num is 0 | |
return '' | |
else | |
strang= '' | |
for i in [1 .. num] | |
strang= strang + 'M' | |
return strang | |
parse_hundreds= (hundreds)-> | |
num= parseInt hundreds | |
switch num | |
when 0 | |
return '' | |
when 1 | |
return 'C' | |
when 2 | |
return 'CC' | |
when 3 | |
return 'CCC' | |
when 4 | |
return 'CD' | |
when 5 | |
return 'D' | |
when 6 | |
return 'DC' | |
when 7 | |
return 'DCC' | |
when 8 | |
return 'CCM' | |
when 9 | |
return 'CM' | |
parse_tens= (tens)-> | |
num= parseInt tens | |
switch num | |
when 0 | |
return '' | |
when 1 | |
return 'X' | |
when 2 | |
return 'XX' | |
when 3 | |
return 'XXX' | |
when 4 | |
return 'XL' | |
when 5 | |
return 'L' | |
when 6 | |
return 'LX' | |
when 7 | |
return 'LXX' | |
when 8 | |
return 'XXC' | |
when 9 | |
return 'XC' | |
parse_ones= (ones)-> | |
num= parseInt ones | |
switch num | |
when 0 | |
return '' | |
when 1 | |
return 'I' | |
when 2 | |
return 'II' | |
when 3 | |
return 'III' | |
when 4 | |
return 'IV' | |
when 5 | |
return 'V' | |
when 6 | |
return 'VI' | |
when 7 | |
return 'VII' | |
when 8 | |
return 'IIX' | |
when 9 | |
return 'IX' | |
#alpha= digitiser 15 | |
#beta= digitiser 225 | |
#gamma= digitiser 1910 | |
romanify= (arabic)-> | |
digitised= digitiser arabic | |
roman= (parse_thousands digitised.thousands) + (parse_hundreds digitised.hundreds) + (parse_tens digitised.tens) + (parse_ones digitised.ones) | |
return roman | |
#strang= (parse_thousands gamma.thousands) + (parse_hundreds gamma.hundreds) + (parse_tens gamma.tens) + (parse_ones gamma.ones) | |
#c 'strang: ', strang | |
test_cases= [1910, 2003, 544, 33, 11, 17] | |
for numero in test_cases | |
c "#{numero}: #{romanify numero}" | |
This file contains 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
def c(s): | |
print s | |
c("another Arabic number to Roman numeral translator. \n") | |
def digitiser(integra): | |
if (integra >= 10000): | |
c("error; we're only taking numbers less than 10,000.") | |
else: | |
strang= str(integra) | |
rayy= list(strang) | |
if (len(strang)== 4): | |
arq= { | |
'thousands': rayy[0], | |
'hundreds': rayy[1], | |
'tens': rayy[2], | |
'ones': rayy[3] | |
} | |
elif (len(strang)== 3): | |
arq= { | |
'thousands': '0', | |
'hundreds': rayy[0], | |
'tens': rayy[1], | |
'ones': rayy[2] | |
} | |
elif (len(strang)== 2): | |
arq= { | |
'thousands': '0', | |
'hundreds': '0', | |
'tens': rayy[0], | |
'ones': rayy[1] | |
} | |
elif (len(strang)== 1): | |
arq= { | |
'thousands': '0', | |
'hundreds': '0', | |
'tens': '0', | |
'ones': rayy[0] | |
} | |
return(arq) | |
def parse_thousands(thousands): | |
num= int(thousands) | |
if num == 0: | |
return '' | |
else: | |
strang= '' | |
for i in range(0, num): | |
strang= strang + 'M' | |
return strang | |
def parse_hundreds(hundreds): | |
num= int(hundreds) | |
if num == 0: | |
return '' | |
elif num == 1: | |
return 'C' | |
elif num == 2: | |
return 'CC' | |
elif num == 3: | |
return 'CCC' | |
elif num == 4: | |
return 'CD' | |
elif num == 5: | |
return 'D' | |
elif num == 6: | |
return 'DC' | |
elif num == 7: | |
return 'DCC' | |
elif num == 8: | |
return 'CCM' | |
elif num == 9: | |
return 'CM' | |
def parse_tens(tens): | |
num= int(tens) | |
if num == 0: | |
return '' | |
elif num == 1: | |
return 'X' | |
elif num == 2: | |
return 'XX' | |
elif num == 3: | |
return 'XXX' | |
elif num == 4: | |
return 'XL' | |
elif num == 5: | |
return 'L' | |
elif num == 6: | |
return 'LX' | |
elif num == 7: | |
return 'LXX' | |
elif num == 8: | |
return 'XXC' | |
elif num == 9: | |
return 'XC' | |
def parse_ones(ones): | |
num= int(ones) | |
if num == 0: | |
return '' | |
elif num == 1: | |
return 'I' | |
elif num == 2: | |
return 'II' | |
elif num == 3: | |
return 'III' | |
elif num == 4: | |
return 'IV' | |
elif num == 5: | |
return 'V' | |
elif num == 6: | |
return 'VI' | |
elif num == 7: | |
return 'VII' | |
elif num == 8: | |
return 'IIX' | |
elif num == 9: | |
return 'IX' | |
def romanify(arabic): | |
digitised= digitiser(arabic) | |
#c(parse_thousands(digitised['thousands'])) | |
return parse_thousands(digitised['thousands']) + parse_hundreds(digitised['hundreds']) + parse_tens(digitised['tens']) + parse_ones(digitised['ones']) | |
test_cases= [5, 10, 15, 23, 800, 55, 1864, 2010, 1907] | |
for item in test_cases: | |
c(str(item) + ": " + romanify(item)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment