From 3d649cab783b9472340df3b6e9452c2042fc31cc Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Thu, 9 Feb 2023 00:13:14 -0800 Subject: [PATCH 1/4] hacktoberfest contribution - binary to hexadecimal --- easyPythonpi/TestBin2Hex.py | 97 ++++++++++++++++++++++++++++++++++++ easyPythonpi/easyPythonpi.py | 74 ++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 easyPythonpi/TestBin2Hex.py diff --git a/easyPythonpi/TestBin2Hex.py b/easyPythonpi/TestBin2Hex.py new file mode 100644 index 0000000..7e13cd2 --- /dev/null +++ b/easyPythonpi/TestBin2Hex.py @@ -0,0 +1,97 @@ +import unittest +import easyPythonpi + +class TestBin2Hex(unittest.TestCase): + + def test_single_binary_zero(self): + self.assertEqual( easyPythonpi.bin2hex('0'), '0') + + def test_single_binary_one(self): + self.assertEqual( easyPythonpi.bin2hex('1'), '1') + + def test_binary_two_zeros(self): + self.assertEqual( easyPythonpi.bin2hex('00'), '0') + + def test_binary_three_zeros(self): + self.assertEqual( easyPythonpi.bin2hex('000'), '0') + + def test_binary_four_zeros(self): + self.assertEqual( easyPythonpi.bin2hex('0000'), '0') + + def test_binary_1111_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1111'), 'F') + + def test_binary_1110_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1110'), 'E') + + def test_binary_1101_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1101'), 'D') + + def test_binary_1100_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1100'), 'C') + + def test_binary_1011_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1011'), 'B') + + def test_binary_1010_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1010'), 'A') + + def test_binary_1001_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1001'), '9') + + def test_binary_1000_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('1000'), '8') + + def test_binary_0111_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0111'), '7') + + def test_binary_111_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('111'), '7') + + def test_binary_0110_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0110'), '6') + + def test_binary_110_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('110'), '6') + + def test_binary_0101_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0101'), '5') + + def test_binary_101_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('101'), '5') + + def test_binary_0100_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0100'), '4') + + def test_binary_100_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('100'), '4') + + def test_binary_0011_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0011'), '3') + + def test_binary_011_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('011'), '3') + + def test_binary_11_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('11'), '3') + + def test_binary_0010_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0010'), '2') + + def test_binary_010_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('010'), '2') + + def test_binary_10_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('10'), '2') + + def test_binary_01_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('01'), '1') + + def test_binary_110100001111_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('110100001111'), 'D0F') + + def test_binary_0100011111_to_hex(self): + self.assertEqual( easyPythonpi.bin2hex('0100011111'), '11F') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index b226610..92234d8 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -128,7 +128,79 @@ def bin2dec(x:'bin')->'dec': a=a+1 return r - + +# A function that converts a binary string to hexadecimal +def bin2hex(x:'bin')->'hex': + """Converts a binary string to a hexadecimal string. + + This function converts a binary string to hexadecimal. If the binary + string's length is a multiple of 4, simply break up the string into + substrings of 4 binary digits and determine their hexadecimal digit. + If the binary string's length not a multiple of 4 prepend the + necessary number of 0's to make the string a multiple of 4. + + Args: + x ('bin') : Binary string to convert to hexadecimal. + + Returns: + The binary converted to hexadecimal. + + Raises: + No errors raised. + """ + + h = '' # hexadecimal number converted from binary and to be returned + + # Get the length of the string + l=len(x) + + # If the length is not a multiple of 4, prepend 0's before converting + if l % 4 != 0: + numZerosPrepended = 4 - ( l % 4 ) # number of zeros to prepend + x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x + + # Begin the process of converting x to its hexadecimal number + x=str(x) + for i in range(len(x)-1, 0, -4): + substring = x[i-3:i+1] # The substring converted to a hex character + + # Determine the binary substring's hex and prepend to h + if substring == '0000': + h = '0' + h + elif substring == '0001': + h = '1' + h + elif substring == '0010': + h = '2' + h + elif substring == '0011': + h = '3' + h + elif substring == '0100': + h = '4' + h + elif substring == '0101': + h = '5' + h + elif substring == '0110': + h = '6' + h + elif substring == '0111': + h = '7' + h + elif substring == '1000': + h = '8' + h + elif substring == '1001': + h = '9' + h + elif substring == '1010': + h = 'A' + h + elif substring == '1011': + h = 'B' + h + elif substring == '1100': + h = 'C' + h + elif substring == '1100': + h = 'C' + h + elif substring == '1101': + h = 'D' + h + elif substring == '1110': + h = 'E' + h + elif substring == '1111': + h = 'F' + h + + return h 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) import numpy as np From 082595089d3b9d0a80a577353774f149926950da Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Sat, 11 Feb 2023 23:24:46 -0800 Subject: [PATCH 2/4] Adding regexp to detect invalid expressions and unit test cases --- easyPythonpi/TestBin2Hex.py | 45 +++++++++++++++++++++++++++++++++++- easyPythonpi/easyPythonpi.py | 21 ++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/easyPythonpi/TestBin2Hex.py b/easyPythonpi/TestBin2Hex.py index 7e13cd2..191b6b0 100644 --- a/easyPythonpi/TestBin2Hex.py +++ b/easyPythonpi/TestBin2Hex.py @@ -91,7 +91,50 @@ def test_binary_110100001111_to_hex(self): self.assertEqual( easyPythonpi.bin2hex('110100001111'), 'D0F') def test_binary_0100011111_to_hex(self): - self.assertEqual( easyPythonpi.bin2hex('0100011111'), '11F') + self.assertEqual( easyPythonpi.bin2hex('0100011111'), '11F') + + def test_invalid_binary_A(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('A') + + def test_invalid_binary_123(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('123') + + def test_invalid_binary_0101A1000100(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('0101A1000100') + + def test_invalid_binary_A101100010B(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('A101100010B') + + def test_invalid_binary_nonalphanumeric(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('!') + + def test_invalid_binary_nonalphanumeric_in_binary(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('001~') + + def test_invalid_binary_subtraction(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('0000-1000') + + def test_invalid_binary_anding(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('0000&1000') + + def test_invalid_helloWorld_expression(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('hello world') + + def test_invalid_regexpression(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2hex('[0-1]') + + + # class TestBin2Octal(unittest.TestCase): if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index 92234d8..65aaa29 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -1,3 +1,4 @@ +import regex as re """ A python module that helps you to calculate some of the most used calculations..... usage-- Just download the file from git and unzip in ur system. @@ -146,21 +147,29 @@ def bin2hex(x:'bin')->'hex': The binary converted to hexadecimal. Raises: - No errors raised. + No errors """ h = '' # hexadecimal number converted from binary and to be returned + + x=str(x) + + # Determine if the string has invalid characters + if re.search('[^(0-1)]', x): + raise InvalidBinaryException + # Get the length of the string l=len(x) + # Begin the process of converting x to its hexadecimal number + # If the length is not a multiple of 4, prepend 0's before converting if l % 4 != 0: numZerosPrepended = 4 - ( l % 4 ) # number of zeros to prepend x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x + - # Begin the process of converting x to its hexadecimal number - x=str(x) for i in range(len(x)-1, 0, -4): substring = x[i-3:i+1] # The substring converted to a hex character @@ -405,3 +414,9 @@ def count_vowels(ip_str:'str')->'int': #return the count dictionary return count + +# Programmer defined exceptions go here: + +# define exception for invalid Binary Strings +class InvalidBinaryException(Exception): + pass From bf735768e135af1ce2af2b8df8fa822c2f2720a4 Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Sun, 19 Feb 2023 18:24:54 -0800 Subject: [PATCH 3/4] Added a function that converts from binary to octal and included unit tests for it --- easyPythonpi/TestBin2Hex.py | 59 +++++++++++++++++++++++++++++++++++- easyPythonpi/easyPythonpi.py | 45 ++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/easyPythonpi/TestBin2Hex.py b/easyPythonpi/TestBin2Hex.py index 191b6b0..0e575da 100644 --- a/easyPythonpi/TestBin2Hex.py +++ b/easyPythonpi/TestBin2Hex.py @@ -134,7 +134,64 @@ def test_invalid_regexpression(self): easyPythonpi.bin2hex('[0-1]') - # class TestBin2Octal(unittest.TestCase): +class TestBin2Octal(unittest.TestCase): + def test_single_binary_zero(self): + self.assertEqual( easyPythonpi.bin2oct('0'), '0') + def test_single_binary_one(self): + self.assertEqual( easyPythonpi.bin2oct('1'), '1') + def test_single_binary_triple_zero(self): + self.assertEqual( easyPythonpi.bin2oct('000'), '0') + def test_single_binary_001(self): + self.assertEqual( easyPythonpi.bin2oct('001'), '1') + def test_single_binary_010(self): + self.assertEqual( easyPythonpi.bin2oct('010'), '2') + def test_single_binary_011(self): + self.assertEqual( easyPythonpi.bin2oct('011'), '3') + def test_single_binary_100(self): + self.assertEqual( easyPythonpi.bin2oct('100'), '4') + def test_single_binary_101(self): + self.assertEqual( easyPythonpi.bin2oct('101'), '5') + def test_single_binary_110(self): + self.assertEqual( easyPythonpi.bin2oct('110'), '6') + def test_single_binary_111(self): + self.assertEqual( easyPythonpi.bin2oct('111'), '7') + def test_single_binary_000010(self): + self.assertEqual( easyPythonpi.bin2oct('000010'), '02') + def test_single_binary_00010(self): + self.assertEqual( easyPythonpi.bin2oct('00010'), '02') + def test_invalid_binary_A(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('A') + def test_invalid_binary_123(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('123') + def test_invalid_binary_0101A1000100(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('0101A1000100') + def test_invalid_binary_A101100010B(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('A101100010B') + def test_invalid_binary_nonalphanumeric(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('!') + def test_invalid_binary_nonalphanumeric_in_binary(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('001~') + def test_invalid_binary_subtraction(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('0000-1000') + def test_invalid_binary_anding(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('0000&1000') + def test_invalid_helloWorld_expression(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('hello world') + def test_invalid_regexpression(self): + with self.assertRaises(easyPythonpi.InvalidBinaryException): + easyPythonpi.bin2oct('[0-1]') + + + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index 65aaa29..8287b81 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -209,7 +209,50 @@ def bin2hex(x:'bin')->'hex': elif substring == '1111': h = 'F' + h - return h + return h + + +def bin2oct(x:'bin')->'oct': + o = '' # hexadecimal number converted from binary and to be returned + + x=str(x) + + # Determine if the string has invalid characters + if re.search('[^(0-1)]', x): + raise InvalidBinaryException + + # Get the length of the string + l=len(x) + + # Begin the process of converting x to its hexadecimal number + + # If the length is not a multiple of 3, prepend 0's before converting + if l % 3 != 0: + numZerosPrepended = 3 - ( l % 3 ) # number of zeros to prepend + x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x + + for i in range(len(x), 0, -3): + substring = x[i-3:i] + + if substring == '000': + o = '0' + o + elif substring == '001': + o = '1' + o + elif substring == '010': + o = '2' + o + elif substring == '011': + o = '3' + o + elif substring == '100': + o = '4' + o + elif substring == '101': + o = '5' + o + elif substring == '110': + o = '6' + o + elif substring == '111': + o = '7' + o + + return o + 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) import numpy as np From f47c5127331ed43da402443421ecb646fd67217f Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Sun, 4 Jun 2023 18:07:25 -0700 Subject: [PATCH 4/4] Refactored the Fibonacci function to use dynamic programming --- easyPythonpi/TestFibRefactored.py | 46 ++++++++++++++++++ .../__pycache__/easyPythonpi.cpython-311.pyc | Bin 0 -> 17928 bytes easyPythonpi/easyPythonpi.py | 36 ++++++++++++-- 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 easyPythonpi/TestFibRefactored.py create mode 100644 easyPythonpi/__pycache__/easyPythonpi.cpython-311.pyc diff --git a/easyPythonpi/TestFibRefactored.py b/easyPythonpi/TestFibRefactored.py new file mode 100644 index 0000000..41baa62 --- /dev/null +++ b/easyPythonpi/TestFibRefactored.py @@ -0,0 +1,46 @@ +import unittest +import easyPythonpi +from easyPythonpi import InvalidNumberFibException + +class TestFibRefactored(unittest.TestCase): + + def test_fibonacci_0(self): + with self.assertRaises(easyPythonpi.InvalidNumberFibException): + easyPythonpi.fibonacci(0) + + def test_fibonacci_negative_number(self): + with self.assertRaises(easyPythonpi.InvalidNumberFibException): + easyPythonpi.fibonacci(-10) + + def test_fibonacci_1(self): + self.assertEqual( easyPythonpi.fibonacci(1), 0) + + def test_fibonacci_2(self): + self.assertEqual( easyPythonpi.fibonacci(2), 1) + + def test_fibonacci_3(self): + self.assertEqual( easyPythonpi.fibonacci(3), 1) + + def test_fibonacci_4(self): + self.assertEqual( easyPythonpi.fibonacci(4), 2) + + def test_fibonacci_5(self): + self.assertEqual( easyPythonpi.fibonacci(5), 3) + + def test_fibonacci_6(self): + self.assertEqual( easyPythonpi.fibonacci(6), 5) + + def test_fibonacci_7(self): + self.assertEqual( easyPythonpi.fibonacci(7), 8) + + def test_fibonacci_8(self): + self.assertEqual( easyPythonpi.fibonacci(8), 13) + + def test_fibonacci_9(self): + self.assertEqual( easyPythonpi.fibonacci(9), 21) + + def test_fibonacci_10(self): + self.assertEqual( easyPythonpi.fibonacci(10), 34) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/easyPythonpi/__pycache__/easyPythonpi.cpython-311.pyc b/easyPythonpi/__pycache__/easyPythonpi.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0619a6acbd41d2bdf93d7ffd77d06b66bc1341a2 GIT binary patch literal 17928 zcmeHO3v65Gb-tHxilij!Wm&dl>S4)p9NY3ku^m4`$IsYS?bvzPd8jn+wM|tki;S3gyv-yGz<9HRA4;_DjvUd!(nNCAe2h2c%ZqYovqHGTdvWLy`}7uQVXF<6b9y zMmj8YU9q01mySrwfoYJQmU?h+l#W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QXIH9MZ@>{|YE_sYS~so|L14Pjv)pzgeTm_7>{3D8$1 zO5PEU401%?5$@ZvRXoYh5bD$`3stU&2p*Vm^gT^#+%r8uNK1CWbNSq zW-g6wqUmCZu8jaY93chGK`ua3i?pbJ9M{Pqd?rg)7xyK= zi{%Ig6nwulB;_5a;D9|Rk0}tLIh4rTMkBAtLkjyA75_VcvgviJJ_fDS&ro2NCN_u! zzLE4aB=+3%)L$FCI+*pe&3f7rd*DIv)Lc7o^+3j!xsY|Y&$`E&W2{{JKvDTB&~!u0#{N2;=}9 z12psO>jZ8SAl~9t@%73}aX?>)DmBPV=zlqjBe6>@OO??$^{ZgW-fse`q-~32x+20m z$>UJjYGi)AE=;jx6t2ZTmRg0NG9^mx-P8QQiXmDn5ca_(jBnx)-G&&bz94Z`;i9HE zmvtaf?z;ppEa1CbgwilpDg5iSjCuGyJgZ^xKS#>oJ>t&9@s0(*=F;7l|3;Ifxi9gt zM? zZ&8mro}heQgP-2gu+Pqmr4nk{6D4Jf*eB-2QeBu^Z7_5?TGAELi_j?DRKUBUVTpIt z6B>>^N@Z2ocA>T#RDutYmw1CpSX93$$CC53Q1*RF-yy)u^8VOVhZ1l8dz8gf#Xi{7 z4V7bmGEcsy!WG}rruN74mZfc>d_1ZIE5Y)9Eyq{S@@WLPjnXS_0<{FlpyHpp^Uu=N zPrlm;*Gpg(fpr8n6WBrE83M-%yiDL#0u~j_|*v|eLk0O4G74)4mi=vnl z>ShZ6b0({}ZmwDsYoQDnfX(>GbN^d7lei7b)&Q!kB8(p+s5Xg+i!lO+Nu)j40IE7f z7(_;}Pqc}!%#5JUBYNfpBWP'float': # To find the perimeter of a circle us PI = 3.142 return 2 * PI * r + def fibonacci(n:'int')->'int': #To find the nth fibonacci series - if n<0: - print("Incorrect input") + """Finds the fibonacci of the nth sequence. + + This function calculates the fibonacci sequence. This function calculates + the nth fibonacci of a number by finding the sum of two numbers in the + fibonacci sequence before n + + Args: + n ('int') : The number to find the fibonacci sequence of, assumes n >=1 + as valid numbers to find the fibonacci of n. + + Returns: + The fibonacci of arg n. + + Raises: + No errors + """ + if n<=0: + raise InvalidNumberFibException(n) # First Fibonacci number is 0 elif n==1: return 0 @@ -51,7 +68,15 @@ def fibonacci(n:'int')->'int': #To find the nth fibonacci series elif n==2: return 1 else: - return fibonacci(n-1)+fibonacci(n-2) + fib1 = 0 + fib2 = 1 + + for i in range(2,n-1): + fibN = fib1 + fib2 + fib1 = fib2 + fib2 = fibN + + return fib1 +fib2 def sort(list:'list'): # To bubble sort and array or list for i in range(len(list) - 1, 0, -1): @@ -463,3 +488,8 @@ def count_vowels(ip_str:'str')->'int': # define exception for invalid Binary Strings class InvalidBinaryException(Exception): pass + +class InvalidNumberFibException(Exception): + def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): + self.n = n + self.message = message \ No newline at end of file