From 04f372f42db6056cd30fa3e0317e1afe01ccd0ce Mon Sep 17 00:00:00 2001 From: Erickson Silva Date: Mon, 26 Oct 2015 08:22:08 -0300 Subject: [PATCH] Corrige conversor de nĂºmeros romanos --- src/AplicaRegras.py | 7 +++++-- src/ConverteExtenso.py | 55 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/AplicaRegras.py b/src/AplicaRegras.py index c56cd2b..15a7422 100644 --- a/src/AplicaRegras.py +++ b/src/AplicaRegras.py @@ -399,8 +399,11 @@ class AplicaRegras(object): tag = it.get_ticket() if tag == "NUM": - num_romano = roman_to_int(it.get_word()) - lista_simplificada[it.get_count()] = [num_romano.decode('utf-8'), 'NUM-R'] + try: + num_romano = roman_to_int(it.get_word().encode('utf-8')) + lista_simplificada[it.get_count()] = [num_romano.decode('utf-8'), 'NUM-R'] + except: + pass num = True if tag[-2:] == "-P" or tag[-2:] == "_P" and self.verificar_excecao_plural(it.get_word()): diff --git a/src/ConverteExtenso.py b/src/ConverteExtenso.py index bb0a0e8..2208aff 100644 --- a/src/ConverteExtenso.py +++ b/src/ConverteExtenso.py @@ -25,28 +25,45 @@ ext = [{"um":"1", "dois":"2", "tres":"3", "quatro":"4", "cinco":"5", "seis":"6", und = {"mil":1000, "milhao":1000000, "bilhao":1000000000, "trilhao":1000000000000} unds = {"mil":"000", "milhao":"000000","milhoes":"000000", "bilhao":"000000000","bilhoes":"000000000", "trilhao":"000000000000", "trilhoes":"000000000000"} -numeral_map = 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 int_to_roman(i): +def int_to_roman(input): + if not isinstance(input, type(1)): + raise TypeError, "expected integer, got %s" % type(input) + if not 0 < input < 4000: + raise ValueError, "Argument must be between 1 and 3999" + 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') result = [] - for integer, numeral in numeral_map: - count = int(i / integer) - result.append(numeral * count) - i -= integer * count - return ''.join(result) -def roman_to_int(n): - n = unicode(n).upper() + for i in range(len(ints)): + count = int(input / ints[i]) + result.append(nums[i] * count) + input -= ints[i] * count + return ''.join(result) - i = result = 0 - for integer, numeral in numeral_map: - while n[i:i + len(numeral)] == numeral: - result += integer - i += len(numeral) - return str(result) +def roman_to_int(input): + if not isinstance(input, type("")): + raise TypeError, "expected string, got %s" % type(input) + input = input.upper( ) + nums = {'M':1000, + 'D':500, + 'C':100, + 'L':50, + 'X':10, + 'V':5, + 'I':1} + sum = 0 + for i in range(len(input)): + try: + value = nums[input[i]] + if i+1 < len(input) and nums[input[i+1]] > value: + sum -= value + else: sum += value + except KeyError: + raise ValueError, 'input is not a valid Roman numeral: %s' % input + + if int_to_roman(sum) == input: return str(sum) + else: + raise ValueError, 'input is not a valid Roman numeral: %s' % input def oneDigit(x): return ext[0][x] -- libgit2 0.21.2