-
Notifications
You must be signed in to change notification settings - Fork 0
/
int2base.py
151 lines (135 loc) · 5.48 KB
/
int2base.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from math import ceil, log
def posmod(num, den): # special modulo for int2array and int2radix
result = num % den
if result < 0: result += abs(den)
return result
################################################################################
def int2array(num, base):
if isinstance(num, complex) == True:
return [int2array(num.real, base), int2array(num.imag, base)]
assert isinstance(num, int), "Error: input not integer"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
if num == 0: return "0"
if num < 0 and base > 0: return ["-"] + int2array(-num, base)
converted = []
while num != 0:
unit = posmod(num, base)
num = (num - unit) // base
converted += [unit]
return converted[::-1]
def array2int(array, base):
assert isinstance(array, list), "Error: input not list"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
if array[0] == "-": return -1 * array2int(array[1:], base)
num = 0
for item in array:
num *= base
num += item
return num
################################################################################
def int2radix(num, base):
if isinstance(num, complex) == True:
return [int2array(num.real, base), int2array(num.imag, base)]
assert isinstance(num, int), "Error: input not integer"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
if num == 0: return "".zfill(ceiling)
if num < 0 and base > 0: return "-" + int2radix(-num, base)
ceiling = ceil(log(abs(base), 10))
result = []
x = int2array(num, base)
for i in int2array(num, base):
result += [str(i).zfill(ceiling)]
return ":".join(result)
def radix2int(string, base):
assert isinstance(string, str), "Error: input not string"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
if string[0] == "-": return -1 * radix2int(string[1:], base)
array = string.split(":")
array = [int(item) for item in array]
return array2int(array, base)
################################################################################
def int2base(num, base, alph = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'):
if isinstance(num, complex) == True:
return [int2base(num.real, base, alph), int2base(num.imag, base, alph)]
assert isinstance(num, int), "Error: input not integer"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
assert len(alph) >= base, "Error: base out of range"
assert " " not in alph, "Error: alphabet contains space"
if num == 0: return alph[0]
if num < 0 and base > 0:
if "-" not in alph: return "--" + int2base(-num, base, alph)
elif "~" not in alph: return "~~" + int2base(-num, base, alph)
else: return "negative " + int2base(-num, base, alph)
converted = ""
while num != 0:
unit = posmod(num, base)
num = (num - unit) // base
converted += alph[unit]
return converted[::-1]
def base2int(string, base, alph = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'):
assert isinstance(string, str), "Error: input not string"
assert isinstance(base, int), "Error: base not integer"
assert abs(base) >= 2, "Error: base impossible"
assert len(alph) >= base, "Error: base out of range"
assert " " not in alph, "Error: alphabet contains space"
if string[0:2] == "--" and "-" not in alph:
return -1 * base2int(string[2:], base, alph)
elif string[0:2] == "~~" and "~" not in alph:
return -1 * base2int(string[2:], base, alph)
elif string[0:9] == "negative ":
return -1 * base2int(string[9:], base, alph)
num = 0
for char in string:
num *= base
num += alph.index(char)
return num
################################################################################
def checkDubs(num, base):
if isinstance(num, complex) == True:
# return a tuple
return checkDubs(num.real, base), checkDubs(num.imag, base)
assert isinstance(num, int), "Error: not a number"
text = int2base(num, base)
revtext = text[::-1]
length = len(revtext)
last = revtext[0]
tuple = 0
while(tuple <= num):
if last != revtext[tuple]: break
tuple += 1
if last != '0' and tuple == length: return "Full num GET"
elif last != '0' and tuple == length - 1: return "Half num GET"
elif last == '0' and tuple == length - 1 and text[0] == '1': return "Full zero GET"
elif last == '0' and tuple == length - 1 and text[0] != '1': return "Half zero GET"
elif tuple > 1: return str(tuple) + "-tuple " + str(last) + "-s"
else: return "Last digit is " + str(last)
def superDubs(num):
assert isinstance(num, int), "Error: not a number"
for i in range (8, 63):
print("base" + str(i) + ": " + str(checkDubs(num, i)))
################################################################################
def checkPali(num, base):
if isinstance(num, complex) == True:
# return a tuple
return checkPali(num.real, base), checkPali(num.imag, base)
assert isinstance(num, int), "Error: not a number"
text = int2base(num, base)
revtext = text[::-1]
if text == revtext: return "full palindrome"
length = len(revtext)
for i in range(1, length-2): #Since doubles are not pali
fliptext = revtext[:-i]
revfliptext = fliptext[::-1]
if fliptext == revfliptext:
if len(fliptext) == length - 1: return "half palindrome"
else: return str(length-i) + "-tuple palindrome"
return "no palindrome"
def superPali(num):
assert isinstance(num, int), "Error: not a number"
for i in range (8, 63):
print("base" + str(i) + ": " + str(checkPali(num, i)))