Skip to content

Commit eceebae

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent dae13f1 commit eceebae

41 files changed

Lines changed: 727 additions & 679 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ciphers/autokey.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ def decrypt(ciphertext: str, key: str) -> str:
139139

140140
doctest.testmod()
141141
operation = int(input("Type 1 to encrypt or 2 to decrypt:"))
142-
if operation == 1:
143-
plaintext = input("Typeplaintext to be encrypted:\n")
144-
key = input("Type the key:\n")
145-
print(encrypt(plaintext, key))
146-
elif operation == 2:
147-
ciphertext = input("Type the ciphertext to be decrypted:\n")
148-
key = input("Type the key:\n")
149-
print(decrypt(ciphertext, key))
142+
match operation:
143+
case 1:
144+
plaintext = input("Typeplaintext to be encrypted:\n")
145+
key = input("Type the key:\n")
146+
print(encrypt(plaintext, key))
147+
case 2:
148+
ciphertext = input("Type the ciphertext to be decrypted:\n")
149+
key = input("Type the key:\n")
150+
print(decrypt(ciphertext, key))
150151
decrypt("jsqqs avvwo", "coffee")

ciphers/hill_cipher.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,15 @@ def main() -> None:
203203

204204
print("Would you like to encrypt or decrypt some text? (1 or 2)")
205205
option = input("\n1. Encrypt\n2. Decrypt\n")
206-
if option == "1":
207-
text_e = input("What text would you like to encrypt?: ")
208-
print("Your encrypted text is:")
209-
print(hc.encrypt(text_e))
210-
elif option == "2":
211-
text_d = input("What text would you like to decrypt?: ")
212-
print("Your decrypted text is:")
213-
print(hc.decrypt(text_d))
206+
match option:
207+
case "1":
208+
text_e = input("What text would you like to encrypt?: ")
209+
print("Your encrypted text is:")
210+
print(hc.encrypt(text_e))
211+
case "2":
212+
text_d = input("What text would you like to decrypt?: ")
213+
print("Your decrypted text is:")
214+
print(hc.decrypt(text_d))
214215

215216

216217
if __name__ == "__main__":

ciphers/mono_alphabetic_ciphers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ def main() -> None:
4949
key = "QWERTYUIOPASDFGHJKLZXCVBNM"
5050
mode = "decrypt" # set to 'encrypt' or 'decrypt'
5151

52-
if mode == "encrypt":
53-
translated = encrypt_message(key, message)
54-
elif mode == "decrypt":
55-
translated = decrypt_message(key, message)
52+
match mode:
53+
case "encrypt":
54+
translated = encrypt_message(key, message)
55+
case "decrypt":
56+
translated = decrypt_message(key, message)
5657
print(f"Using the key {key}, the {mode}ed message is: {translated}")
5758

5859

ciphers/rsa_cipher.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,29 @@ def main() -> None:
121121
elif response.lower().startswith("d"):
122122
mode = "decrypt"
123123

124-
if mode == "encrypt":
125-
if not os.path.exists("rsa_pubkey.txt"):
126-
rkg.make_key_files("rsa", 1024)
127-
128-
message = input("\nEnter message: ")
129-
pubkey_filename = "rsa_pubkey.txt"
130-
print(f"Encrypting and writing to {filename}...")
131-
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)
132-
133-
print("\nEncrypted text:")
134-
print(encrypted_text)
135-
136-
elif mode == "decrypt":
137-
privkey_filename = "rsa_privkey.txt"
138-
print(f"Reading from {filename} and decrypting...")
139-
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
140-
print("writing decryption to rsa_decryption.txt...")
141-
with open("rsa_decryption.txt", "w") as dec:
142-
dec.write(decrypted_text)
143-
144-
print("\nDecryption:")
145-
print(decrypted_text)
124+
match mode:
125+
case "encrypt":
126+
if not os.path.exists("rsa_pubkey.txt"):
127+
rkg.make_key_files("rsa", 1024)
128+
129+
message = input("\nEnter message: ")
130+
pubkey_filename = "rsa_pubkey.txt"
131+
print(f"Encrypting and writing to {filename}...")
132+
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)
133+
134+
print("\nEncrypted text:")
135+
print(encrypted_text)
136+
137+
case "decrypt":
138+
privkey_filename = "rsa_privkey.txt"
139+
print(f"Reading from {filename} and decrypting...")
140+
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
141+
print("writing decryption to rsa_decryption.txt...")
142+
with open("rsa_decryption.txt", "w") as dec:
143+
dec.write(decrypted_text)
144+
145+
print("\nDecryption:")
146+
print(decrypted_text)
146147

