Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ repos:
rev: "0.26"
hooks:
- id: validate-pyproject

- repo: https://github.com/15r10nk/matchify

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The rest of the file leaves a blank line between hooks for readability.

Suggested change
- repo: https://github.com/15r10nk/matchify
- repo: https://github.com/15r10nk/matchify

rev: v0.2.0
hooks:
- id: matchify
exclude: ^data_structures/linked_list/doubly_linked_list\.py$
17 changes: 9 additions & 8 deletions ciphers/autokey.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ def decrypt(ciphertext: str, key: str) -> str:

doctest.testmod()
operation = int(input("Type 1 to encrypt or 2 to decrypt:"))
if operation == 1:
plaintext = input("Typeplaintext to be encrypted:\n")
key = input("Type the key:\n")
print(encrypt(plaintext, key))
elif operation == 2:
ciphertext = input("Type the ciphertext to be decrypted:\n")
key = input("Type the key:\n")
print(decrypt(ciphertext, key))
match operation:
case 1:
plaintext = input("Typeplaintext to be encrypted:\n")
key = input("Type the key:\n")
print(encrypt(plaintext, key))
case 2:
ciphertext = input("Type the ciphertext to be decrypted:\n")
key = input("Type the key:\n")
print(decrypt(ciphertext, key))
decrypt("jsqqs avvwo", "coffee")
17 changes: 9 additions & 8 deletions ciphers/hill_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,15 @@ def main() -> None:

print("Would you like to encrypt or decrypt some text? (1 or 2)")
option = input("\n1. Encrypt\n2. Decrypt\n")
if option == "1":
text_e = input("What text would you like to encrypt?: ")
print("Your encrypted text is:")
print(hc.encrypt(text_e))
elif option == "2":
text_d = input("What text would you like to decrypt?: ")
print("Your decrypted text is:")
print(hc.decrypt(text_d))
match option:
case "1":
text_e = input("What text would you like to encrypt?: ")
print("Your encrypted text is:")
print(hc.encrypt(text_e))
case "2":
text_d = input("What text would you like to decrypt?: ")
print("Your decrypted text is:")
print(hc.decrypt(text_d))


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions ciphers/mono_alphabetic_ciphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ def main() -> None:
key = "QWERTYUIOPASDFGHJKLZXCVBNM"
mode = "decrypt" # set to 'encrypt' or 'decrypt'

if mode == "encrypt":
translated = encrypt_message(key, message)
elif mode == "decrypt":
translated = decrypt_message(key, message)
match mode:
case "encrypt":
translated = encrypt_message(key, message)
case "decrypt":
translated = decrypt_message(key, message)
print(f"Using the key {key}, the {mode}ed message is: {translated}")


Expand Down
47 changes: 25 additions & 22 deletions ciphers/rsa_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,31 @@ def main() -> None:
elif response.lower().startswith("d"):
mode = "decrypt"

if mode == "encrypt":
if not os.path.exists("rsa_pubkey.txt"):
rkg.make_key_files("rsa", 1024)

message = input("\nEnter message: ")
pubkey_filename = "rsa_pubkey.txt"
print(f"Encrypting and writing to {filename}...")
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)

print("\nEncrypted text:")
print(encrypted_text)

elif mode == "decrypt":
privkey_filename = "rsa_privkey.txt"
print(f"Reading from {filename} and decrypting...")
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
print("writing decryption to rsa_decryption.txt...")
with open("rsa_decryption.txt", "w") as dec:
dec.write(decrypted_text)

print("\nDecryption:")
print(decrypted_text)
match mode:
case "encrypt":
if not os.path.exists("rsa_pubkey.txt"):
rkg.make_key_files("rsa", 1024)

message = input("\nEnter message: ")
pubkey_filename = "rsa_pubkey.txt"
print(f"Encrypting and writing to {filename}...")
encrypted_text = encrypt_and_write_to_file(
filename, pubkey_filename, message
)

print("\nEncrypted text:")
print(encrypted_text)

case "decrypt":
privkey_filename = "rsa_privkey.txt"
print(f"Reading from {filename} and decrypting...")
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
print("writing decryption to rsa_decryption.txt...")
with open("rsa_decryption.txt", "w") as dec:
dec.write(decrypted_text)

