Decimal to Roman conversion
#11
(01-08-2016, 06:09 PM)b1nary Wrote:
(01-08-2016, 05:15 PM)bitm0de Wrote: Huh? AutoIT is for Windows too, not sure what you're talking about there.

https://www.autoitscript.com/site/autoit/
Quote:AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!

If you notice, I said "non windoze users". As in, anybody NOT on windows. Those on linux for example. Autoit is not cross platform if you're not familiar with it. The only OS it will run on, is windows. Smile

https://appdb.winehq.org/objectManager.p...n&iId=5480
Reply
#12
(01-09-2016, 01:25 AM)bitm0de Wrote: https://appdb.winehq.org/objectManager.php?sClass=version&iId=5480

Not saying you can't get it to run on linux, but it's not like C or Java. You can't just compile and run it on linux (or Mac) without wine Smile
Reply
#13
(01-09-2016, 04:23 AM)b1nary Wrote:
(01-09-2016, 01:25 AM)bitm0de Wrote: https://appdb.winehq.org/objectManager.php?sClass=version&iId=5480

Not saying you can't get it to run on linux, but it's not like C or Java. You can't just compile and run it on linux (or Mac) without wine Smile

You maybe didn't mean it like that, but you sure said it.
Quote:The only OS it will run on, is windows.
Reply
#14
(01-09-2016, 05:40 AM)zeroday Wrote: You maybe didn't mean it like that, but you sure said it.

You're right, and for that I apologize I should have elaborated every single special condition you could execute this code on non windows OS. AutoIt was designed for windows, not linux etc, was really all that I meant by the statement. Not to mention, who do you know that has autoit installed on a non M$ box to even compile said code?

All this over the other guy TOTALLY misreading the first statement, thinking I said it would not run on windows, when in fact I said just the opposite. Gee willikers guys, give it a rest.
Reply
#15
CODE:
Code:
def Convert(number):
    
    thousand = ['M']
    houndred = ['C','CD','D','CM']
    decimal = ['X','XL','L','XC']
    unit = ['I','IV','V','IX']
    roman = ''
    
    th = number / 1000
    if ( th != 0 ) :
        roman += stepConv(thousand,th)
        
    ho = (number / 100) - (th * 10)
    if ( ho != 0 ) :
        roman += stepConv(houndred,ho)
        
    de = (number / 10) - ((th * 100) + (ho * 10))
    if ( de != 0 ) :
        roman += stepConv(decimal,de)
        
    un = number % 10
    roman += stepConv(unit,un)
    
    return roman

def stepConv(ref,number):
    
    converted = ''
    if number < 4 :
        for i in range(number):
            converted += ref[0]
    elif number == 4 :
        converted += ref[1]
    elif number  == 5 :
        converted += ref[2]
    elif number > 5 and number < 9 :
        converted += ref[2]
        for i in range(number - 5):
            converted += ref[0]
    elif number == 9 :
        converted += ref[3]    

    return converted
    
while True:
    
    decimal = input("Type a number : ")
    if  decimal < 3999 and decimal > 0 :
        roman = Convert(decimal)
        print roman
        break
    else :
        print("You have to type a number between 1 and 3999")

RESULT:

[Image: X5AuThu.png]

I'm learning Python, and this was a very good exercise Smile ....
Reply
#16
(02-14-2016, 08:25 PM)overfl0wN Wrote: CODE:
Code:
def Convert(number):

thousand = ['M']
houndred = ['C','CD','D','CM']
decimal = ['X','XL','L','XC']
unit = ['I','IV','V','IX']
roman = ''

th = number / 1000
if ( th != 0 ) :
roman += stepConv(thousand,th)

ho = (number / 100) - (th * 10)
if ( ho != 0 ) :
roman += stepConv(houndred,ho)

de = (number / 10) - ((th * 100) + (ho * 10))
if ( de != 0 ) :
roman += stepConv(decimal,de)

un = number % 10
roman += stepConv(unit,un)

return roman

def stepConv(ref,number):

converted = ''
if number < 4 :
for i in range(number):
converted += ref[0]
elif number == 4 :
converted += ref[1]
elif number  == 5 :
converted += ref[2]
elif number > 5 and number < 9 :
converted += ref[2]
for i in range(number - 5):
converted += ref[0]
elif number == 9 :
converted += ref[3]

return converted

while True:

decimal = input("Type a number : ")
if  decimal < 3999 and decimal > 0 :
roman = Convert(decimal)
print roman
break
else :
print("You have to type a number between 1 and 3999")

RESULT:

[Image: X5AuThu.png]

I'm learning Python, and this was a very good exercise Smile ....

Hey,

Cleaned up your code a little bit:

Code:
#!/usr/bin/env python

try:
   input = raw_input
except NameError:
   pass

roman_numerals = {
   "THOUSAND": [1000, ["M"]],
   "HUNDRED": [100, ["C", "CD", "D", "CM"]],
   "DECIMAL": [10, ["X", "XL", "L", "XC"]],
   "UNIT": [0, ["I", "IV", "V", "IX"]]
}


def convert(number):
   roman = ""
   for key in roman_numerals.keys():
       value = roman_numerals[key]

       if value[0] > 0:
           test = number / value[0]
           number -= test * value[0]
       else:
           test = number % 10
           number -= test
       if (test != 0):
           roman += step_conv(value[1], test)

   return roman


def step_conv(ref, number):
   converted = ""
   if number < 4:
       for i in range(number):
           converted += ref[0]
   elif number == 4:
       converted += ref[1]
   elif number == 5:
       converted += ref[2]
   elif number > 5 and number < 9:
       converted += ref[2]
       for i in range(number - 5):
           converted += ref[0]
   else:
       converted += ref[3]
   return converted


if __name__ == "__main__":
   number = int(input("Type a number: "))
   if number > 0 and number < 3999:
       roman = convert(number)
       print(roman)
   else:
       print("Your number must be between 1 and 3999")
Reply