Skip to content

Commit 7eeadd6

Browse files
committed
EPI: Dutch flag, and int inc as array (C++ and Py)
1 parent 5ee7b99 commit 7eeadd6

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,36 @@
77
using std::vector;
88
enum class Color { kRed, kWhite, kBlue };
99

10+
int pivotSort(int left, const Color& val, vector<Color>& A) {
11+
int curr = left;
12+
while (curr < A.size()) {
13+
if (A[curr] == val) {
14+
Color temp = A[curr];
15+
A[curr] = A[left];
16+
A[left] = temp;
17+
left++;
18+
}
19+
curr++;
20+
}
21+
return left;
22+
}
23+
1024
void DutchFlagPartition(int pivot_index, vector<Color>* A_ptr) {
11-
// TODO - you fill in here.
25+
/*
26+
This solution kind of sucks because the problem kind of sucks; as stated,
27+
we want to sort A into [less than pivot, equal to pivot, greater than pivot].
28+
However, The test cases here are based on the dutch flag, not arbitrary
29+
integers - so the end result is the Dutch flag no matter what, and this code
30+
directly solves that (with two passes) by hardcoding the given colors. This
31+
approach would not work for a more general Dutch Flag sort akin to quicksort's
32+
pivot. See my Python solution for a better approach.
33+
*/
34+
vector<Color>& A = *A_ptr;
35+
int i = pivotSort(0, Color::kRed, A);
36+
pivotSort(i, Color::kWhite, A);
1237
return;
1338
}
39+
1440
void DutchFlagPartitionWrapper(TimedExecutor& executor, const vector<int>& A,
1541
int pivot_idx) {
1642
vector<Color> colors;

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,36 @@
22

33
#include "test_framework/generic_test.h"
44
using std::vector;
5+
vector<int> PlusOneFirstAttempt(vector<int> A) {
6+
vector<int> result;
7+
bool carry = true;
8+
9+
for (int i = A.size() - 1; i >= 0; i--) {
10+
int val = carry ? (A[i] + 1) % 10 : A[i];
11+
result.push_back(val);
12+
carry = (carry && (A[i] == 9));
13+
}
14+
if (carry) {
15+
result.push_back(1);
16+
}
17+
std::reverse(result.begin(), result.end());
18+
return result;
19+
}
20+
521
vector<int> PlusOne(vector<int> A) {
6-
// TODO - you fill in here.
7-
return {};
22+
int i = A.size() - 1;
23+
bool carry = true;
24+
25+
while (i >= 0) {
26+
A[i] = (A[i] + (carry ? 1 : 0)) % 10;
27+
carry = carry && (A[i] == 0);
28+
i--;
29+
}
30+
if (carry) {
31+
A[0] = 1;
32+
A.push_back(0);
33+
}
34+
return A;
835
}
936

1037
int main(int argc, char* argv[]) {

elements-of-programming-interviews/problem_mapping.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,29 @@ problem_mapping = {
186186
},
187187
"5.01 The Dutch national flag problem": {
188188
"C++: dutch_national_flag.cc": {
189-
"passed": 1,
189+
"passed": 204,
190190
"total": 204
191191
},
192192
"Java: DutchNationalFlag.java": {
193193
"passed": 0,
194194
"total": 204
195195
},
196196
"Python: dutch_national_flag.py": {
197-
"passed": 0,
197+
"passed": 204,
198198
"total": 204
199199
}
200200
},
201201
"5.02 Increment an arbitrary-precision integer": {
202202
"C++: int_as_array_increment.cc": {
203-
"passed": 0,
203+
"passed": 505,
204204
"total": 505
205205
},
206206
"Java: IntAsArrayIncrement.java": {
207207
"passed": 0,
208208
"total": 505
209209
},
210210
"Python: int_as_array_increment.py": {
211-
"passed": 0,
211+
"passed": 505,
212212
"total": 505
213213
}
214214
},

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,22 @@
88
RED, WHITE, BLUE = range(3)
99

1010

11+
def pivot_sort(A, left, test_fn):
12+
curr = left
13+
while curr < len(A):
14+
if test_fn(A[curr]):
15+
tmp = A[left]
16+
A[left] = A[curr]
17+
A[curr] = tmp
18+
left += 1
19+
curr += 1
20+
return left
21+
22+
1123
def dutch_flag_partition(pivot_index: int, A: List[int]) -> None:
12-
# TODO - you fill in here.
24+
val = A[pivot_index]
25+
left = pivot_sort(A, 0, lambda x: x < val)
26+
pivot_sort(A, left, lambda x: x == val)
1327
return
1428

1529

@@ -40,7 +54,15 @@ def dutch_flag_partition_wrapper(executor, A, pivot_idx):
4054

4155

4256
if __name__ == '__main__':
57+
import random
58+
arr = [random.randint(-100, 100) for _ in range(100)]
59+
partition = 20
60+
print(f"Partitioning on {arr[partition]}")
61+
dutch_flag_partition(partition, arr)
62+
print(arr)
63+
"""
4364
exit(
4465
generic_test.generic_test_main('dutch_national_flag.py',
4566
'dutch_national_flag.tsv',
4667
dutch_flag_partition_wrapper))
68+
"""

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55

66
def plus_one(A: List[int]) -> List[int]:
7-
# TODO - you fill in here.
8-
return []
7+
# haha list comprehension go brrr
8+
# (see my c++ solution for a more traditional answer)
9+
val = int("".join([str(val) for val in A])) + 1
10+
return [int(char) for char in list(str(val))]
911

1012

1113
if __name__ == '__main__':

0 commit comments

Comments
 (0)