-
Notifications
You must be signed in to change notification settings - Fork 0
/
bases.py
163 lines (134 loc) · 5.02 KB
/
bases.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
152
153
154
155
156
157
158
159
160
161
162
163
#!python
import string
# Hint: Use these string constants to encode/decode hexadecimal digits and more
# string.digits is '0123456789'
# string.hexdigits is '0123456789abcdefABCDEF'
# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'
# string.ascii_uppercase is 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# string.ascii_letters is ascii_lowercase + ascii_uppercase
# string.printable is digits + ascii_letters + punctuation + whitespace
def decode(digits, base):
"""Decode given digits in given base to number in base 10.
digits: str -- string representation of number (in given base)
base: int -- base of given number
return: int -- integer representation of number (in base 10)"""
# Handle up to base 36 [0-9a-z]
assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
# PSEUDO BRAINSTORM
# map that starts out adding 1, 10, 100, etc. based on binary values
# map function that takes in the length of digits and generates a map then subtracts from total
# var romanNumeralDict map[int]string = map[int]string{
# 1000: "M",
# 900: "CM",
# 500: "D",
# 400: "CD",
# 100: "C",
# 90: "XC",
# 50: "L",
# 40: "XL",
# 10: "X",
# 9: "IX",
# 5: "V",
# 4: "IV",
# 1: "I",
# }
# test = map(lambda x: 2 ** x)
# PSEUDO 2
# for length of digits, becomes 2 to the power of n - 1
# add total values if value at position = 1
# n - 1
# total = 0
# for bit in len(digits):
# if digits[bit] == 1:
# total += digits ** (bit - 1)
# else:
# continue
# TODO: Decode digits from binary (base 2)
# total = 0
# for bit in range(0, len(digits)):
# # if digits in string.digits:
# print(digits[bit])
# if digits[bit] == '1':
# total += base ** int(digits[bit])
# else:
# continue
# return total
# TODO: Decode digits from hexadecimal (base 16)
# PSEUDO BRAINSTORM:
# 110
total = 0
power = len(digits) - 1
for character in digits:
total += string.printable.index(character) * (base ** power)
power -= 1
return total
# TODO: Decode digits from any base (2 up to 36)
# ...
def encode(number, base):
"""Encode given number in base 10 to digits in given base.
number: int -- integer representation of number (in base 10)
base: int -- base to convert to
return: str -- string representation of number (in given base)"""
# Handle up to base 36 [0-9a-z]
assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
# Handle unsigned numbers only for now
assert number >= 0, 'number is negative: {}'.format(number)
# TODO: Encode number in binary (base 2)
# PSEUDO BRAINSTORM
#
# TODO: Encode number in hexadecimal (base 16)
# PSEUDO BRAINSTORM
# divide the decimal number by 16 repeatedly.
# write the last remainder you obtained in the hex equivalent column
# If the remainder is more than nine, remember to change it to its hex letter equivalent.
# Return the remainders in reverse order
result = number
# remainder = digits % 16
tmp_remainder = []
tmp_decimal = ""
while result > 0:
remainder = number % base
tmp_remainder.append(remainder)
result = int(number / base)
# tmp_decimal.append(string.printable[remainder])
tmp_decimal += string.printable[remainder]
print('LOOK HERE STUPID', tmp_decimal)
number = result
return tmp_decimal[::-1]
# TODO: Encode number in any base (2 up to 36)
# ...
def convert(digits, base1, base2):
"""Convert given digits in base1 to digits in base2.
digits: str -- string representation of number (in base1)
base1: int -- base of given number
base2: int -- base to convert to
return: str -- string representation of number (in base2)"""
# Handle up to base 36 [0-9a-z]
assert 2 <= base1 <= 36, 'base1 is out of range: {}'.format(base1)
assert 2 <= base2 <= 36, 'base2 is out of range: {}'.format(base2)
# TODO: Convert digits from base 2 to base 16 (and vice versa)
# ...
# TODO: Convert digits from base 2 to base 10 (and vice versa)
# ...
# TODO: Convert digits from base 10 to base 16 (and vice versa)
# ...
# TODO: Convert digits from any base to any base (2 up to 36)
# test = decode(digits, base1)
# return encode(test, base2)
return encode(decode(digits, base1), base2)
def main():
"""Read command-line arguments and convert given digits between bases."""
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) == 3:
digits = args[0]
base1 = int(args[1])
base2 = int(args[2])
# Convert given digits between bases
result = convert(digits, base1, base2)
print('{} in base {} is {} in base {}'.format(digits, base1, result, base2))
else:
print('Usage: {} digits base1 base2'.format(sys.argv[0]))
print('Converts digits from base1 to base2')
if __name__ == '__main__':
main()