-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntConstructorsBases.py
More file actions
executable file
·113 lines (90 loc) · 2.23 KB
/
IntConstructorsBases.py
File metadata and controls
executable file
·113 lines (90 loc) · 2.23 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 10:11:49 2021
@author: maherme
"""
#%%
# For documentation about class int you can watch help(int)
# It is not neede to pass a integer number to the constructor of the int class
print(int(10.5))
print(int(10.99999))
print(int(True))
print(int(False))
import fractions
a = fractions.Fraction(22, 7)
print(a)
print(float(a))
print(int(a))
#%%
# The default base is 10 in the constructor:
print(int("12345"))
print(int("101", 2))
print(int("FF", 16))
#%%
# We can use built-in representation:
print(bin(10))
print(bin(5))
print(oct(10))
print(hex(255))
a = int('101', 2)
b = 0b101
print(a)
print(b)
#%%
# Let's see custom base conversion:
def from_base10(n, b):
if n < 2:
raise ValueError('Base b must be >= 2')
if n < 0:
raise ValueError('Number n must be >= 0')
if n == 0:
return[0]
digits = []
while n > 0:
n, m = divmod(n, b)
digits.insert(0, m)
return digits
print(from_base10(10, 2))
print(from_base10(255, 16))
#%%
# Let's do the encoding:
def encode(digits, digit_map):
if max(digits) >= len(digit_map):
raise ValueError("digit_map is not long enough to encode the digits")
# encoding = ''
# for d in digits:
# encoding += digit_map[d]
# return encoding
return ''.join([digit_map[d] for d in digits])
print(encode([15, 15], '0123456789ABCDEF'))
#%%
# We can combine the two functions above:
def rebase_from10(number, base):
digit_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if base < 2 or base > 36:
raise ValueError('Invalid base: 2 <= base <= 36')
sign = -1 if number < 0 else 1
number *= sign
digits = from_base10(number, base)
encoding = encode(digits, digit_map)
if sign == -1:
encoding = '-' + encoding
return encoding
e = rebase_from10(314, 2)
print('***** number 314 base 2 *****')
print(e)
print(int(e, base=2))
e = rebase_from10(-314, 2)
print('***** number -314 base 2 *****')
print(e)
print(int(e, base=2))
e = rebase_from10(3451, 16)
print('***** number 3451 base 16 *****')
print(e)
print(int(e, base=16))
e = rebase_from10(-3451, 16)
print('***** number -3451 base 16 *****')
print(e)
print(int(e, base=16))
#%%