print("\nDecryption:")
print(decrypted_text)


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions ciphers/vigenere_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def translate_message(key: str, message: str, mode: str) -> str:
for symbol in message:
num = LETTERS.find(symbol.upper())
if num != -1:
if mode == "encrypt":
num += LETTERS.find(key[key_index])
elif mode == "decrypt":
num -= LETTERS.find(key[key_index])
match mode:
case "encrypt":
num += LETTERS.find(key[key_index])
case "decrypt":
num -= LETTERS.find(key[key_index])

num %= len(LETTERS)

Expand Down
21 changes: 11 additions & 10 deletions computer_vision/flip_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ def update_image_and_anno(
path_list.append(path)
img_annos = anno_list[idx]
img = cv2.imread(path)
if flip_type == 1:
new_img = cv2.flip(img, flip_type)
for bbox in img_annos:
x_center_new = 1 - bbox[1]
new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]])
elif flip_type == 0:
new_img = cv2.flip(img, flip_type)
for bbox in img_annos:
y_center_new = 1 - bbox[2]
new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]])
match flip_type:
case 1:
new_img = cv2.flip(img, flip_type)
for bbox in img_annos:
x_center_new = 1 - bbox[1]
new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]])
case 0:
new_img = cv2.flip(img, flip_type)
for bbox in img_annos:
y_center_new = 1 - bbox[2]
new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]])
new_annos_lists.append(new_annos)
new_imgs_list.append(new_img)
return new_imgs_list, new_annos_lists, path_list
Expand Down
82 changes: 42 additions & 40 deletions computer_vision/mosaic_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,46 +117,48 @@ def update_image_and_anno(
path_list.append(path)
img_annos = all_annos[index]
img = cv2.imread(path)
if i == 0: # top-left
img = cv2.resize(img, (divid_point_x, divid_point_y))
output_img[:divid_point_y, :divid_point_x, :] = img
for bbox in img_annos:
xmin = bbox[1] * scale_x
ymin = bbox[2] * scale_y
xmax = bbox[3] * scale_x
ymax = bbox[4] * scale_y
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
elif i == 1: # top-right
img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y))
output_img[:divid_point_y, divid_point_x : output_size[1], :] = img
for bbox in img_annos:
xmin = scale_x + bbox[1] * (1 - scale_x)
ymin = bbox[2] * scale_y
xmax = scale_x + bbox[3] * (1 - scale_x)
ymax = bbox[4] * scale_y
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
elif i == 2: # bottom-left
img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y))
output_img[divid_point_y : output_size[0], :divid_point_x, :] = img
for bbox in img_annos:
xmin = bbox[1] * scale_x
ymin = scale_y + bbox[2] * (1 - scale_y)
xmax = bbox[3] * scale_x
ymax = scale_y + bbox[4] * (1 - scale_y)
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
else: # bottom-right
img = cv2.resize(
img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y)
)
output_img[
divid_point_y : output_size[0], divid_point_x : output_size[1], :
] = img
for bbox in img_annos:
xmin = scale_x + bbox[1] * (1 - scale_x)
ymin = scale_y + bbox[2] * (1 - scale_y)
xmax = scale_x + bbox[3] * (1 - scale_x)
ymax = scale_y + bbox[4] * (1 - scale_y)
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
match i:
case 0: # top-left
img = cv2.resize(img, (divid_point_x, divid_point_y))
output_img[:divid_point_y, :divid_point_x, :] = img
for bbox in img_annos:
xmin = bbox[1] * scale_x
ymin = bbox[2] * scale_y
xmax = bbox[3] * scale_x
ymax = bbox[4] * scale_y
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
case 1: # top-right
img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y))
output_img[:divid_point_y, divid_point_x : output_size[1], :] = img
for bbox in img_annos:
xmin = scale_x + bbox[1] * (1 - scale_x)
ymin = bbox[2] * scale_y
xmax = scale_x + bbox[3] * (1 - scale_x)
ymax = bbox[4] * scale_y
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
case 2: # bottom-left
img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y))
output_img[divid_point_y : output_size[0], :divid_point_x, :] = img
for bbox in img_annos:
xmin = bbox[1] * scale_x
ymin = scale_y + bbox[2] * (1 - scale_y)
xmax = bbox[3] * scale_x
ymax = scale_y + bbox[4] * (1 - scale_y)
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
case _: # bottom-right
img = cv2.resize(
img,
(output_size[1] - divid_point_x, output_size[0] - divid_point_y),
)
output_img[
divid_point_y : output_size[0], divid_point_x : output_size[1], :
] = img
for bbox in img_annos:
xmin = scale_x + bbox[1] * (1 - scale_x)
ymin = scale_y + bbox[2] * (1 - scale_y)
xmax = scale_x + bbox[3] * (1 - scale_x)
ymax = scale_y + bbox[4] * (1 - scale_y)
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])

# Remove bounding box small than scale of filter
if filter_scale > 0:
Expand Down
11 changes: 6 additions & 5 deletions conversions/decimal_to_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ def decimal_to_any(num: int, base: int) -> str:
new_value += actual_value
div = num // base
num = div
if div == 0:
return str(new_value[::-1])
elif div == 1:
new_value += str(div)
return str(new_value[::-1])
match div:
case 0:
return str(new_value[::-1])
case 1:
new_value += str(div)
return str(new_value[::-1])

return new_value[::-1]

Expand Down
25 changes: 13 additions & 12 deletions data_structures/binary_tree/avl_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,19 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
left_child = root.get_left()
right_child = root.get_right()

if get_height(right_child) - get_height(left_child) == 2:
assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
root = left_rotation(root)
else:
root = rl_rotation(root)
elif get_height(right_child) - get_height(left_child) == -2:
assert left_child is not None
if get_height(left_child.get_left()) > get_height(left_child.get_right()):
root = right_rotation(root)
else:
root = lr_rotation(root)
match get_height(right_child) - get_height(left_child):
case 2:
assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
root = left_rotation(root)
else:
root = rl_rotation(root)
case -2:
assert left_child is not None
if get_height(left_child.get_left()) > get_height(left_child.get_right()):
root = right_rotation(root)
else:
root = lr_rotation(root)
height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
root.set_height(height)
return root
Expand Down
13 changes: 7 additions & 6 deletions data_structures/binary_tree/treap.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ def interact_treap(root: Node | None, args: str) -> Node | None:
Unknown command
"""
for arg in args.split():
if arg[0] == "+":
root = insert(root, int(arg[1:]))
match arg[0]:
case "+":
root = insert(root, int(arg[1:]))

elif arg[0] == "-":
root = erase(root, int(arg[1:]))
case "-":
root = erase(root, int(arg[1:]))

else:
print("Unknown command")
case _:
print("Unknown command")

return root

Expand Down
31 changes: 16 additions & 15 deletions data_structures/linked_list/sorted_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,19 @@ def merge(self, other_list: SortedLinkedList) -> None:
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
node_data = int(input("Enter a number: "))
linked_list.insert(node_data)
elif choice == "2":
linked_list.display()
elif choice == "3":
node_data = int(input("Enter the data to delete: "))
if linked_list.delete(node_data):
print(f"Node with data {node_data} deleted successfully")
else:
print(f"Node with data {node_data} not found in the list")
elif choice == "4":
break
else:
print("Wrong input")
match choice:
case "1":
node_data = int(input("Enter a number: "))
linked_list.insert(node_data)
case "2":
linked_list.display()
case "3":
node_data = int(input("Enter the data to delete: "))
if linked_list.delete(node_data):
print(f"Node with data {node_data} deleted successfully")
else:
print(f"Node with data {node_data} not found in the list")
case "4":
break
case _:
print("Wrong input")
9 changes: 5 additions & 4 deletions data_structures/stacks/infix_to_prefix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ def infix_2_prefix(infix: str) -> str:
reversed_infix = list(infix[::-1]) # reverse the infix equation

for i in range(len(reversed_infix)):
if reversed_infix[i] == "(":
reversed_infix[i] = ")" # change "(" to ")"
elif reversed_infix[i] == ")":
reversed_infix[i] = "(" # change ")" to "("
match reversed_infix[i]:
case "(":
reversed_infix[i] = ")" # change "(" to ")"
case ")":
reversed_infix[i] = "(" # change ")" to "("

# call infix_2_postfix on Infix, return reverse of Postfix
return (infix_2_postfix("".join(reversed_infix)))[::-1]
Expand Down
Loading
Loading