Skip to content

Commit f28b563

Browse files
author
Jessica Yung
committed
feat(icake): add code for problems 5-7
1 parent 4a4351b commit f28b563

File tree

11 files changed

+240
-131
lines changed

11 files changed

+240
-131
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

interview-cake/p1_to_p5.py

Lines changed: 0 additions & 127 deletions
This file was deleted.

interview-cake/p4.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
1717
1818
"""
19-
import unittest
2019

2120

2221
def condense_meeting_times_alt(meeting_times):
@@ -64,6 +63,3 @@ def action_two_tuples(first_index, second_index, tuples_list):
6463
if second[1] >= first[0]:
6564
# TODO: list append merged, delete prev
6665
merge(second_index, first_index)
67-
68-
if __name__ == '__main__':
69-
unittest.main()

interview-cake/p5-memoization.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
3+
4+
class Fibonacci:
5+
6+
def __init__(self):
7+
self.memo = {}
8+
9+
def fib(self, n):
10+
"""Computes the nth Fibonacci number."""
11+
if n < 0:
12+
raise Exception("Index was negative. There are no negative-index Fibonacci numbers.")
13+
14+
# Base cases
15+
elif n == 0 or n == 1:
16+
return 1
17+
# Non-base cases
18+
elif n in self.memo:
19+
print("Fetch fib for %i" % n)
20+
return self.memo[n]
21+
22+
print("Compute fib %i" % n)
23+
result = self.fib(n-1) + self.fib(n-2)
24+
25+
# Memo-ize result
26+
self.memo[n] = result
27+
28+
return result
29+
30+
31+
class Tests(unittest.TestCase):
32+
33+
def test_fibonacci(self):
34+
self.assertEqual(Fibonacci().fib(10), 89)
35+
self.assertEqual(Fibonacci().fib(1), 1)
36+
self.assertEqual(Fibonacci().fib(0), 1)
37+
self.assertEqual(Fibonacci().fib(5), 8)
38+
39+
40+
if __name__ == '__main__':
41+
unittest.main()

interview-cake/p5.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Problem 5
3+
4+
Also covers:
5+
- Memo-ization (Fibonacci numbers example)
6+
7+
8+
"""
9+
10+
11+
def combinations(amount, denominations):
12+
sorted_denoms = sorted(denominations)
13+
combos = 0
14+
if sorted_denoms[0] > amount:
15+
return combos
16+
if divmod(amount, denominations[0])[1] == 0:
17+
pass
18+
"""
19+
for multiples in range(divmod(amount, min)[0])
20+
Try divmod(amount, min)[0] min.
21+
22+
if divmod(amount, min)[1] == 0:
23+
combos += 1
24+
# Then Try divmod(amount, min)[0] -2
25+
26+
Try (#smallest you can fit - 2) smallest + 1 second smallest
27+
if too small, try + 1 third smallest... until greater than or equal to
28+
if equal,
29+
Try (#smallest
30+
"""
31+
32+
33+
number_of_ways = 0
34+
35+
36+
def num_ways_top_down(amount, denominations, current_index=0,):
37+
38+
# Previously got to exact amount
39+
if amount == 0:
40+
return 1
41+
# Overshot: Used too many coins
42+
if amount < 0:
43+
return 0
44+
# Used all denominations, so no remaining ways to resolve
45+
if current_index == len(denominations):
46+
return 0
47+
48+
# Choose a current denomination (using current_index)
49+
current_denomination = denominations[current_index]
50+
51+
num_possibilities = 0
52+
while amount >= 0:
53+
num_possibilities += num_ways_top_down(amount, denominations, current_index+1)
54+
amount -= current_denomination
55+
56+
for denomination in denominations:
57+
# for each number_of_times_to_use_denomination in possible_number_of_times_to_use_denom_without_overshooting_amount:
58+
for times_to_use in range(divmod(amount, denomination)[0]):
59+
number_of_ways += num_ways(remaining_amount, denominations)
60+
61+
# Might there not be double counting?
62+
63+

