-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14632152.py
57 lines (47 loc) · 1.72 KB
/
14632152.py
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
from argparse import ArgumentParser
def es_negativo(base, representacion):
if base % 2 == 0:
return representacion[0] >= base / 2
numeros = representacion[::-1]
while numeros:
actual = numeros.pop()
if actual < base // 2:
return False
if actual > base // 2:
return True
return False
def decimal(base, representacion):
res = 0
for i in range(len(representacion) - 1, -1, -1):
res += representacion[i] * base ** (len(representacion) - i - 1)
return res
def suma_carry(base, representacion, sumando):
resultado = representacion[:]
for i in range(len(representacion) - 1, -1, -1):
if representacion[i] + sumando < base:
resultado[i] = representacion[i] + sumando
break
resultado[i] = 0
return resultado
def inverso_aditivo(base, representacion):
inverso = []
rep = [0] + representacion[:]
comp = [base - i - 1 for i in rep][1:]
inverso = suma_carry(base, comp, 1)
if es_negativo(base, representacion):
numero = -decimal(base, inverso)
else:
numero = decimal(base, representacion)
return numero, inverso
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('path')
args = parser.parse_args()
with open(args.path) as file:
for line in file:
things = [int(i) for i in line.strip().split(',')]
base, rep = things[0], things[1:]
n, inv = inverso_aditivo(base, rep)
rep_string = ", ".join([str(i) for i in rep])
inv_string = ", ".join([str(i) for i in inv])
print(rep_string, '(%d) ==>' % n, inv_string)