This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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... :) | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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., |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isHappy(n: int) -> bool: | |
""" | |
Leetcode 202. Happy number | |
>>> isHappy(7) | |
True | |
>>> isHappy(19) | |
True | |
>>> isHappy(2) | |
False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |