You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
Copy file name to clipboardExpand all lines: easyPythonpi/easyPythonpi.py
+43-1Lines changed: 43 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -208,8 +208,50 @@ def bin2hex(x:'bin')->'hex':
208
208
h='E'+h
209
209
elifsubstring=='1111':
210
210
h='F'+h
211
+
212
+
returnh
213
+
214
+
215
+
defbin2oct(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
+
ifre.search('[^(0-1)]', x):
222
+
raiseInvalidBinaryException
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
+
ifl%3!=0:
231
+
numZerosPrepended=3- ( l%3 ) # number of zeros to prepend
232
+
x= (numZerosPrepended*'0') +x# concatenate numZerosPrepended to x
211
233
212
-
returnh
234
+
foriinrange(len(x), 0, -3):
235
+
substring=x[i-3:i]
236
+
237
+
ifsubstring=='000':
238
+
o='0'+o
239
+
elifsubstring=='001':
240
+
o='1'+o
241
+
elifsubstring=='010':
242
+
o='2'+o
243
+
elifsubstring=='011':
244
+
o='3'+o
245
+
elifsubstring=='100':
246
+
o='4'+o
247
+
elifsubstring=='101':
248
+
o='5'+o
249
+
elifsubstring=='110':
250
+
o='6'+o
251
+
elifsubstring=='111':
252
+
o='7'+o
253
+
254
+
returno
213
255
214
256
defcreatearray(length:'int',dtype='int')->'array': # To create an array of entered length and entered data type(interger data type is a default data type)
0 commit comments