Skip to content
Open
Changes from all 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
39 changes: 39 additions & 0 deletions bitarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class bitarray:

def __init__(self, size):
self.arr = [0] * size

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You should add a__str__ and __repr__ as a good practice. https://docs.python.org/3.8/reference/datamodel.html#object.__str__


def setall(self, bit):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Knit pick. Remove these empty lines.

if bit == 0 or bit == 1:
for i in range(len(self.arr)):
self.arr[i] = bit

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since everything is the same bit, it's probably faster to just do self.arr = [bit] * len(self.arr) again.

else:
raise Exception("SetAll: Invalid bit entered, only 1 or 0 are valid inputs.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I recommend raising specific exceptions when it makes sense. This seems to be a ValueError, you can reference the builtin exceptions here: https://docs.python.org/3/library/exceptions.html#ValueError



def set(self, n, bit):

if bit == 0 or bit == 1:

try:
self.arr[n] = bit
except IndexError:
raise Exception("Set: Value entered is out of range of the array.")
except TypeError:
raise Exception("Set: Enter a value that is an integer")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Forgot to add generic exceptions here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's better to just re-raise the same type of exceptions here if you want to add this information. You should also specify which value was incorrect as it's not clear what exactly went wrong from the message alone.


else:
raise Exception("Set: Invalid bit entered, only 1 or 0 are valid inputs.")



def get(self, n):

try:
return self.arr[n]
except IndexError:
raise Exception("Get: Value entered is out of range of the array.")
except TypeError:
raise Exception("Get: Value entered is not an integer.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above, re-raise the same error. Also I don't think it's necessary to mention which method in the exception message. We will already know that from the stacktrace.