Skip to content

Instantly share code, notes, and snippets.

@yevrah
Last active October 13, 2017 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yevrah/ab9825b22092020b690a2d5105467acc to your computer and use it in GitHub Desktop.
Save yevrah/ab9825b22092020b690a2d5105467acc to your computer and use it in GitHub Desktop.
Roman Numerals
#!/usr/bin/env python3
# encoding: utf-8
class Romanize():
# Values from https://www.math.nmsu.edu/~pmorandi/math111f01/RomanNumerals.html
numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
numeral = [ 'm', 'cm', 'd', 'cd', 'c', 'xc', 'l', 'xl', 'x', 'ix', 'v', 'iv', 'i']
def calculate(number):
out = ""
number = int(number)
for i, r in zip(Romanize.numbers, Romanize.numeral):
while number >= i:
number = number - i
out += r
return out
if __name__ == '__main__':
print("Some examples: ")
print(" Using {} we get {}".format(7, Romanize.calculate(7)))
print(" Using {} we get {}".format(8, Romanize.calculate(8)))
print(" Using {} we get {}".format(90, Romanize.calculate(90)))
print(" Using {} we get {}".format(123, Romanize.calculate(123)))
print(" Using {} we get {}".format(1984, Romanize.calculate(1984)))
print()
input = input('Now enter in your own number: ')
print("The roman numeral is {}".format(Romanize.calculate(input)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment