Skip to content

Added binery_or_operator.py to bit manipulation file #2331

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 8 commits into from
Aug 27, 2020
Merged
Changes from 2 commits
Commits
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
72 changes: 72 additions & 0 deletions bit_manipulation/binary_or_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
"""
https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
This function takes in 2 integer, convert them to binary and returns a binary
in str format that is the result of an Binary OR operation from the 2 integer
input.

Returns a binary resulted from 2 integer input in str format
>>> Binary_OR_Operator(25,32)
'0b111001'

>>> Binary_OR_Operator(37,50)
'0b110111'

>>> Binary_OR_Operator(21,30)
'0b11111'

>>> Binary_OR_Operator(58,73)
'0b1111011'
"""


def Binary_OR_Operator (a : int, b : int):

if type(a) == float or type(b) == float:
Copy link
Member

Choose a reason for hiding this comment

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

Use isinstance() instead of directly comparing types as discussed in PEP8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright noted, thanks

raise TypeError("'Float' object cannot be implemented as an integer")
if type(a) == str or type(b) == str:
raise TypeError("'str' object cannot be implemented as an integer")
if a <0 or b < 0:
raise ValueError("the value of both input must be positive")
"""
a_binary and b_binary are the binary of a and b in str format
"""
a_binary = convert_to_binary(a)
b_binary = convert_to_binary(b)
binary = []
if len(a_binary) > len(b_binary):
greater = len(a_binary)
for i in range(greater - len(b_binary)):
b_binary= "0" + b_binary
else:
greater = len(b_binary)
for i in range(greater - len(a_binary)):
a_binary= "0" + a_binary
for i in range(greater):
Copy link
Member

Choose a reason for hiding this comment

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

Nice! So how could we use zip() https://docs.python.org/3/library/functions.html#zip

    for char_a, char_b in zip(a_binary, b_binary):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably just swapping out the for loop for that
for char_a, char_b in zip(a_binary, b_binary):
if char_a == "1" or char_b == "1":
binary.append("1")
else:
binary.append("0")
I'm not entirely sure if there is a simpler way for it or not

if a_binary[i] == "1" or b_binary[i] == "1":
binary.append("1")
else:
binary.append("0")
return "0b" + "".join(binary)


"""
The function below converts the integer input from decimal to binary and
returns the binary in str format
"""
def convert_to_binary(num: int)-> str:
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well i saw the empty file there so i thought it wanted somthing like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well i can i can remove the extra function for that as well

Copy link
Member

Choose a reason for hiding this comment

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

You can leave the function in if you want but just make sure there are doctests which demonstrate that it produces the same results as bin() for positive ints, zero, negative ints, floats, str, True, and None.


first = True
while num > 0:
if first:
binarynumber = str(num%2)
first = False
else:
binarynumber = str(num%2) + binarynumber
num = int(num / 2)
return binarynumber

if __name__ == "__main__":
import doctest

doctest.testmod()