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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / count_array_inversions.py
Created November 26, 2022 21:48
Implement 'MergeSort + Count Inversions' algorithm on a 1-D array
#!/usr/bin/env python3
def merge_count_split_inversions(left_sorted: list, right_sorted: list, lst_len: int) -> tuple:
"""
Perform merge of two sorted lists and count inversions while merging
>>> merge_count_split_inversions([2], [1], 2)
([1, 2], 1)
>>> merge_count_split_inversions([1, 3, 5], [2, 4, 6], 6)
@Per48edjes
Per48edjes / happy_number.py
Created November 29, 2022 03:03
Leetcode 202. Happy Number
def isHappy(n: int) -> bool:
"""
Leetcode 202. Happy number
>>> isHappy(7)
True
>>> isHappy(19)
True
>>> isHappy(2)
False
@Per48edjes
Per48edjes / rotated_sorted_array.py
Last active November 30, 2022 19:24
Count number of times sorted array has been rotated
def num_rotations(nums: list) -> int:
"""
Returns the number of times a sorted array has been rotated to the right
with the numbers wrapping to the beginning when they are rotated at the end
of the list
>>> nums = [3, 4, 5, 1, 2]
>>> num_rotations(nums)
3
>>> nums2 = [1, 2, 3, 4, 5]