Skip to content

Commit

Permalink
Refactored solutions 251 - 252
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent d897fe8 commit 0874b27
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
30 changes: 18 additions & 12 deletions Solutions/251.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"""
Problem:
Given an array of a million integers between zero and a billion, out of order, how can you efficiently sort it?
Assume that you cannot store an array of a billion elements in memory.
Given an array of a million integers between zero and a billion, out of order, how can
you efficiently sort it? Assume that you cannot store an array of a billion elements in
memory.
"""

from random import randint
from typing import List


def gen_arr():
# function to generate the required array
def gen_arr() -> List[int]:
return [randint(0, 1_000_000_000) for _ in range(1_000_000)]


def countingSort(arr, exp, n):
def counting_sort(arr: List[int], exp: int, n: int) -> int:
# output array elements to store the sorted arr
output = [0] * n
count = [0] * 10
Expand All @@ -33,17 +34,22 @@ def countingSort(arr, exp, n):
arr[i] = output[i]


# Method to do Radix Sort
def radixSort(arr):
def radix_sort(arr: List[int]) -> None:
length = len(arr)
# find the number digits in the largest number (generalized for any array)
digits = len(str(max(arr)))
# perform counting sort for every digit
exp = 1
for _ in range(digits):
countingSort(arr, exp, length)
counting_sort(arr, exp, length)
exp *= 10


# DRIVER CODE
radixSort(gen_arr())
if __name__ == "__main__":
radix_sort(gen_arr())


"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(range of the numbers)
"""
42 changes: 27 additions & 15 deletions Solutions/252.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
"""
Problem:
The ancient Egyptians used to express fractions as a sum of several terms where each numerator is one.
Create an algorithm to turn an ordinary fraction a / b, where a < b, into an Egyptian fraction.
For example, 4 / 13 can be represented as 1 / 4 + 1 / 18 + 1 / 468.
The ancient Egyptians used to express fractions as a sum of several terms where each
numerator is one. For example, 4 / 13 can be represented as
1 / (4 + 1 / (18 + (1 / 468))).
Create an algorithm to turn an ordinary fraction a / b, where a < b, into an Egyptian
fraction.
"""

from fractions import Fraction
from math import ceil
from typing import List


def get_egyptian_frac(
fraction: Fraction, previous_fraction: List[Fraction] = list()
) -> List[Fraction]:
if fraction.numerator == 1:
previous_fraction.append(fraction)
return previous_fraction

egyptian_fraction = Fraction(1, ceil(fraction.denominator / fraction.numerator))
previous_fraction.append(egyptian_fraction)
return get_egyptian_frac(fraction - egyptian_fraction, previous_fraction)


def get_egyptian_frac(frac, prev_frac=list()):
# base case for recursion
if frac.numerator == 1:
prev_frac.append(frac)
return prev_frac
# generating the next fraction
egyptian_frac = Fraction(1, ceil(frac.denominator / frac.numerator))
prev_frac.append(egyptian_frac)
# calling the function recursively
return get_egyptian_frac(frac - egyptian_frac, prev_frac)
if __name__ == "__main__":
print(get_egyptian_frac(Fraction(4, 13)))


# DRIVER CODE
print(get_egyptian_frac(Fraction(4, 13)))
"""
SPECS:
TIME COMPLEXITY: O(log(n))
SPACE COMPLEXITY: O(log(n))
"""

0 comments on commit 0874b27

Please sign in to comment.