Skip to content

Instantly share code, notes, and snippets.

@bismarckjunior
Created November 19, 2018 13:30
Show Gist options
  • Save bismarckjunior/9e5612bc13780e788a17c79afbde46f2 to your computer and use it in GitHub Desktop.
Save bismarckjunior/9e5612bc13780e788a17c79afbde46f2 to your computer and use it in GitHub Desktop.
Two functions that convert a number in a Roman numeral.
# Author: Bismarck Gomes <bismarckgomes@gmail.com>
def to_roman(num):
d = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
roman = d[0] * num
for i, v, x in zip(d[::2],d[1::2], d[2::2]):
roman = roman.replace(5*i, v).replace(4*i, i+v)
roman = roman.replace(2*v, x).replace(v+i+v, i+x)
return roman
def to_roman2(num):
roman = ''
d = [('I', 1), ('IV', 4), ('V', 5), ('IX', 9),
('X',10), ('XL', 40), ('L', 50), ('XC', 90),
('C',100), ('CD', 400), ('D', 500), ('CM', 900),
('M', 1000) ]
for t, n in d[::-1]:
q, num = divmod(num, n)
roman += q*t
return roman
if __name__ == '__main__':
num = 200
func = to_roman#2
rows, cols = 5, 8
ini = 1
while (ini < num):
for row in range(ini,ini+rows):
for n in range(row, ini+rows*cols, rows):
if (n > num): break
print "%3d %-10s" % (n, func(n)),
print
print
ini += rows*cols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment