Skip to content
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

Raise error not string #7945

Merged
merged 15 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ extend-ignore =
# Formatting style for `black`
E203 # Whitespace before ':'
W503 # Line break occurred before a binary operator
B023 # Function definition does not bind loop variable
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions boolean_algebra/quine_mc_cluskey.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import Literal


def compare_string(string1: str, string2: str) -> str:
def compare_string(string1: str, string2: str) -> str | Literal[False]:
"""
>>> compare_string('0010','0110')
'0_10'

>>> compare_string('0110','1101')
'X'
False
"""
list1 = list(string1)
list2 = list(string2)
Expand All @@ -19,7 +20,7 @@ def compare_string(string1: str, string2: str) -> str:
count += 1
list1[i] = "_"
if count > 1:
return "X"
return False
else:
return "".join(list1)

Expand Down
2 changes: 1 addition & 1 deletion ciphers/shuffled_shift_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __str__(self) -> str:
"""
:return: passcode of the cipher object
"""
return "Passcode is: " + "".join(self.__passcode)
return "".join(self.__passcode)

def __neg_pos(self, iterlist: list[int]) -> list[int]:
"""
Expand Down
3 changes: 1 addition & 2 deletions computer_vision/harris_corner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __init__(self, k: float, window_size: int):
raise ValueError("invalid k value")

def __str__(self) -> str:

return f"Harris Corner detection with k : {self.k}"
return self.k

def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]:

Expand Down
112 changes: 56 additions & 56 deletions data_structures/binary_tree/segment_tree_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self, start, end, val, left=None, right=None):
self.left = left
self.right = right

def __str__(self):
return f"val: {self.val}, start: {self.start}, end: {self.end}"
def __repr__(self):
return f"<val={self.val} start={self.start} end={self.end}>"
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved


class SegmentTree:
Expand All @@ -27,29 +27,29 @@ class SegmentTree:
>>> for node in num_arr.traverse():
... print(node)
...
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved
val: 15, start: 0, end: 4
val: 8, start: 0, end: 2
val: 7, start: 3, end: 4
val: 3, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 1, start: 1, end: 1
<val=15 start=0 end=4>
<val=8 start=0 end=2>
<val=7 start=3 end=4>
<val=3 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=1 start=1 end=1>
>>>
>>> num_arr.update(1, 5)
>>> for node in num_arr.traverse():
... print(node)
...
val: 19, start: 0, end: 4
val: 12, start: 0, end: 2
val: 7, start: 3, end: 4
val: 7, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 5, start: 1, end: 1
<val=19 start=0 end=4>
<val=12 start=0 end=2>
<val=7 start=3 end=4>
<val=7 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=5 start=1 end=1>
>>>
>>> num_arr.query_range(3, 4)
7
Expand All @@ -62,29 +62,29 @@ class SegmentTree:
>>> for node in max_arr.traverse():
... print(node)
...
val: 5, start: 0, end: 4
val: 5, start: 0, end: 2
val: 4, start: 3, end: 4
val: 2, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 1, start: 1, end: 1
<val=5 start=0 end=4>
<val=5 start=0 end=2>
<val=4 start=3 end=4>
<val=2 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=1 start=1 end=1>
>>>
>>> max_arr.update(1, 5)
>>> for node in max_arr.traverse():
... print(node)
...
val: 5, start: 0, end: 4
val: 5, start: 0, end: 2
val: 4, start: 3, end: 4
val: 5, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 5, start: 1, end: 1
<val=5 start=0 end=4>
<val=5 start=0 end=2>
<val=4 start=3 end=4>
<val=5 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=5 start=1 end=1>
>>>
>>> max_arr.query_range(3, 4)
4
Expand All @@ -97,29 +97,29 @@ class SegmentTree:
>>> for node in min_arr.traverse():
... print(node)
...
val: 1, start: 0, end: 4
val: 1, start: 0, end: 2
val: 3, start: 3, end: 4
val: 1, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 1, start: 1, end: 1
<val=1 start=0 end=4>
<val=1 start=0 end=2>
<val=3 start=3 end=4>
<val=1 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=1 start=1 end=1>
>>>
>>> min_arr.update(1, 5)
>>> for node in min_arr.traverse():
... print(node)
...
val: 2, start: 0, end: 4
val: 2, start: 0, end: 2
val: 3, start: 3, end: 4
val: 2, start: 0, end: 1
val: 5, start: 2, end: 2
val: 3, start: 3, end: 3
val: 4, start: 4, end: 4
val: 2, start: 0, end: 0
val: 5, start: 1, end: 1
<val=2 start=0 end=4>
<val=2 start=0 end=2>
<val=3 start=3 end=4>
<val=2 start=0 end=1>
<val=5 start=2 end=2>
<val=3 start=3 end=3>
<val=4 start=4 end=4>
<val=2 start=0 end=0>
<val=5 start=1 end=1>
>>>
>>> min_arr.query_range(3, 4)
3
Expand Down
6 changes: 3 additions & 3 deletions data_structures/binary_tree/wavelet_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def __repr__(self) -> str:
"""
>>> node = Node(length=27)
>>> repr(node)
'min_value: -1, max_value: -1'
'<min_value=-1 max_value=-1>'
>>> repr(node) == str(node)
True
"""
return f"min_value: {self.minn}, max_value: {self.maxx}"
return f"<min_value={self.minn} max_value={self.maxx}>"


def build_tree(arr: list[int]) -> Node | None:
Expand All @@ -37,7 +37,7 @@ def build_tree(arr: list[int]) -> Node | None:
of the constructed tree

>>> build_tree(test_array)
min_value: 0, max_value: 9
<min_value=0 max_value=9>
"""
root = Node(len(arr))
root.minn, root.maxx = min(arr), max(arr)
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def delete(self, data) -> str:
if current.next:
current = current.next
else: # We have reached the end an no value matches
return "No data matching given value"
raise ValueError("No data matching given value")