147148

148149
if __name__ == "__main__":

ciphers/vigenere_cipher.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ def translate_message(key: str, message: str, mode: str) -> str:
4141
for symbol in message:
4242
num = LETTERS.find(symbol.upper())
4343
if num != -1:
44-
if mode == "encrypt":
45-
num += LETTERS.find(key[key_index])
46-
elif mode == "decrypt":
47-
num -= LETTERS.find(key[key_index])
44+
match mode:
45+
case "encrypt":
46+
num += LETTERS.find(key[key_index])
47+
case "decrypt":
48+
num -= LETTERS.find(key[key_index])
4849

4950
num %= len(LETTERS)
5051

computer_vision/flip_augmentation.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ def update_image_and_anno(
9696
path_list.append(path)
9797
img_annos = anno_list[idx]
9898
img = cv2.imread(path)
99-
if flip_type == 1:
100-
new_img = cv2.flip(img, flip_type)
101-
for bbox in img_annos:
102-
x_center_new = 1 - bbox[1]
103-
new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]])
104-
elif flip_type == 0:
105-
new_img = cv2.flip(img, flip_type)
106-
for bbox in img_annos:
107-
y_center_new = 1 - bbox[2]
108-
new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]])
99+
match flip_type:
100+
case 1:
101+
new_img = cv2.flip(img, flip_type)
102+
for bbox in img_annos:
103+
x_center_new = 1 - bbox[1]
104+
new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]])
105+
case 0:
106+
new_img = cv2.flip(img, flip_type)
107+
for bbox in img_annos:
108+
y_center_new = 1 - bbox[2]
109+
new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]])
109110
new_annos_lists.append(new_annos)
110111
new_imgs_list.append(new_img)
111112
return new_imgs_list, new_annos_lists, path_list

computer_vision/mosaic_augmentation.py

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -117,46 +117,47 @@ def update_image_and_anno(
117117
path_list.append(path)
118118
img_annos = all_annos[index]
119119
img = cv2.imread(path)
120-
if i == 0: # top-left
121-
img = cv2.resize(img, (divid_point_x, divid_point_y))
122-
output_img[:divid_point_y, :divid_point_x, :] = img
123-
for bbox in img_annos:
124-
xmin = bbox[1] * scale_x
125-
ymin = bbox[2] * scale_y
126-
xmax = bbox[3] * scale_x
127-
ymax = bbox[4] * scale_y
128-
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
129-
elif i == 1: # top-right
130-
img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y))
131-
output_img[:divid_point_y, divid_point_x : output_size[1], :] = img
132-
for bbox in img_annos:
133-
xmin = scale_x + bbox[1] * (1 - scale_x)
134-
ymin = bbox[2] * scale_y
135-
xmax = scale_x + bbox[3] * (1 - scale_x)
136-
ymax = bbox[4] * scale_y
137-
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
138-
elif i == 2: # bottom-left
139-
img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y))
140-
output_img[divid_point_y : output_size[0], :divid_point_x, :] = img
141-
for bbox in img_annos:
142-
xmin = bbox[1] * scale_x
143-
ymin = scale_y + bbox[2] * (1 - scale_y)
144-
xmax = bbox[3] * scale_x
145-
ymax = scale_y + bbox[4] * (1 - scale_y)
146-
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
147-
else: # bottom-right
148-
img = cv2.resize(
149-
img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y)
150-
)
151-
output_img[
152-
divid_point_y : output_size[0], divid_point_x : output_size[1], :
153-
] = img
154-
for bbox in img_annos:
155-
xmin = scale_x + bbox[1] * (1 - scale_x)
156-
ymin = scale_y + bbox[2] * (1 - scale_y)
157-
xmax = scale_x + bbox[3] * (1 - scale_x)
158-
ymax = scale_y + bbox[4] * (1 - scale_y)
159-
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
120+
match i:
121+
case 0: # top-left
122+
img = cv2.resize(img, (divid_point_x, divid_point_y))
123+
output_img[:divid_point_y, :divid_point_x, :] = img
124+
for bbox in img_annos:
125+
xmin = bbox[1] * scale_x
126+
ymin = bbox[2] * scale_y
127+
xmax = bbox[3] * scale_x
128+
ymax = bbox[4] * scale_y
129+
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
130+
case 1: # top-right
131+
img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y))
132+
output_img[:divid_point_y, divid_point_x : output_size[1], :] = img
133+
for bbox in img_annos:
134+
xmin = scale_x + bbox[1] * (1 - scale_x)
135+
ymin = bbox[2] * scale_y
136+
xmax = scale_x + bbox[3] * (1 - scale_x)
137+
ymax = bbox[4] * scale_y
138+
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
139+
case 2: # bottom-left
140+
img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y))
141+
output_img[divid_point_y : output_size[0], :divid_point_x, :] = img
142+
for bbox in img_annos:
143+
xmin = bbox[1] * scale_x
144+
ymin = scale_y + bbox[2] * (1 - scale_y)
145+
xmax = bbox[3] * scale_x
146+
ymax = scale_y + bbox[4] * (1 - scale_y)
147+
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
148+
case _: # bottom-right
149+
img = cv2.resize(
150+
img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y)
151+
)
152+
output_img[
153+
divid_point_y : output_size[0], divid_point_x : output_size[1], :
154+
] = img
155+
for bbox in img_annos:
156+
xmin = scale_x + bbox[1] * (1 - scale_x)
157+
ymin = scale_y + bbox[2] * (1 - scale_y)
158+
xmax = scale_x + bbox[3] * (1 - scale_x)
159+
ymax = scale_y + bbox[4] * (1 - scale_y)
160+
new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
160161

