Skip to content

Commit 0c946b9

Browse files
authored
Merge pull request #49 from IanDoarn/master
Added binary algorithms
2 parents f0a2afc + 53c4ed0 commit 0c946b9

File tree

16 files changed

+498
-15
lines changed

16 files changed

+498
-15
lines changed

pygorithm/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@
6060
]
6161

6262
__all__ = [
63+
'binary',
6364
'data_structures',
6465
'fibonacci',
6566
'math',
6667
'searching',
6768
'sorting',
6869
'string',
69-
'pathfinding',
70+
'pathfinding'
71+
'geometry'
7072
]

pygorithm/binary/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Collection or binary conversions and algorithms
3+
"""
4+
from . import ascii
5+
from . import base2
6+
from . import base10
7+
from . import base16
8+
9+
__all__ = [
10+
'ascii',
11+
'base2',
12+
'base10',
13+
'base16'
14+
]

pygorithm/binary/ascii.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
ASCII
3+
4+
Conversions from ASCII to:
5+
- base2
6+
- base16
7+
8+
Author: Ian Doarn
9+
"""
10+
from pygorithm.binary.base10 import to_base2 as b10_to_b2
11+
from pygorithm.binary.base2 import to_base16 as b2_to_b16, \
12+
to_ascii as b2_to_ascii
13+
14+
15+
def to_base16(string, visualize=False):
16+
"""
17+
Convert ascii to hexadecimal
18+
:param string: string to convert
19+
:param visualize: Show process
20+
:param as_string: return value as string not array
21+
:return: hex representation of given string
22+
"""
23+
hex_array = []
24+
25+
for b_value in to_base2(string):
26+
if visualize:
27+
print("{} -> {}".format(
28+
b2_to_ascii(b_value), b2_to_b16(b_value)
29+
))
30+
hex_array.append(b2_to_b16(b_value))
31+
32+
return hex_array
33+
34+
35+
def to_base2(string, visualize=False, as_string=False):
36+
"""
37+
Convert ascii string to binary
38+
:param string: Ascii string
39+
:param visualize: Show process
40+
:param as_string: join strings with a space as one large value
41+
:return: array of binary numbers, or entire string
42+
"""
43+
_list = []
44+
for x in string:
45+
if visualize:
46+
print("{} -> {} -> {}".format(
47+
x, str(ord(x)),
48+
str(b10_to_b2(ord(x)))
49+
))
50+
_list.append(str(b10_to_b2(ord(x))))
51+
52+
if as_string:
53+
return ' '.join(_list)
54+
return _list

