From 34fee0041e0078672b08681a8a65e97b70c2ef90 Mon Sep 17 00:00:00 2001 From: sijukannan <104427917+sijukannan@users.noreply.github.com> Date: Sat, 18 Mar 2023 13:12:13 +0530 Subject: [PATCH 1/3] Create crit-bit tree.py --- crit-bit tree.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 crit-bit tree.py diff --git a/crit-bit tree.py b/crit-bit tree.py new file mode 100644 index 000000000..4ff030fc2 --- /dev/null +++ b/crit-bit tree.py @@ -0,0 +1,45 @@ +# Define a class for the nodes in the tree +class CritBitNode: + def _init_(self, key=None, terminal=False): + self.key = key + self.left_child = None + self.right_child = None + self.terminal = terminal + +# Define a class for the tree +class CritBitTree: + def _init_(self): + self.root = None + + # Insert a key into the tree + def insert(self, key): + if self.root is None: + self.root = CritBitNode(key) + else: + current_node = self.root + while True: + if key < current_node.key: + if current_node.left_child is None: + current_node.left_child = CritBitNode(key) + break + else: + current_node = current_node.left_child + else: + if current_node.right_child is None: + current_node.right_child = CritBitNode(key) + break + else: + current_node = current_node.right_child + current_node.terminal = True + + # Search for a key in the tree + def search(self, key): + current_node = self.root + while current_node is not None: + if key == current_node.key: + return current_node.terminal + elif key < current_node.key: + current_node = current_node.left_child + else: + current_node = current_node.right_child + return False From 28efd90fc1dd7f46172ac2b1a889e42170d5bd73 Mon Sep 17 00:00:00 2001 From: sijukannan <104427917+sijukannan@users.noreply.github.com> Date: Sat, 18 Mar 2023 13:12:58 +0530 Subject: [PATCH 2/3] Create cipher.py --- cipher.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cipher.py diff --git a/cipher.py b/cipher.py new file mode 100644 index 000000000..be01b0b8e --- /dev/null +++ b/cipher.py @@ -0,0 +1,57 @@ +def caesar_cipher(plaintext, shift): + ciphertext = "" + for char in plaintext: + if char.isalpha(): + if char.isupper(): + ciphertext += chr((ord(char) - 65 + shift) % 26 + 65) + else: + ciphertext += chr((ord(char) - 97 + shift) % 26 + 97) + else: + ciphertext += char + return ciphertext +def vigenere_cipher(plaintext, key): + ciphertext = "" + key_index = 0 + for char in plaintext: + if char.isalpha(): + if char.isupper(): + shift = ord(key[key_index % len(key)].upper()) - 65 + ciphertext += chr((ord(char) - 65 + shift) % 26 + 65) + else: + shift = ord(key[key_index % len(key)].lower()) - 97 + ciphertext += chr((ord(char) - 97 + shift) % 26 + 97) + key_index += 1 + else: + ciphertext += char + return ciphertext +def create_playfair_matrix(keyword): + alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" + matrix = [] + for char in keyword.upper(): + if char not in matrix and char in alphabet: + matrix.append(char) + for char in alphabet: + if char not in matrix: + matrix.append(char) + playfair_matrix = [matrix[i:i+5] for i in range(0, 25, 5)] + return playfair_matrix + +def playfair_cipher(plaintext, keyword): + playfair_matrix = create_playfair_matrix(keyword) + ciphertext = "" + plaintext = plaintext.upper().replace("J", "I") + plaintext = plaintext.replace(" ", "") + plaintext = plaintext + "X" if len(plaintext) % 2 != 0 else plaintext + for i in range(0, len(plaintext), 2): + row1, col1 = divmod(playfair_matrix.index(playfair_matrix[int(playfair_matrix.index(plaintext[i])/5)]), 5) + row2, col2 = divmod(playfair_matrix.index(playfair_matrix[int(playfair_matrix.index(plaintext[i+1])/5)]), 5) + if row1 == row2: + ciphertext += playfair_matrix[row1][(col1+1)%5] + ciphertext += playfair_matrix[row2][(col2+1)%5] + elif col1 == col2: + ciphertext += playfair_matrix[(row1+1)%5][col1] + ciphertext += playfair_matrix[(row2+1)%5][col2] + else: + ciphertext += playfair_matrix[row1][col2] + ciphertext += playfair_matrix[row2][col1] + return ciphertext From 51a8a572460e70bcafcdf62bbc49fd037132387e Mon Sep 17 00:00:00 2001 From: sijukannan <104427917+sijukannan@users.noreply.github.com> Date: Sat, 18 Mar 2023 13:13:46 +0530 Subject: [PATCH 3/3] Create Array.py --- Array.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Array.py diff --git a/Array.py b/Array.py new file mode 100644 index 000000000..bf60f5af8 --- /dev/null +++ b/Array.py @@ -0,0 +1,30 @@ +# Initialize an array with 5 elements +my_array = [10, 20, 30, 40, 50] + +# Access array elements +print(my_array[0]) # Output: 10 +print(my_array[3]) # Output: 40 + +# Search for an element +element_to_search = 30 +if element_to_search in my_array: + print(f"Element {element_to_search} found at index {my_array.index(element_to_search)}") +else: + print(f"Element {element_to_search} not found") + +# Sort the array +sorted_array = sorted(my_array) +print(sorted_array) # Output: [10, 20, 30, 40, 50] + +# Insert an element +my_array.append(60) +print(my_array) # Output: [10, 20, 30, 40, 50, 60] + +# Delete an element +del my_array[2] +print(my_array) # Output: [10, 20, 40, 50, 60] + +# Resize the array +my_array = [1, 2, 3, 4, 5] +my_array = my_array[:3] # Resize array to first 3 elements +print(my_array) # Output: [1, 2, 3]