Compare commits

...

12 Commits

Author SHA1 Message Date
Shaurya Bisht
9943ada72e
Merge 240acd1448 into e3bd7721c8 2024-11-16 16:42:42 +05:30
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
Shaurya Bisht
240acd1448 edited else-if to elif 2024-10-14 18:12:01 +00:00
pre-commit-ci[bot]
fb53ab85f6 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-14 18:08:00 +00:00
Shaurya Bisht
bc93da6d92 Merge branch 'master' of https://github.com/ShauryaDusht/Python 2024-10-14 22:43:57 +05:30
Shaurya Bisht
7e994ff238 Added new case in insert function 2024-10-14 22:43:31 +05:30
Shaurya Bisht
00fdf1bedd Added new case in radix_tree.py 2024-10-14 13:58:21 +00:00
Shaurya Bisht
b3ba109e2c Merge branch 'master' of https://github.com/ShauryaDusht/Python 2024-10-14 19:08:42 +05:30
Shaurya Bisht
c7c39d0d6a Fixed insert function - added case 1 2024-10-14 19:00:30 +05:30
pre-commit-ci[bot]
c91f6b6359 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-12 13:36:44 +00:00
Shaurya Bisht
672cb3bbd9 Improved del_node function in avl_tree.py
In previous version of code balance factor was not taken in account
So, I updated the code using balance factor
2024-10-12 19:05:40 +05:30
7 changed files with 44 additions and 22 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2 rev: v0.7.3
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format
@ -42,7 +42,7 @@ repos:
pass_filenames: false pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject - repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22 rev: v0.23
hooks: hooks:
- id: validate-pyproject - id: validate-pyproject

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"): def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep." "Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115 return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17): def random_puzzle(assignments=17):

View File

@ -196,8 +196,13 @@ def get_left_most(root: MyNode) -> Any:
def del_node(root: MyNode, data: Any) -> MyNode | None: def del_node(root: MyNode, data: Any) -> MyNode | None:
if root is None:
print("Node is empty, nothing to delete")
return None
left_child = root.get_left() left_child = root.get_left()
right_child = root.get_right() right_child = root.get_right()
if root.get_data() == data: if root.get_data() == data:
if left_child is not None and right_child is not None: if left_child is not None and right_child is not None:
temp_data = get_left_most(right_child) temp_data = get_left_most(right_child)
@ -209,32 +214,40 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
root = right_child root = right_child
else: else:
return None return None
elif root.get_data() > data: elif data < root.get_data():
if left_child is None: if left_child is None:
print("No such data") print(f"No such data ({data}) exists in the left subtree.")
return root return root
else: else:
root.set_left(del_node(left_child, data)) root.set_left(del_node(left_child, data))
# root.get_data() < data
elif right_child is None: elif right_child is None:
print(f"No such data ({data}) exists in the right subtree.")
return root return root
else: else:
root.set_right(del_node(right_child, data)) root.set_right(del_node(right_child, data))
if get_height(right_child) - get_height(left_child) == 2: # Update the height of the node
root.set_height(
1 + my_max(get_height(root.get_left()), get_height(root.get_right()))
)
# Get the balance factor
balance_factor = get_height(root.get_right()) - get_height(root.get_left())
# Balance the tree
if balance_factor == 2:
assert right_child is not None assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()): if get_height(right_child.get_right()) > get_height(right_child.get_left()):
root = left_rotation(root) root = left_rotation(root)
else: else:
root = rl_rotation(root) root = rl_rotation(root)
elif get_height(right_child) - get_height(left_child) == -2: elif balance_factor == -2:
assert left_child is not None assert left_child is not None
if get_height(left_child.get_left()) > get_height(left_child.get_right()): if get_height(left_child.get_left()) > get_height(left_child.get_right()):
root = right_rotation(root) root = right_rotation(root)
else: else:
root = lr_rotation(root) root = lr_rotation(root)
height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
root.set_height(height)
return root return root

View File

@ -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:

View File

@ -39,10 +39,12 @@ 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 (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n if k != n
else sum(vl[:k]) else sum(vl[:k])
) )
)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -204,9 +204,11 @@ class Matrix:
return Matrix( return Matrix(
[ [
[ [
(
self.minors().rows[row][column] self.minors().rows[row][column]
if (row + column) % 2 == 0 if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1 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)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!python
import os import os
try: try: