StringAux.py
3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
# -*- coding: utf-8 -*-
from unicodedata import normalize
ext = [{1:"um", 2:"dois", 3:"três", 4:"quatro", 5:"cinco", 6:"seis",
7:"sete", 8:"oito", 9:"nove", 10:"dez", 11:"onze", 12:"doze",
13:"treze", 14:"quatorze", 15:"quinze", 16:"dezesseis",
17:"dezessete", 18:"dezoito", 19:"dezenove"}, {2:"vinte", 3:"trinta",
4:"quarenta", 5:"cinquenta", 6:"sessenta", 7:"setenta", 8:"oitenta",
9:"noventa"}, {1:"cento", 2:"duzentos", 3:"trezentos",
4:"quatrocentos", 5:"quinhentos", 6:"seissentos", 7:"setessentos",
8:"oitocentos", 9:"novecentos"}]
und = ['', ' mil', (' milhão', ' milhões'), (' bilhão', ' bilhões'),
(' trilhão', ' trilhões')]
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 cent(s, grand):
s = '0' * (3 - len(s)) + s
if s == '000':
return ''
if s == '100':
return 'cem'
ret = ''
dez = s[1] + s[2]
if s[0] != '0':
ret += ext[2][int(s[0])]
if dez != '00':
ret += ' e '
else:
return ret + (type(und[grand]) == type(()) and (int(s) > 1 and und[grand][1] or und[grand][0]) or und[grand])
if int(dez) < 20:
ret += ext[0][int(dez)]
else:
if s[1] != '0':
ret += ext[1][int(s[1])]
if s[2] != '0':
ret += ' e ' + ext[0][int(s[2])]
return ret + (type(und[grand]) == type(()) and (int(s) > 1 and und[grand][1] or und[grand][0]) or und[grand])
def extenso(n):
sn = str(int(n))
ret = []
grand = 0
while sn:
s = sn[-3:]
sn = sn[:-3]
ret.append(cent(s, grand))
grand += 1
ret.reverse()
return ' e '.join([r for r in ret if r])
def remover_acentos(txt):
""" Devolve cópia de uma str substituindo os caracteres
acentuados pelos seus equivalentes não acentuados.
ATENÇÃO: carateres gráficos não ASCII e não alfa-numéricos,
tais como bullets, travessões, aspas assimétricas, etc.
são simplesmente removidos!
>>> remover_acentos('[ACENTUAÇÃO] ç: áàãâä! éèêë? íìĩîï, óòõôö; úùũûü.')
'[ACENTUACAO] c: aaaaa! eeee? iiiii, ooooo; uuuuu.'
"""
try:
return normalize('NFKD', txt.decode('utf-8')).encode('ASCII','ignore')
except:
return normalize('NFKD', txt.decode('iso-8859-1')).encode('ASCII','ignore')
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 sum
else:
raise ValueError, 'input is not a valid Roman numeral: %s' % input
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 i in range(len(ints)):
count = int(input / ints[i])
result.append(nums[i] * count)
input -= ints[i] * count
return ''.join(result)