if current == self.head:
self.delete_head()
Expand Down
2 changes: 1 addition & 1 deletion data_structures/queue/double_ended_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def __repr__(self) -> str:
values_list.append(aux.val)
aux = aux.next

return "[" + ", ".join(repr(val) for val in values_list) + "]"
return f"[{', '.join(repr(val) for val in values_list)}]"


if __name__ == "__main__":
Expand Down
4 changes: 1 addition & 3 deletions genetic_algorithm/basic_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ def select(parent_1: tuple[str, float]) -> list[str]:
child_n = int(parent_1[1] * 100) + 1
child_n = 10 if child_n >= 10 else child_n
for _ in range(child_n):
parent_2 = population_score[ # noqa: B023
random.randint(0, N_SELECTED)
][0]
parent_2 = population_score[random.randint(0, N_SELECTED)][0]
cclauss marked this conversation as resolved.
Show resolved Hide resolved

child_1, child_2 = crossover(parent_1[0], parent_2)
# Append new string to the population list.
Expand Down
8 changes: 6 additions & 2 deletions graphs/breadth_first_search_shortest_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def shortest_path(self, target_vertex: str) -> str:

Case 1 - No path is found.
>>> g.shortest_path("Foo")
'No path from vertex:G to vertex:Foo'
Traceback (most recent call last):
...
ValueError: No path from vertex: G to vertex: Foo

Case 2 - The path is found.
>>> g.shortest_path("D")
Expand All @@ -71,7 +73,9 @@ def shortest_path(self, target_vertex: str) -> str:

target_vertex_parent = self.parent.get(target_vertex)
if target_vertex_parent is None:
return f"No path from vertex:{self.source_vertex} to vertex:{target_vertex}"
raise ValueError(
f"No path from vertex: {self.source_vertex} to vertex: {target_vertex}"
)

return self.shortest_path(target_vertex_parent) + f"->{target_vertex}"

Expand Down
2 changes: 1 addition & 1 deletion graphs/page_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def add_outbound(self, node):
self.outbound.append(node)

def __repr__(self):
return f"Node {self.name}: Inbound: {self.inbound} ; Outbound: {self.outbound}"
return f"<node={self.name} inbound={self.inbound} outbound={self.outbound}>"


def page_rank(nodes, limit=3, d=0.85):
Expand Down
2 changes: 1 addition & 1 deletion linear_algebra/src/polynom_for_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
elif check == 2:
return solved
else:
return "The program cannot work out a fitting polynomial."
raise ValueError("The program cannot work out a fitting polynomial.")


if __name__ == "__main__":
Expand Down
3 changes: 0 additions & 3 deletions maths/monte_carlo_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ def __init__(self):
def roll(self):
return random.choice(self.sides)

def _str_(self):
return "Fair Dice"


def throw_dice(num_throws: int, num_dice: int = 2) -> list[float]:
"""
Expand Down
16 changes: 9 additions & 7 deletions matrix/cramers_rule_2x2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://en.wikipedia.org/wiki/Cramer%27s_rule


def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str:
def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float]:
"""
Solves the system of linear equation in 2 variables.
:param: equation1: list of 3 numbers
Expand All @@ -14,13 +14,13 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str:
determinant_y = [[a1, d1], [a2, d2]]

>>> cramers_rule_2x2([2, 3, 0], [5, 1, 0])
'Trivial solution. (Consistent system) x = 0 and y = 0'
(0, 0)
>>> cramers_rule_2x2([0, 4, 50], [2, 0, 26])
'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5'
(13.0, 12.5)
>>> cramers_rule_2x2([11, 2, 30], [1, 0, 4])
'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0'
(4.0, -7.0)
>>> cramers_rule_2x2([4, 7, 1], [1, 2, 0])
'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0'
(2.0, -1.0)

>>> cramers_rule_2x2([1, 2, 3], [2, 4, 6])
Traceback (most recent call last):
Expand Down Expand Up @@ -75,8 +75,10 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str:
raise ValueError("No solution. (Inconsistent system)")
else:
if determinant_x == determinant_y == 0:
return "Trivial solution. (Consistent system) x = 0 and y = 0"
# Trivial solution (Inconsistent system)
return (0, 0)
else:
x = determinant_x / determinant
y = determinant_y / determinant
return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}"
# Non-Trivial Solution (Consistent system)
return (x, y)