interview-cake/p6.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def rectangular_intersection(rect1, rect2):
2+
"""rect1 and rect2 are dictionaries."""
3+
4+
# Initialise rectangle
5+
rect_intersection = {}
6+
7+
# Put x coordinates into an array
8+
x_coordinates = [rect1['left_x'], rect1['left_x'] + rect1['width'], rect2['left_x'], rect2['left_x'] + rect2['width']]
9+
10+
# Check two rectangles intersect in x
11+
if x_coordinates[1] < x_coordinates[2] and x_coordinates[3] < x_coordinates[1]:
12+
return False
13+
14+
# Add rectangular intersection x values to rect_intersection dictionary
15+
x_coordinates = sorted(x_coordinates)
16+
rect_intersection['left_x'] = x_coordinates[1]
17+
rect_intersection['width'] = x_coordinates[2] - x_coordinates[1]
18+
19+
# Put y coordinates into an array
20+
y_coordinates = [rect1['bottom_y'], rect1['bottom_y'] + rect1['height'], rect2['bottom_y'], rect2['bottom_y'] + rect2['height']]
21+
22+
# Check two rectangles intersect in y
23+
if y_coordinates[1] < y_coordinates[2] and y_coordinates[3] < y_coordinates[0]:
24+
return False
25+
26+
# Add rectangular intersection y values to rect_intersection dictionary
27+
y_coordinates = sorted(y_coordinates)
28+
rect_intersection['bottom_y'] = y_coordinates[1]
29+
rect_intersection['height'] = y_coordinates[2] - y_coordinates[1]
30+
31+
"""
32+
Dumb way of doing it:
33+
34+
if rect1['left_x'] < rect2['left_x']:
35+
left_rect = rect1
36+
right_rect = rect2
37+
elif rect1['left_x'] == rect2['left_x']:
38+
if rect1['width'] <= rect2['width']:
39+
left_rect = rect1
40+
right_rect = rect2
41+
else:
42+
left_rect = rect2
43+
right_rect = rect1
44+
else:
45+
left_rect = rect2
46+
right_rect = rect1
47+
48+
# Find x intersection
49+
if left_rect['left_x'] + left_rect['width'] < right_rect['left_x']:
50+
return False
51+
"""

interview-cake/p7.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class TempTracker:
2+
3+
def __init__(self):
4+
5+
# For mean
6+
self.temp_count = 0
7+
self.mean = None
8+
self.sum = 0.0
9+
10+
# For max
11+
self.max = None
12+
13+
# For min
14+
self.min = None
15+
16+
# For mode
17+
self.mode = None
18+
self.occurrences = [0] * 111
19+
self.max_occurrences = 0
20+
21+
def insert(self, temp):
22+
23+
# Update mean
24+
self.temp_count += 1
25+
self.sum += temp
26+
self.mean = self.sum / self.temp_count
27+
28+
# Update max and min
29+
if (self.max is None) or (temp > self.max):
30+
self.max = temp
31+
if (self.min is None) or (temp < self.min):
32+
self.min = temp
33+
34+
# Update mode
35+
self.occurrences[temp] += 1
36+
if self.occurrences[temp] > self.max_occurrences:
37+
self.max_occurrences = self.occurrences[temp]
38+
self.mode = temp
39+
40+
def get_max(self):
41+
return self.max
42+
43+
def get_min(self):
44+
return self.min
45+
46+
def get_mean(self):
47+
return self.mean
48+
49+
def get_mode(self):
50+
return self.mode

interview-cake/rough.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def find_linear_regression_line(points):
2+
# Separate points into X and y to fit LinearRegression model
3+
points_x = [[point[0]] for point in points]
4+
points_y = [point[1] for point in points]
5+
print("X points: ", points_x, "Length: ", len(points_x))
6+
print("Y points: ", points_y, "Length: ", len(points_y))
7+
8+
# Fit points to LinearRegression line
9+
clf = LinearRegression().fit(points_x, points_y)
10+
11+
# Get parameters from line
12+
coef = clf.coef_[0]
13+
intercept = clf.intercept_
14+
print("Coefficients: ", coef, "Intercept: ", intercept)
15+
return coef, intercept
16+
17+
def intersection_x(coef1, intercept1, coef2, intercept2):
18+
"""Returns x-coordinate of intersection of two lines."""
19+
x = (intercept2-intercept1)/(coef1-coef2)
20+
return x
21+
22+
def draw_linear_regression_line(coef1, intercept1, intersection_x, imshape=[540,960]):
23+
24+
# Get starting and ending points of regression line, ints.
25+
point_one = (int(intersection_x), int(intersection_x * coef1 + intercept1))
26+
print("Point one: ", point_one)
27+
point_two = (imshape[1], int(imshape[1] * coef1 + intercept1))
28+
print("Point one: ", point_one, "Point two: ", point_two)
29+
# Draw line using cv2.line
30+
cv2.line(img, point_one, point_two, color, thickness)

mathematics/__init__.py

Whitespace-only changes.

programming-flashcards.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sorting takes O(?) time; O(nlogn)

0 commit comments

Comments
 (0)