Skip to content

Instantly share code, notes, and snippets.

View talhaahussain's full-sized avatar
🌑
In orbit...

Talhaa Hussain talhaahussain

🌑
In orbit...
View GitHub Profile
@talhaahussain
talhaahussain / charToMorse.hs
Created June 16, 2024 05:33
charToMorse.hs -- A Haskell function to convert a given character to its equivalent Morse code string.
charToMorse :: Char -> [Char]
charToMorse x
| x == ' ' = " " -- U+0020
| x == '!' = "-.-.--" -- U+0021
| x == '"' = ".-..-." -- U+0022
| x == '&' = ".-..." -- U+0026
| x == '\'' = ".----." -- U+0027
| x == '(' = "-.--." -- U+0028
| x == ')' = "-.--.-" -- U+0029
| x == '+' = ".-.-." -- U+002B
@talhaahussain
talhaahussain / image_seq_to_video.py
Created June 14, 2024 18:32
image_seq_to_video.py -- A Python function to convert a sequence of images to video. Depends on OpenCV. See the original and an application here: https://github.com/talhaahussain/grappling-pose-identification/
import cv2
def concat_frames_into_video(filename, frames, fps):
height, width, channels = frames[0].shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(filename, fourcc, fps, (width, height))
for frame in frames:
out.write(frame)
@talhaahussain
talhaahussain / video_to_image_seq.py
Last active June 14, 2024 18:33
video_to_image_seq.py -- A Python function to convert a video to a sequence of images. Depends on OpenCV. See the original and an application here: https://github.com/talhaahussain/grappling-pose-identification/
import cv2
def split_video_into_frames(filename):
frames = []
cap = cv2.VideoCapture(filename)
fps = cap.get(cv2.CAP_PROP_FPS)
success = 1
while success:
success, frame = cap.read()
@talhaahussain
talhaahussain / manhattan_distance.py
Created June 13, 2024 02:28
manhattan_distance.py -- A Python program for computing the distance between two n-dimensional points in Manhattan geometry, using Cartesian coordinates, where each point is represented as a list of n length.
def distance(a: list[int], b: list[int]) -> list[int]:
assert len(a) == len(b), "Coordinates must be in same dimension."
return [abs((a[i] - b[i])) for i in range(len(a))]
@talhaahussain
talhaahussain / euclidean_distance.py
Created June 13, 2024 02:09
euclidean_distance.py -- A Python program for computing the line segment length between two n-dimensional points in Euclidean space, using Cartesian coordinates and the Pythagorean theorem, where each point is represented as a list of n length.
import math
def distance(a: list[int], b: list[int]) -> list[int]:
assert len(a) == len(b), "Coordinates must be in same dimension."
return math.sqrt(sum([(a[i] - b[i])**2 for i in range(len(a))]))
@talhaahussain
talhaahussain / contiguous_subarrays.py
Last active June 12, 2024 02:49
contiguous_subarrays.py -- A simple, type-hinted solution using list comprehension for generating all contiguous subarrays of any homogeneous or non-homogeneous list across one dimension. Does not return the original list.
def generate_subarrays(a: list[any]) -> list[any]:
return [a[i:j] for i in range(len(a)) for j in range(i + 1, len(a) + 1)]
@talhaahussain
talhaahussain / find_neighbours.py
Last active May 30, 2024 22:13
find_neighbours.py -- A simple, elegant way of finding all neighbours for a cell in a grid, using the Moore neighbourhood or the von Neumann neighbourhood. Assumes an inclusive lower bound and exclusive upper bound for x and y. Useful for cellular automata.
def moore(x, y, lower_x, lower_y, upper_x, upper_y):
neighbours = [(i, j) for i, j in ((x-1, y-1), (x, y-1), (x+1, y-1), (x-1, y), (x+1, y), (x-1, y+1), (x, y+1), (x+1, y+1)) if lower_x<=i<upper_x and lower_y<=j<upper_y]
return neighbours
def von_Neumann(x, y, lower_x, lower_y, upper_x, upper_y):
neighbours = [(i, j) for i, j in ((x, y-1), (x-1, y), (x+1, y), (x, y+1)) if lower_x<=i<upper_x and lower_y<=j<upper_y]
return neighbours
@talhaahussain
talhaahussain / collatz.py
Created May 28, 2024 07:27
collatz.py -- A very simple demonstration of the Collatz conjecture, an unsolved problem in mathematics, in Python. Applies the rules of the conjecture in a repeated loop on some integer x, and outputs the number of steps taken to reach 1.
import math
x = 99 # Select a starting point here
i = 0
running = True
while running:
i += 1
if x % 2 == 0:
x = x / 2
@talhaahussain
talhaahussain / maze-algorithm.py
Created May 25, 2024 13:00
maze-algorithm.py -- A simple, flexible maze generation algorithm implemented in Python. See the original and an application here: https://github.com/talhaahussain/maze-game/
import random # Since the generation of this maze is random, I need the random module.
def generate_maze(x_cells, y_cells):
#x_cells = 20 # This defines the number of cells in the x-direction.
#y_cells = 20 # This defines the number of cells in the y-direction.
grid = [] # This allows me to build the grid from which the maze is generated.
for y in range(y_cells):
row = [] # Creates a 'row' list for all y locations (10 rows)
for x in range(x_cells):
@talhaahussain
talhaahussain / stack.py
Last active June 4, 2024 02:00
stack.py -- An object-oriented, first principles implementation of a stack data structure, written in Python.
class Stack:
def __init__(self):
self.stack = []
self.stacklen = int(input(print("How long should the stack be?")))
def push(self):
if len(self.stack) == self.stacklen:
self.full()
else:
self.stack.append(int(input("Enter a value to append.\n")))