Last active
November 2, 2022 16:26
-
-
Save epignatelli/3db3d5b6ebc7af81ca2f061338ed0d92 to your computer and use it in GitHub Desktop.
Useful coding interview utility functions
This file contains 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
import decimal | |
import math | |
from typing import List | |
def is_prime(n: int) -> bool: | |
if n == 0: | |
return False | |
if n == 1: | |
return False | |
if n == 2: | |
return True | |
if n == 3: | |
return True | |
prime = True | |
for i in range(3, n): | |
if n % 2 == 0: | |
return False | |
if n % 3 == 0: | |
return False | |
if n % i == 0: | |
return False | |
return True | |
def pi_digits(): | |
pi = decimal.Decimal(math.pi) | |
pi_digits = list(map(int, str(pi).replace(".", ""))) | |
return pi_digits | |
def fizzbuzz(n: int) -> List[str]: | |
words = [] | |
for i in range(1, n + 1): | |
word = None | |
if (i % 3) == 0 and (i % 5) != 0: | |
word = "Fizz" | |
elif (i % 3) != 0 and (i % 5) == 0: | |
word = "Buzz" | |
elif (i % 3) == 0 and (i % 5) == 0: | |
word = "FizzBuzz" | |
if word is not None: | |
words.append(word) | |
print(i, word) | |
return words | |
def traverse_binary_tree(arr): | |
if len(arr) == 0: | |
return "" | |
depth = math.floor(math.log2(len(arr))) | |
max_idx = len(arr) - 1 | |
for i in range(1, depth + 1): | |
start = 2**i - 1 | |
end = 2**(i+1) - 1 | |
mid = (end - start // 2) + 1 | |
arr_level = arr[start:end] | |
print(arr[start:end]) | |
left = arr_level[start:mid] | |
right = arr_level[mid: end] | |
def multiples_of(number, max_n): | |
multiples = [] | |
for i in range(max_n): | |
mult = i * number | |
if mult <= max_n: | |
multiples.append(mult) | |
return multiples | |
def multiples_of_many(numbers, max_n): | |
multiples = [] | |
for number in numbers: | |
multiples += multiples_of(number, max_n) | |
return set(multiples) | |
def fibonacci(max_value): | |
series = [1, 2] | |
while series[-1] < max_value: | |
next = series[-2] + series[-1] | |
if next > max_value: | |
break | |
series.append(next) | |
return series | |
def fibonacci_sum(max_value): | |
series = [1, 2] | |
result = sum(series) | |
while series[-1] < max_value: | |
next = series[-2] + series[-1] | |
if next > max_value: | |
break | |
series = [series[-1], next] | |
result += next | |
return result | |
def square_of_sum_minus_sum_of_squares(n): | |
s = 0 | |
for i in range(n): | |
for j in range(i, n + 1): | |
if i == j: | |
continue | |
s -= 2 * i * j | |
return s | |
def factorise(n): | |
factors = [] | |
while n > 1: | |
for i in range(2, n + 1): | |
if n % i == 0: | |
print(n, i) | |
n = n // i | |
print(is_prime(i)) | |
factors += [i] | |
break | |
return factors | |
def is_palindrome(n: int): | |
n_str = str(n) | |
return n_str == n_str[::-1] | |
def integers_with_n_digits(n: int): | |
return list(range(int(10 ** (n - 1)), int(10 ** (n)))) | |
def largest_palindrom_prod_of_two_n_digits(): | |
numbers = integers_with_n_digits(3) | |
i = numbers[-1] | |
j = numbers[-1] | |
maximum = -1 | |
for i in reversed(numbers): | |
for j in reversed(numbers): | |
if is_palindrome(i * j): | |
maximum = max(maximum, i * j) | |
return maximum | |
def occurrencies(list_of_values): | |
occ = {} | |
for value in sorted(set(list_of_values)): | |
occ[value] = list_of_values.count(value) | |
return occ | |
def least_common_multiple(divisors): | |
common_factors = {} | |
for d in divisors: | |
factors = occurrencies(factorise(d)) | |
for k, v in factors.items(): | |
current = common_factors.get(k, 0) | |
if v > current: | |
common_factors[k] = v | |
prod = 1 | |
for k, v in common_factors.items(): | |
prod *= k ** v | |
return prod |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment