Skip to content

Commit 525c90a

Browse files
committed
Last ch. 4 problems
1 parent 2ad9510 commit 525c90a

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

elements-of-programming-interviews/cpp/uniform_random_number.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,27 @@ int ZeroOneRandom() {
1717
}
1818

1919
int UniformRandom(int lower_bound, int upper_bound) {
20-
// TODO - you fill in here.
21-
return 0;
20+
int bound = upper_bound - lower_bound;
21+
int bound_copy = bound;
22+
int bits_needed = 0;
23+
while (bound_copy) {
24+
bound_copy >>= 1;
25+
bits_needed++;
26+
}
27+
while (true) {
28+
int guess = 0;
29+
for (int i = 0; i < bits_needed; i++) {
30+
guess <<= 1;
31+
if (ZeroOneRandom()) {
32+
guess |= 1;
33+
}
34+
}
35+
if (guess <= bound) {
36+
return guess;
37+
}
38+
}
2239
}
40+
2341
bool UniformRandomRunner(TimedExecutor& executor, int lower_bound,
2442
int upper_bound) {
2543
using namespace test_framework;

elements-of-programming-interviews/problem_mapping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ problem_mapping = {
142142
},
143143
"4.10 Generate uniform random numbers": {
144144
"C++: uniform_random_number.cc": {
145-
"passed": 0,
145+
"passed": 7,
146146
"total": 7
147147
},
148148
"Java: UniformRandomNumber.java": {
@@ -164,7 +164,7 @@ problem_mapping = {
164164
"total": 10000
165165
},
166166
"Python: rectangle_intersection.py": {
167-
"passed": 0,
167+
"passed": 10000,
168168
"total": 10000
169169
}
170170
}

elements-of-programming-interviews/python/rectangle_intersection.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,41 @@
66
Rect = collections.namedtuple('Rect', ('x', 'y', 'width', 'height'))
77

88

9+
def left(rect):
10+
return rect.x
11+
12+
13+
def right(rect):
14+
return rect.x + rect.width
15+
16+
17+
def down(rect):
18+
return rect.y
19+
20+
21+
def up(rect):
22+
return rect.y + rect.height
23+
24+
25+
def interval_overlap(x, l, r):
26+
return l <= x <= r
27+
28+
29+
def line_overlap(a1, a2, b1, b2):
30+
return interval_overlap(a1, b1, b2) or \
31+
interval_overlap(a2, b1, b2) or \
32+
interval_overlap(b1, a1, a2) or \
33+
interval_overlap(b2, a1, a2)
34+
35+
936
def intersect_rectangle(r1: Rect, r2: Rect) -> Rect:
10-
# TODO - you fill in here.
11-
return Rect(0, 0, 0, 0)
37+
if not (line_overlap(left(r1), right(r1), left(r2), right(r2)) and
38+
line_overlap(down(r1), up(r1), down(r2), up(r2))):
39+
return Rect(0, 0, -1, -1)
40+
41+
x_vals = sorted([left(r1), right(r1), left(r2), right(r2)])
42+
y_vals = sorted([up(r1), down(r1), up(r2), down(r2)])
43+
return Rect(x_vals[1], y_vals[1], x_vals[2] - x_vals[1], y_vals[2] - y_vals[1])
1244

1345

1446
def intersect_rectangle_wrapper(r1, r2):
@@ -26,6 +58,11 @@ def fmt(x):
2658

2759

2860
if __name__ == '__main__':
61+
"""
62+
r1 = Rect(76, 9, 12, 14)
63+
r2 = Rect(20, 1, 62, 60)
64+
# print(interval_overlap(up(r1), down(r2), up(r2)))
65+
"""
2966
exit(
3067
generic_test.generic_test_main('rectangle_intersection.py',
3168
'rectangle_intersection.tsv',

0 commit comments

Comments
 (0)