Skip to content

Instantly share code, notes, and snippets.

@hackerdem
Created April 14, 2016 07:52
Show Gist options
  • Save hackerdem/02b8bdfdc5bebb5da54f1318aa7c8c82 to your computer and use it in GitHub Desktop.
Save hackerdem/02b8bdfdc5bebb5da54f1318aa7c8c82 to your computer and use it in GitHub Desktop.
Convert numbers to roman numbers using a class instances
class A:
def __init__(self,num):
self.num=num
self.num_list=[1,4,5,9,10,40,50,90,100,400,500,900,1000]
self.rom_list=['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M']
self.roman_num=" "
self.next_val= num
def convert(self,num):
while self.next_val!=0:
if self.next_val<1000:
for i in range(len(self.num_list)):
if self.next_val<self.num_list[i] and self.next_val>=self.num_list[i-1]:
self.next_val=self.next_val-self.num_list[i-1]
self.roman_num=self.roman_num+(self.rom_list[i-1])
else:
self.roman_num=self.roman_num+(self.next_val//1000)*'M'
self.next_val=self.next_val-(self.next_val//1000)*1000
print(self.roman_num)
numbers=[1996,2012,6512,57,66]
for i in numbers:
deneme=A(i)
deneme.convert(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment