Skip to content

Instantly share code, notes, and snippets.

@zoek1
Created July 4, 2017 00:56
Show Gist options
  • Save zoek1/9ddc7e162f3165b57af932a29d400244 to your computer and use it in GitHub Desktop.
Save zoek1/9ddc7e162f3165b57af932a29d400244 to your computer and use it in GitHub Desktop.
Kata de numeros romanos - python 101
import unittest
import substitucion
# Escribir tests correspondientes a reglas de numeros romanos
# Refactorizar codigo como sea requerido
def romano2decimal(romano):
acum = 0
numbers = {
'I': 1,
'V': 5,
'X': 10
}
last = numbers[romano[0]]
# V I
for number in romano:
if last < numbers[number]:
acum = numbers[number] - last
elif 'I' == number:
acum += 1
else:
acum += numbers[number]
last = numbers[number]
return acum
class RomanosTest(unittest.TestCase):
def test_numero_uno(self):
self.assertEqual(1, romano2decimal('I'))
def test_numero_dos(self):
self.assertEqual(2, romano2decimal('II'))
def test_numero_tres(self):
self.assertEqual(3, romano2decimal('III'))
def test_resta_de_pares_de_numeros(self):
for elemento in [
('IV', 4), ('IX', 9) ]:
self.assertEqual(elemento[1],
romano2decimal(elemento[0]))
def test_suma_de_elementos_consecutivos(self):
for elemento in [
('VI', 6), ('XI', 11),
('VII', 7), ('XII', 12),
('VIII', 8), ('XIII', 13), ('XV', 15),
('XVIII', 18) ]:
self.assertEqual(elemento[1],
romano2decimal(elemento[0]))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment