Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / karatsuba_multiplication.py
Created November 26, 2022 05:05
Recursive Python implementation of Karatsuba multiplication
#!/usr/bin/env python3
def karatsuba_mul(m: int, n: int) -> int:
"""
Implementation of recursive Karatsuba multiplication
>>> karatsuba_mul(56, 12)
672
>>> karatsuba_mul(78, 34)
@Per48edjes
Per48edjes / catalan_numbers.py
Created November 3, 2022 03:35
Exploring recursive + closed form solutions to generating Catalan numbers
"""
Inspired by Problem 3, Lab 09 from Berkeley's CS 61A course to investigate and
implement the closed form solution via `catalan_nums` function
"""
from math import comb
def num_trees(n):
"""Returns the number of unique full binary trees with exactly n leaves. E.g.,
@Per48edjes
Per48edjes / generating_subsets.py
Created October 31, 2022 22:36
Using generators, recursion, and decorators to return all even subsets of a sequence
#!/usr/bin/env python3
"""
Problem inspired by "Even Subsets" found here:
https://cs61a.org/assets/slides/28-Scheme_Lists_full.pdf
Playing around with recursion, generators, and decorators...of course, this
is completely for my own edification. I would not recommend writing code like
this in an applied setting... :)
"""
@Per48edjes
Per48edjes / leap_year.py
Last active October 21, 2022 23:41
Function to check if Gregorian calendar year is a leap year in Python
def is_leap_year(n: int) -> bool:
"""
Returns whether an year `n` AD is a leap year per Gregorian calendar.
Definitionally, this function checks whether `n` is divisible by 400 or
divisible by 4 and NOT 100.
>>> is_leap_year(2000)
True
>>> is_leap_year(1896)
@Per48edjes
Per48edjes / fast_exponentiation.c
Created September 10, 2022 22:01
Implementation of 'fast exponentiation' algorithm in C & Python
#include <stdint.h>
#include <stdio.h>
double fast_exp(double base, uint64_t power)
{
double result = 1;
while (power > 0)
{
result *= (power & 1) ? base : 1;
power >>= 1;
@Per48edjes
Per48edjes / split_chocolate_bar.py
Created August 23, 2022 13:58
Two implementations of splitting a bar into unit pieces
def split_chocolate_bar(m: int, n: int) -> int:
"""
Count the number of splits required to divide `m` by `n` chocolate bar into
`mn` unit squares in O(mn) time complexity
>>> split_chocolate_bar(1,1)
0
>>> split_chocolate_bar(2, 1)
1
>>> split_chocolate_bar(1, 2)
@Per48edjes
Per48edjes / integer_factorization.py
Last active September 12, 2022 06:15
Prime factorization-related functions
from math import sqrt
from typing import List, Tuple, Union
def gcd(a: int, b: int) -> int:
"""
Return the greatest common divisor of integers `a` and `b`
>>> gcd(3, 6)
3
@Per48edjes
Per48edjes / digital_root.py
Last active August 23, 2022 13:59
Recursively calculate the digital root of a non-negative integer
def digital_root(n: int, original=None) -> int:
"""
Return the digital root of a non-negative integer `n`. Note that this is
basically the same as `n % 9` except in the special case when `n` is a
multiple of 9.
@param n: A non-negative integer
@type n: int
@return: The digital root of `n`
@Per48edjes
Per48edjes / merge_ordered_lists.py
Last active August 3, 2022 01:48
Merge arbitrary number of ordered lists via recursion + reduction
from functools import reduce
def merge(lst1, lst2):
"""Merges two sorted lists.
>>> s1 = [1, 3, 5]
>>> s2 = [2, 4, 6]
>>> merge(s1, s2)
[1, 2, 3, 4, 5, 6]
>>> s1
@Per48edjes
Per48edjes / euclid_algorithm.py
Last active October 23, 2022 02:39
Recursive implementation of Euclid's algorithm to compute GCD of two integers
from typing import Tuple
def gcd(a: int, b: int) -> int:
"""
Simple recursive implementation of Euclidean algorithm
"""
a, b = abs(a), abs(b)
if b == 0:
return a