pygorithm/binary/base10.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Binary: Base10
3+
4+
Conversions from base10 to:
5+
- base2
6+
- base16
7+
8+
Author: Ian Doarn
9+
"""
10+
HEX_VALUES = {
11+
0: '0', 1: '1', 2: '2',
12+
3: '3', 4: '4', 5: '5',
13+
6: '6', 7: '7', 8: '8',
14+
9: '9', 10: 'A', 11: 'B',
15+
12: 'C', 13: 'D', 14: 'E',
16+
15: 'F'
17+
}
18+
19+
20+
def to_base2(n, visualize=False):
21+
"""
22+
Divide each number by 2 using
23+
the % operator.
24+
25+
Reverse the resulting list of numbers
26+
and return the result
27+
28+
:param n: decimal number
29+
:param visualize: Show process
30+
:return: binary number
31+
"""
32+
_list = []
33+
while n > 0.5:
34+
if visualize:
35+
print("{} / 2 = {} || {} % 2 = {}".format(str(n), str(n / 2), str(n), str(n % 2)))
36+
_list.append(n % 2)
37+
n = int(n / 2)
38+
39+
# Reverse the list, turn each number to a string,
40+
# join the string, and convert it back to an integer
41+
return int(''.join([str(i) for i in _list[::-1]]))
42+
43+
44+
def to_base16(n, visualize=False):
45+
"""
46+
Convert decimal number to hexadecimal
47+
48+
Divide the number by 16 and add the remainder
49+
to a list, round down the value after division
50+
and repeat till our value is 0
51+
52+
Reverse the results list, get each values respective
53+
hex value using HEX_VALUES map
54+
55+
:param n: decimal number
56+
:param visualize: Show process
57+
:return: hexadecimal number
58+
"""
59+
_list = []
60+
while n != 0:
61+
if visualize:
62+
print("{} % 16 = {} -> hex = {}".format(
63+
str(n), str(n % 16), HEX_VALUES[n % 16]
64+
))
65+
_list.append(HEX_VALUES[n % 16])
66+
n = int(n / 16)
67+
68+
if visualize:
69+
print(_list)
70+
print("reversed = " + str(_list[::-1]))
71+
72+
return ''.join(_list[::-1])

pygorithm/binary/base16.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
Binary: Base16
3+
4+
Conversions from base16 to:
5+
- base2
6+
- base10
7+
- ASCII
8+
9+
Author: Ian Doarn
10+
"""
11+
from pygorithm.binary.base2 import to_ascii as b2_to_ascii
12+
from math import pow
13+
14+
HEX_BINARY_VALUES = {
15+
'0': '0000', '1': '0001', '2': '0010', '3': '0011',
16+
'4': '0100', '5': '0101', '6': '0110', '7': '0111',
17+
'8': '1000', '9': '1001', 'A': '1010', 'B': '1011',
18+
'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'
19+
}
20+
21+
HEX_LETTER_VALUES = {
22+
'0': 0, '1': 1, '2': 2,
23+
'3': 3, '4': 4, '5': 5,
24+
'6': 6, '7': 7, '8': 8,
25+
'9': 9, 'A': 10, 'B': 11,
26+
'C': 12, 'D': 13, 'E': 14,
27+
'F': 15
28+
}
29+
30+
31+
def to_base2(h, visualize=False):
32+
"""
33+
Convert hexadecimal to binary number
34+
35+
:param h: hexadecimal number
36+
:param visualize: Show process
37+
:return: binary number
38+
"""
39+
40+
hex_char_list = list(h)
41+
_list = []
42+
43+
for value in hex_char_list:
44+
if visualize:
45+
print("{} -> {}".format(
46+
value, HEX_BINARY_VALUES[value]
47+
))
48+
_list.append(HEX_BINARY_VALUES[value])
49+
50+
return int(''.join(_list))
51+
52+
53+
def to_base10(h, visualize=False):
54+
"""
55+
Convert hexadecimal number to decimal number
56+
57+
Send hex to a list and reverse. Evaluate each hex value
58+
via HEX_LETTER_VALUES map. Enumerate the list,
59+
60+
using the equation: value * 16 ^ index
61+
62+
value is the hex char value: F -> 15
63+
index is its position in the list: ['1', 'A', 'F'] F's index = 2
64+
65+
Continue this for each hex letter until we reach the end of the list,
66+
summing all evaluated values.
67+
68+
:param h: hexadecimal number
69+
:param visualize: Show process
70+
:return: decimal number
71+
"""
72+
73+
# Check to see if '0x' is at the beginning and remove it
74+
if h[0:2] == '0x':
75+
hex_char_list = list(h[2:])[::-1]
76+
else:
77+
hex_char_list = list(h)[::-1]
78+
79+
value = 0
80+
81+
for i, v in enumerate(hex_char_list):
82+
if visualize:
83+
print("{} -> {} || {} * (16 ^ {}) = {}".format(
84+
v, str(HEX_LETTER_VALUES[v]),
85+
str(HEX_LETTER_VALUES[v]),
86+
str(i),
87+
str(HEX_LETTER_VALUES[v] * (pow(16, i)))
88+
))
89+
90+
value += HEX_LETTER_VALUES[v] * (pow(16, i))
91+
92+
return int(value)
93+
94+
95+
def to_ascii(h_array, visualize=False):
96+
"""
97+
Convert base16 array to ASCII string
98+
99+
Input must be a list of strings:
100+
Example::
101+
102+
array = [
103+
'74', '68', '65',
104+
'20', '71', '75',
105+
'69', '63', '6B',
106+
'20', '62', '72',
107+
'6F', '77', '6E',
108+
'20', '66', '6F',
109+
'78', '20', '6A',
110+
'75', '6D', '70',
111+
'73', '20', '6F',
112+
'76', '65', '72',
113+
'20', '74', '68',
114+
'65', '20', '6C',
115+
'61', '7A', '79',
116+
'20', '64', '6F',
117+
'67'
118+
]
119+
120+
result -> the quick brown fox jumps over the lazy dog
121+
122+
:param h_array: hex value array
123+
:param visualize: Show process
124+
:return: ASCII string
125+
"""
126+
string = ''
127+
128+
for h_value in h_array:
129+
if visualize:
130+
print("{} -> {} -> {}".format(
131+
h_value,
132+
to_base2(h_value),
133+
b2_to_ascii(str(to_base2(h_value)))
134+
))
135+
136+
string += b2_to_ascii(str(to_base2(h_value)))
137+
138+
return string

0 commit comments

Comments
 (0)