Skip to content

Added a hex-bin.py file in conversion.py #4433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1f6f4c6
Added a file that converts hexa to binary
ktsrivastava29 May 17, 2021
9149d37
Added file to convert hexadecimal to binary
ktsrivastava29 May 17, 2021
71cbcbc
Update hex-bin.py
ktsrivastava29 May 17, 2021
9d4e6cb
added type hint in the code
ktsrivastava29 May 17, 2021
e82db59
Added doctest
ktsrivastava29 May 17, 2021
6549855
Added code to handle exception
ktsrivastava29 May 17, 2021
65d275b
Resolved doctest issue
ktsrivastava29 May 17, 2021
fc99340
Update hex-bin.py
ktsrivastava29 May 17, 2021
cbce9e1
Modified convert function
ktsrivastava29 May 18, 2021
e794056
Added WhiteSpace around operators.
ktsrivastava29 May 18, 2021
4a36cad
Made more pythonic
ktsrivastava29 May 18, 2021
9e212c6
removed whitespace
ktsrivastava29 May 18, 2021
63c2096
Updated doctest command
ktsrivastava29 May 18, 2021
8d5c32a
Removed whitespace
ktsrivastava29 May 19, 2021
6ba22e3
imported union
ktsrivastava29 May 19, 2021
180f602
Replaced flag with is_negative
ktsrivastava29 May 19, 2021
a5c49a1
updated return type
ktsrivastava29 May 19, 2021
95e9da1
removed pip command
ktsrivastava29 May 19, 2021
77aec40
Resolved doctest issue
ktsrivastava29 May 19, 2021
14a2d8b
Resolved doctest error
ktsrivastava29 May 19, 2021
350817e
Reformated the code
ktsrivastava29 May 19, 2021
e6abb38
Changes function name
ktsrivastava29 May 20, 2021
62a52e2
Changed exception handling statements
ktsrivastava29 May 20, 2021
c02fa6a
Update and rename hex-bin.py to hex_to_bin.py
cclauss May 20, 2021
213e22a
Update newton_method.py
cclauss May 20, 2021
e24b8a3
Update matrix_operation.py
cclauss May 20, 2021
2d9cf63
Update can_string_be_rearranged_as_palindrome.py
cclauss May 20, 2021
a80cadd
Update hex_to_bin.py
cclauss May 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions arithmetic_analysis/newton_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@


# function is the f(x) and derivative is the f'(x)
def newton(
function: RealFunc,
derivative: RealFunc,
starting_int: int,
) -> float:
def newton(function: RealFunc, derivative: RealFunc, starting_int: int,) -> float:
"""
>>> newton(lambda x: x ** 3 - 2 * x - 5, lambda x: 3 * x ** 2 - 2, 3)
2.0945514815423474
Expand Down
57 changes: 57 additions & 0 deletions conversions/hex-bin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# CONVERT HEXADECIMAL TO BINARY
import math


def hex_to_bin(hex_num: str) -> int:
"""
Convert a hexadecimal value to its decimal equivalent
#https://stackoverflow.com/questions/1425493/convert-hex-to-binary
Here, we have used " >> " bitwise operator
Bitwise right shift:
Shifts the bits of the number to the right and fills 0 on voids left as a result.
Similar effect as of dividing the number with some power of two.
Example:
a = 10
a >> 1 = 5

>>> hex_to_bin("AC")
10101100
>>> hex_to_bin("9A4")
100110100100
>>> hex_to_bin(" 12f ")
100101111
>>> hex_to_bin("FfFf")
1111111111111111
>>> hex_to_bin("F-f")
False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns an int or it raises an Exception.

>>> hex_to_bin("")
False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns an int or it raises an Exception.

"""

bin_str: str = ""
hex_str: str = hex_num.strip()

if not hex_str:
return False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below. Simply raise a ValueError instead of returning a boolean

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns an int or it raises an Exception.


is_negative: bool = hex_str[0] == "-"

if is_negative:
hex_str = hex_str[1:]
try:
int_num: int = int(hex_str, 16)
except ValueError:
return False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of implicitly silencing the error as mentioned by @cclauss please use the previous format of int_num: int = int(hex_str, 16) as this explicitly raises the error.
Sorry for the turnaround, but it makes more sense. I would have pushed this change myself but I can't due to some permisions.


while int_num > 0:
str_num: str = str(int_num % 2)
bin_str = str_num + bin_str
int_num = int_num >> 1

return int("-" + "".join(bin_str)) if is_negative else int(bin_str)


if __name__ == "__main__":
import doctest

doctest.testmod()
4 changes: 1 addition & 3 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ def main():
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(
f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",
)
print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",)
print(f"Identity: {identity(5)}\n")
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")
Expand Down
4 changes: 1 addition & 3 deletions strings/can_string_be_rearranged_as_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
# Counter is faster for long strings and non-Counter is faster for short strings.


def can_string_be_rearranged_as_palindrome_counter(
input_str: str = "",
) -> bool:
def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool:
"""
A Palindrome is a String that reads the same forward as it does backwards.
Examples of Palindromes mom, dad, malayalam
Expand Down