Skip to content

Commit c7c39d0

Browse files
committed
Fixed insert function - added case 1
1 parent e9e7c96 commit c7c39d0

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

data_structures/trie/radix_tree.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ def insert(self, word: str) -> None:
6262
-- A (leaf)
6363
--- A (leaf)
6464
"""
65-
# Case 1: If the word is the prefix of the node
65+
# Case 1: If the word is empty, mark current node as leaf
66+
if not word:
67+
self.is_leaf = True
68+
return
69+
70+
# Case 2: If the word is the prefix of the node
6671
# Solution: We set the current node as leaf
6772
if self.prefix == word and not self.is_leaf:
6873
self.is_leaf = True
6974

70-
# Case 2: The node has no edges that have a prefix to the word
75+
# Case 3: The node has no edges that have a prefix to the word
7176
# Solution: We create an edge from the current node to a new one
7277
# containing the word
7378
elif word[0] not in self.nodes:
@@ -79,12 +84,12 @@ def insert(self, word: str) -> None:
7984
word
8085
)
8186

82-
# Case 3: The node prefix is equal to the matching
87+
# Case 4: The node prefix is equal to the matching
8388
# Solution: We insert remaining word on the next node
8489
if remaining_prefix == "":
85-
self.nodes[matching_string[0]].insert(remaining_word)
90+
incoming_node.insert(remaining_word)
8691

87-
# Case 4: The word is greater equal to the matching
92+
# Case 5: The word is greater equal to the matching
8893
# Solution: Create a node in between both nodes, change
8994
# prefixes and add the new node for the remaining word
9095
else:

greedy_methods/fractional_knapsack.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
3939
return (
4040
0
4141
if k == 0
42-
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
43-
if k != n
44-
else sum(vl[:k])
42+
else (
43+
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
44+
if k != n
45+
else sum(vl[:k])
46+
)
4547
)
4648

4749

matrix/matrix_class.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
204204
return Matrix(
205205
[
206206
[
207-
self.minors().rows[row][column]
208-
if (row + column) % 2 == 0
209-
else self.minors().rows[row][column] * -1
207+
(
208+
self.minors().rows[row][column]
209+
if (row + column) % 2 == 0
210+
else self.minors().rows[row][column] * -1
211+
)
210212
for column in range(self.minors().num_columns)
211213
]
212214
for row in range(self.minors().num_rows)

0 commit comments

Comments
 (0)