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
""" | |
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 | |
""" | |
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
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
#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 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
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 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` |
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 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 |
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 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 |