Browse Source

Utils fix and test, add decode roman numbers

remotes/origin/feincms
Tomas Gavenciak 10 years ago
parent
commit
3d6dfda6ba
  1. 6
      seminar/tests.py
  2. 14
      seminar/utils.py

6
seminar/tests.py

@ -11,6 +11,7 @@ from django.core.management import call_command
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Soustredeni, Nastaveni from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Soustredeni, Nastaveni
from seminar.testutils import create_test_data from seminar.testutils import create_test_data
from seminar import ovvpfile from seminar import ovvpfile
from seminar import utils
class SeminarBasicTests(TestCase): class SeminarBasicTests(TestCase):
def setUp(self): def setUp(self):
@ -72,4 +73,9 @@ class SeminarBasicTests(TestCase):
t = o.to_string() t = o.to_string()
assert t == filetext assert t == filetext
def test_roman(self):
for i in [0, 1, 23, 2015, 1999, 42, 4, 400, 78, 4321, 8765, 999]:
r = utils.roman(i)
fr = utils.from_roman(r)
assert fr == i

14
seminar/utils.py

@ -1,12 +1,20 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
roman_numerals = zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I'))
def roman(num): def roman(num):
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
res = "" res = ""
for i, n in zip(ints, nums): for i, n in roman_numerals:
res += n * (num // i) res += n * (num // i)
num %= i num %= i
return res return res
def from_roman(rom):
if not rom:
return 0
for i, n in roman_numerals:
if rom.upper().startswith(n):
return i + from_roman(rom[len(n):])
raise Exception('Invalid roman numeral: "%s"', rom)

Loading…
Cancel
Save