Skip to content

added algorithms #3477 #3543

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

Open
wants to merge 1 commit 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
31 changes: 31 additions & 0 deletions One-Time Pad Cipher/One-Time_Pad_Cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import random

def generate_key(length):
"""Generates a random binary key of a given length."""
return ''.join([str(random.randint(0,1)) for i in range(length)])

def encrypt(plaintext, key):
"""Encrypts a plaintext message using a given key."""
ciphertext = ''
for i in range(len(plaintext)):
# Perform bitwise XOR operation on each bit of the plaintext and key
ciphertext += str(int(plaintext[i]) ^ int(key[i]))
return ciphertext

def decrypt(ciphertext, key):
"""Decrypts a ciphertext message using a given key."""
plaintext = ''
for i in range(len(ciphertext)):
# Perform bitwise XOR operation on each bit of the ciphertext and key
plaintext += str(int(ciphertext[i]) ^ int(key[i]))
return plaintext

# Example usage
plaintext = 'Hello World'
key = generate_key(len(plaintext))
ciphertext = encrypt(''.join(format(ord(c), '08b') for c in plaintext), key)
decrypted_text = decrypt(ciphertext, key)
print('Plaintext:', plaintext)
print('Key:', key)
print('Ciphertext:', ciphertext)
print('Decrypted Text:', ''.join(chr(int(decrypted_text[i:i+8], 2)) for i in range(0, len(decrypted_text), 8)))
51 changes: 51 additions & 0 deletions binary search tree/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data

class BinaryTree:
def __init__(self):
self.root = None

def insert(self, data):
if self.root is None:
self.root = Node(data)
else:
self._insert(data, self.root)

def _insert(self, data, node):
if data < node.data:
if node.left is None:
node.left = Node(data)
else:
self._insert(data, node.left)
else:
if node.right is None:
node.right = Node(data)
else:
self._insert(data, node.right)

def search(self, data):
if self.root is not None:
return self._search(data, self.root)
else:
return None

def _search(self, data, node):
if data == node.data:
return node
elif data < node.data and node.left is not None:
return self._search(data, node.left)
elif data > node.data and node.right is not None:
return self._search(data, node.right)

def traverse_in_order(self):
if self.root is not None:
self._traverse_in_order(self.root)

def _traverse_in_order(self, node):
if node is not None:
self._traverse_in_order(node.left)
print(node.data)
self._traverse_in_order(node.right)
24 changes: 24 additions & 0 deletions data_structures/Priority_Queues/Priority_Queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import heapq

class PriorityQueue:
def __init__(self):
self._heap = []

def push(self, priority, item):
heapq.heappush(self._heap, (priority, item))

def pop(self):
priority, item = heapq.heappop(self._heap)
return item

def is_empty(self):
return len(self._heap) == 0

def size(self):
return len(self._heap)

def top(self):
if self.is_empty():
raise Exception("Priority queue is empty")
priority, item = self._heap[0]
return item
24 changes: 24 additions & 0 deletions puzzles/Eight queens/Eight_queens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class EightQueens:
def __init__(self, size=8):
self.size = size
self.columns = []

def place_queen(self, row, column):
for r, c in enumerate(self.columns):
if c == column or r - c == row - column or r + c == row + column:
return False
self.columns.append(column)
return True

def solve(self):
solutions = []
def backtrack(row=0):
if row == self.size:
solutions.append(list(self.columns))
else:
for column in range(self.size):
if self.place_queen(row, column):
backtrack(row + 1)
self.columns.pop()
backtrack()
return solutions