As part of my Python learning, I'm taking my old Ruby codes and ported them to Python...and believe me...there's no better way to learn -:)
The first program that I took off was Decimals to Romans, which I discovered that wasn't working right for big numbers -:( After a few tweaks on Python, it's ready for action -;)
Roman_Table = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD',
100: 'C', 90: 'XC', 50: 'L', 40: 'XL',
10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}
global result
keys = []
def reverse_numeric(x, y):
return y - x
def Roman_Number(number):
result = ""
keys = Roman_Table.keys()
keys = sorted(keys, cmp=reverse_numeric)
while number > 0:
for i in keys:
if number >= i:
result += str(Roman_Table.get(i, 0))
number -= i
break
return result
number = input("\nEnter a number: ")
result = Roman_Number(number)
print ("\n" + result)
The next one was LCD Numbers which believe it or not...I manage to cut down from the 160 lines of Ruby code, to only 35 lines...awesome, right?
global line1, line2, line3
line1 = ""
line2 = ""
line3 = ""
zero = {1: ' _ ', 2: '| | ', 3: '|_| '}
one = {1: ' ', 2: '| ', 3: '| '}
two = {1: ' _ ', 2: ' _| ', 3: '|_ '}
three = {1: '_ ', 2: '_| ', 3: '_| '}
four = {1: ' ', 2: '|_| ', 3: ' | '}
five = {1: ' _ ', 2: '|_ ', 3: ' _| '}
six = {1: ' _ ', 2: '|_ ', 3: '|_| '}
seven = {1: '_ ', 2: ' | ', 3: ' | '}
eight = {1: ' _ ', 2: '|_| ', 3: '|_| '}
nine = {1: ' _ ', 2: '|_| ', 3: ' _| '}
num_lines = {0: zero, 1: one, 2: two, 3: three, 4: four,
5: five, 6: six, 7: seven, 8: eight, 9: nine}
def Lines(number):
global line1, line2, line3
line1 += number.get(1, 0)
line2 += number.get(2, 0)
line3 += number.get(3, 0)
number = str(input("\nEnter a number: "))
length = len(number)
for i in range(0, length):
Lines(num_lines.get(int(number[i:i+1]), 0))
print ("\n")
print line1
print line2
print line3
print ("\n")
I'm really sorry about Ruby, because I still love it...but Python is making so much efforts to become my scripting language of choice -;)
Greetings,
Blag.
No hay comentarios:
Publicar un comentario