mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
Fixed insert function - added case 1
This commit is contained in:
parent
e9e7c96465
commit
c7c39d0d6a
|
@ -62,12 +62,17 @@ class RadixNode:
|
||||||
-- A (leaf)
|
-- A (leaf)
|
||||||
--- A (leaf)
|
--- A (leaf)
|
||||||
"""
|
"""
|
||||||
# Case 1: If the word is the prefix of the node
|
# Case 1: If the word is empty, mark current node as leaf
|
||||||
|
if not word:
|
||||||
|
self.is_leaf = True
|
||||||
|
return
|
||||||
|
|
||||||
|
# Case 2: If the word is the prefix of the node
|
||||||
# Solution: We set the current node as leaf
|
# Solution: We set the current node as leaf
|
||||||
if self.prefix == word and not self.is_leaf:
|
if self.prefix == word and not self.is_leaf:
|
||||||
self.is_leaf = True
|
self.is_leaf = True
|
||||||
|
|
||||||
# Case 2: The node has no edges that have a prefix to the word
|
# Case 3: The node has no edges that have a prefix to the word
|
||||||
# Solution: We create an edge from the current node to a new one
|
# Solution: We create an edge from the current node to a new one
|
||||||
# containing the word
|
# containing the word
|
||||||
elif word[0] not in self.nodes:
|
elif word[0] not in self.nodes:
|
||||||
|
@ -79,12 +84,12 @@ class RadixNode:
|
||||||
word
|
word
|
||||||
)
|
)
|
||||||
|
|
||||||
# Case 3: The node prefix is equal to the matching
|
# Case 4: The node prefix is equal to the matching
|
||||||
# Solution: We insert remaining word on the next node
|
# Solution: We insert remaining word on the next node
|
||||||
if remaining_prefix == "":
|
if remaining_prefix == "":
|
||||||
self.nodes[matching_string[0]].insert(remaining_word)
|
incoming_node.insert(remaining_word)
|
||||||
|
|
||||||
# Case 4: The word is greater equal to the matching
|
# Case 5: The word is greater equal to the matching
|
||||||
# Solution: Create a node in between both nodes, change
|
# Solution: Create a node in between both nodes, change
|
||||||
# prefixes and add the new node for the remaining word
|
# prefixes and add the new node for the remaining word
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
|
||||||
return (
|
return (
|
||||||
0
|
0
|
||||||
if k == 0
|
if k == 0
|
||||||
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
|
else (
|
||||||
if k != n
|
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
|
||||||
else sum(vl[:k])
|
if k != n
|
||||||
|
else sum(vl[:k])
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -204,9 +204,11 @@ class Matrix:
|
||||||
return Matrix(
|
return Matrix(
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
self.minors().rows[row][column]
|
(
|
||||||
if (row + column) % 2 == 0
|
self.minors().rows[row][column]
|
||||||
else self.minors().rows[row][column] * -1
|
if (row + column) % 2 == 0
|
||||||
|
else self.minors().rows[row][column] * -1
|
||||||
|
)
|
||||||
for column in range(self.minors().num_columns)
|
for column in range(self.minors().num_columns)
|
||||||
]
|
]
|
||||||
for row in range(self.minors().num_rows)
|
for row in range(self.minors().num_rows)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user