Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new algorithm, data structure, cipher, puzzle (Hacktoberfest accepted) #3474 #3547

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions Array.py
Original file line number Diff line number Diff line change
@@ -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]
57 changes: 57 additions & 0 deletions cipher.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions crit-bit tree.py
Original file line number Diff line number Diff line change
@@ -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