Commit 04f372f42db6056cd30fa3e0317e1afe01ccd0ce
1 parent
0b1d2c96
Exists in
master
and in
1 other branch
Corrige conversor de números romanos
Showing
2 changed files
with
41 additions
and
21 deletions
Show diff stats
src/AplicaRegras.py
@@ -399,8 +399,11 @@ class AplicaRegras(object): | @@ -399,8 +399,11 @@ class AplicaRegras(object): | ||
399 | tag = it.get_ticket() | 399 | tag = it.get_ticket() |
400 | 400 | ||
401 | if tag == "NUM": | 401 | if tag == "NUM": |
402 | - num_romano = roman_to_int(it.get_word()) | ||
403 | - lista_simplificada[it.get_count()] = [num_romano.decode('utf-8'), 'NUM-R'] | 402 | + try: |
403 | + num_romano = roman_to_int(it.get_word().encode('utf-8')) | ||
404 | + lista_simplificada[it.get_count()] = [num_romano.decode('utf-8'), 'NUM-R'] | ||
405 | + except: | ||
406 | + pass | ||
404 | num = True | 407 | num = True |
405 | 408 | ||
406 | if tag[-2:] == "-P" or tag[-2:] == "_P" and self.verificar_excecao_plural(it.get_word()): | 409 | if tag[-2:] == "-P" or tag[-2:] == "_P" and self.verificar_excecao_plural(it.get_word()): |
src/ConverteExtenso.py
@@ -25,28 +25,45 @@ ext = [{"um":"1", "dois":"2", "tres":"3", "quatro":"4", "cinco":"5", "seis":"6", | @@ -25,28 +25,45 @@ ext = [{"um":"1", "dois":"2", "tres":"3", "quatro":"4", "cinco":"5", "seis":"6", | ||
25 | und = {"mil":1000, "milhao":1000000, "bilhao":1000000000, "trilhao":1000000000000} | 25 | und = {"mil":1000, "milhao":1000000, "bilhao":1000000000, "trilhao":1000000000000} |
26 | unds = {"mil":"000", "milhao":"000000","milhoes":"000000", "bilhao":"000000000","bilhoes":"000000000", "trilhao":"000000000000", "trilhoes":"000000000000"} | 26 | unds = {"mil":"000", "milhao":"000000","milhoes":"000000", "bilhao":"000000000","bilhoes":"000000000", "trilhao":"000000000000", "trilhoes":"000000000000"} |
27 | 27 | ||
28 | -numeral_map = zip( | ||
29 | - (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), | ||
30 | - ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') | ||
31 | -) | ||
32 | - | ||
33 | -def int_to_roman(i): | 28 | +def int_to_roman(input): |
29 | + if not isinstance(input, type(1)): | ||
30 | + raise TypeError, "expected integer, got %s" % type(input) | ||
31 | + if not 0 < input < 4000: | ||
32 | + raise ValueError, "Argument must be between 1 and 3999" | ||
33 | + ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) | ||
34 | + nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I') | ||
34 | result = [] | 35 | result = [] |
35 | - for integer, numeral in numeral_map: | ||
36 | - count = int(i / integer) | ||
37 | - result.append(numeral * count) | ||
38 | - i -= integer * count | ||
39 | - return ''.join(result) | ||
40 | 36 | ||
41 | -def roman_to_int(n): | ||
42 | - n = unicode(n).upper() | 37 | + for i in range(len(ints)): |
38 | + count = int(input / ints[i]) | ||
39 | + result.append(nums[i] * count) | ||
40 | + input -= ints[i] * count | ||
41 | + return ''.join(result) | ||
43 | 42 | ||
44 | - i = result = 0 | ||
45 | - for integer, numeral in numeral_map: | ||
46 | - while n[i:i + len(numeral)] == numeral: | ||
47 | - result += integer | ||
48 | - i += len(numeral) | ||
49 | - return str(result) | 43 | +def roman_to_int(input): |
44 | + if not isinstance(input, type("")): | ||
45 | + raise TypeError, "expected string, got %s" % type(input) | ||
46 | + input = input.upper( ) | ||
47 | + nums = {'M':1000, | ||
48 | + 'D':500, | ||
49 | + 'C':100, | ||
50 | + 'L':50, | ||
51 | + 'X':10, | ||
52 | + 'V':5, | ||
53 | + 'I':1} | ||
54 | + sum = 0 | ||
55 | + for i in range(len(input)): | ||
56 | + try: | ||
57 | + value = nums[input[i]] | ||
58 | + if i+1 < len(input) and nums[input[i+1]] > value: | ||
59 | + sum -= value | ||
60 | + else: sum += value | ||
61 | + except KeyError: | ||
62 | + raise ValueError, 'input is not a valid Roman numeral: %s' % input | ||
63 | + | ||
64 | + if int_to_roman(sum) == input: return str(sum) | ||
65 | + else: | ||
66 | + raise ValueError, 'input is not a valid Roman numeral: %s' % input | ||
50 | 67 | ||
51 | def oneDigit(x): | 68 | def oneDigit(x): |
52 | return ext[0][x] | 69 | return ext[0][x] |