Skip to content

Commit 77ae03d

Browse files
Andrew hung nguyen (#32)
* hacktoberfest contribution - binary to hexadecimal * Adding regexp to detect invalid expressions and unit test cases * Added a function that converts from binary to octal and included unit tests for it
1 parent 5a8670a commit 77ae03d

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

easyPythonpi/TestBin2Hex.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,62 @@ def test_invalid_regexpression(self):
133133
with self.assertRaises(easyPythonpi.InvalidBinaryException):
134134
easyPythonpi.bin2hex('[0-1]')
135135

136-
137-
# class TestBin2Octal(unittest.TestCase):
136+
137+
class TestBin2Octal(unittest.TestCase):
138+
def test_single_binary_zero(self):
139+
self.assertEqual( easyPythonpi.bin2oct('0'), '0')
140+
def test_single_binary_one(self):
141+
self.assertEqual( easyPythonpi.bin2oct('1'), '1')
142+
def test_single_binary_triple_zero(self):
143+
self.assertEqual( easyPythonpi.bin2oct('000'), '0')
144+
def test_single_binary_001(self):
145+
self.assertEqual( easyPythonpi.bin2oct('001'), '1')
146+
def test_single_binary_010(self):
147+
self.assertEqual( easyPythonpi.bin2oct('010'), '2')
148+
def test_single_binary_011(self):
149+
self.assertEqual( easyPythonpi.bin2oct('011'), '3')
150+
def test_single_binary_100(self):
151+
self.assertEqual( easyPythonpi.bin2oct('100'), '4')
152+
def test_single_binary_101(self):
153+
self.assertEqual( easyPythonpi.bin2oct('101'), '5')
154+
def test_single_binary_110(self):
155+
self.assertEqual( easyPythonpi.bin2oct('110'), '6')
156+
def test_single_binary_111(self):
157+
self.assertEqual( easyPythonpi.bin2oct('111'), '7')
158+
def test_single_binary_000010(self):
159+
self.assertEqual( easyPythonpi.bin2oct('000010'), '02')
160+
def test_single_binary_00010(self):
161+
self.assertEqual( easyPythonpi.bin2oct('00010'), '02')
162+
def test_invalid_binary_A(self):
163+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
164+
easyPythonpi.bin2oct('A')
165+
def test_invalid_binary_123(self):
166+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
167+
easyPythonpi.bin2oct('123')
168+
def test_invalid_binary_0101A1000100(self):
169+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
170+
easyPythonpi.bin2oct('0101A1000100')
171+
def test_invalid_binary_A101100010B(self):
172+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
173+
easyPythonpi.bin2oct('A101100010B')
174+
def test_invalid_binary_nonalphanumeric(self):
175+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
176+
easyPythonpi.bin2oct('!')
177+
def test_invalid_binary_nonalphanumeric_in_binary(self):
178+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
179+
easyPythonpi.bin2oct('001~')
180+
def test_invalid_binary_subtraction(self):
181+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
182+
easyPythonpi.bin2oct('0000-1000')
183+
def test_invalid_binary_anding(self):
184+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
185+
easyPythonpi.bin2oct('0000&1000')
186+
def test_invalid_helloWorld_expression(self):
187+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
188+
easyPythonpi.bin2oct('hello world')
189+
def test_invalid_regexpression(self):
190+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
191+
easyPythonpi.bin2oct('[0-1]')
138192

139193
if __name__ == '__main__':
140194
unittest.main()

easyPythonpi/easyPythonpi.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,50 @@ def bin2hex(x:'bin')->'hex':
208208
h = 'E' + h
209209
elif substring == '1111':
210210
h = 'F' + h
211+
212+
return h
213+
214+
215+
def bin2oct(x:'bin')->'oct':
216+
o = '' # hexadecimal number converted from binary and to be returned
217+
218+
x=str(x)
219+
220+
# Determine if the string has invalid characters
221+
if re.search('[^(0-1)]', x):
222+
raise InvalidBinaryException
223+
224+
# Get the length of the string
225+
l=len(x)
226+
227+
# Begin the process of converting x to its hexadecimal number
228+
229+
# If the length is not a multiple of 3, prepend 0's before converting
230+
if l % 3 != 0:
231+
numZerosPrepended = 3 - ( l % 3 ) # number of zeros to prepend
232+
x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x
211233

212-
return h
234+
for i in range(len(x), 0, -3):
235+
substring = x[i-3:i]
236+
237+
if substring == '000':
238+
o = '0' + o
239+
elif substring == '001':
240+
o = '1' + o
241+
elif substring == '010':
242+
o = '2' + o
243+
elif substring == '011':
244+
o = '3' + o
245+
elif substring == '100':
246+
o = '4' + o
247+
elif substring == '101':
248+
o = '5' + o
249+
elif substring == '110':
250+
o = '6' + o
251+
elif substring == '111':
252+
o = '7' + o
253+
254+
return o
213255

214256
def createarray(length:'int',dtype='int')->'array': # To create an array of entered length and entered data type(interger data type is a default data type)
215257
import numpy as np

0 commit comments

Comments
 (0)