161162
# Remove bounding box small than scale of filter
162163
if filter_scale > 0:

conversions/decimal_to_any.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ def decimal_to_any(num: int, base: int) -> str:
8181
new_value += actual_value
8282
div = num // base
8383
num = div
84-
if div == 0:
85-
return str(new_value[::-1])
86-
elif div == 1:
87-
new_value += str(div)
88-
return str(new_value[::-1])
84+
match div:
85+
case 0:
86+
return str(new_value[::-1])
87+
case 1:
88+
new_value += str(div)
89+
return str(new_value[::-1])
8990

9091
return new_value[::-1]
9192

data_structures/binary_tree/avl_tree.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,19 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
225225
left_child = root.get_left()
226226
right_child = root.get_right()
227227

228-
if get_height(right_child) - get_height(left_child) == 2:
229-
assert right_child is not None
230-
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
231-
root = left_rotation(root)
232-
else:
233-
root = rl_rotation(root)
234-
elif get_height(right_child) - get_height(left_child) == -2:
235-
assert left_child is not None
236-
if get_height(left_child.get_left()) > get_height(left_child.get_right()):
237-
root = right_rotation(root)
238-
else:
239-
root = lr_rotation(root)
228+
match get_height(right_child) - get_height(left_child):
229+
case 2:
230+
assert right_child is not None
231+
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
232+
root = left_rotation(root)
233+
else:
234+
root = rl_rotation(root)
235+
case -2:
236+
assert left_child is not None
237+
if get_height(left_child.get_left()) > get_height(left_child.get_right()):
238+
root = right_rotation(root)
239+
else:
240+
root = lr_rotation(root)
240241
height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
241242
root.set_height(height)
242243
return root

data_structures/binary_tree/treap.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,15 @@ def interact_treap(root: Node | None, args: str) -> Node | None:
143143
Unknown command
144144
"""
145145
for arg in args.split():
146-
if arg[0] == "+":
147-
root = insert(root, int(arg[1:]))
146+
match arg[0]:
147+
case "+":
148+
root = insert(root, int(arg[1:]))
148149

149-
elif arg[0] == "-":
150-
root = erase(root, int(arg[1:]))
150+
case "-":
151+
root = erase(root, int(arg[1:]))
151152

152-
else:
153-
print("Unknown command")
153+
case _:
154+
print("Unknown command")
154155

155156
return root
156157

0 commit comments

Comments
 (0)