Como parte de mi aprendizaje de Python, estoy tomando mis viejos codigos de Ruby y convirtiendolos a Python...y creanme...no hay mejor manera de aprender -:)
El primer programa que tome fue Decimales a Romanos, el cual descubri que no estaba funcionando bien para numeros grandes -:( Luego de unos retoques con Python, esta listo para la accion -;)
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)

El siguiente fue Numeros LCD el cual creanlo o no...logre bajar de las 160 lineas de codigo de Ruby, a solamente 35 lineas...impresionante, no?
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")

Siento mucha pensa por Ruby, porque aun lo amo mucho...pero Python esta haciendo mucho esfuerzo para convertirse en mi lenguaje script favorito -;)
Saludos,
Blag.
No comments:
Post a Comment