Skip to content

Commit b4da00c

Browse files
author
IanDoarn
committed
Unittests and other stuff
1 parent 06e1b29 commit b4da00c

File tree

13 files changed

+135
-7
lines changed

13 files changed

+135
-7
lines changed

pygorithm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
'math',
6969
'searching',
7070
'sorting',
71-
'string',
71+
'strings',
7272
'pathfinding'
7373
'geometry',
7474
'greedy_algorithm'

pygorithm/binary/ascii.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
Author: Ian Doarn
99
"""
10+
from pygorithm.binary.binary_utils import pad
1011
from pygorithm.binary.base10 import to_base2 as b10_to_b2
1112
from pygorithm.binary.base2 import to_base16 as b2_to_b16, \
1213
to_ascii as b2_to_ascii
@@ -47,7 +48,8 @@ def to_base2(string, visualize=False, as_string=False):
4748
x, str(ord(x)),
4849
str(b10_to_b2(ord(x)))
4950
))
50-
_list.append(str(b10_to_b2(ord(x))))
51+
value = pad(str(b10_to_b2(ord(x))))
52+
_list.append(value)
5153

5254
if as_string:
5355
return ' '.join(_list)

pygorithm/binary/base10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def to_base16(n, visualize=False):
6262
print("{} % 16 = {} -> hex = {}".format(
6363
str(n), str(n % 16), HEX_VALUES[n % 16]
6464
))
65-
_list.append(HEX_VALUES[n % 16])
65+
_list.append(HEX_VALUES[n % 16])
6666
n = int(n / 16)
6767

6868
if visualize:

pygorithm/binary/base16.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Author: Ian Doarn
1010
"""
1111
from pygorithm.binary.base2 import to_ascii as b2_to_ascii
12+
from pygorithm.binary.binary_utils import pad
1213
from math import pow
1314

1415
HEX_BINARY_VALUES = {
@@ -45,7 +46,7 @@ def to_base2(h, visualize=False):
4546
print("{} -> {}".format(
4647
value, HEX_BINARY_VALUES[value]
4748
))
48-
_list.append(HEX_BINARY_VALUES[value])
49+
_list.append(pad(HEX_BINARY_VALUES[value]))
4950

5051
return int(''.join(_list))
5152

pygorithm/binary/binary_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Helper methods for binary package
3+
Author: Ian Doarn
4+
"""
5+
6+
7+
def pad(value: str, return_type=str) -> """Pad binary value with zeros""":
8+
if len(value) % 4 != 0:
9+
pad_amount = 4 - (len(value) % 4)
10+
return return_type(('0' * pad_amount) + value)
11+
else:
12+
return return_type(value)
13+
14+
15+
def to_string(binary_array: list, delimiter=' ') -> """Convert binary array to string""":
16+
return delimiter.join(binary_array)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)