Skip to content

Commit be6ceb1

Browse files
committed
int_as_array_multiply (python, cpp)
1 parent 7eeadd6 commit be6ceb1

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1+
#include <algorithm>
12
#include <vector>
23

34
#include "test_framework/generic_test.h"
45
using std::vector;
56
vector<int> Multiply(vector<int> num1, vector<int> num2) {
6-
// TODO - you fill in here.
7-
return {};
7+
int neg = ((num1[0] < 0) ^ (num2[0] < 0)) ? -1 : 1;
8+
num1[0] = abs(num1[0]);
9+
num2[0] = abs(num2[0]);
10+
11+
// multiplying a m digit number by an n digit number
12+
// will produce a number with no more than m+n digits.
13+
vector<int> result(num1.size() + num2.size(), 0);
14+
for (int i = num1.size() - 1; i >= 0; i--) {
15+
for (int j = num2.size() - 1; j >= 0; j--) {
16+
// This is a clever technique for handling overflow and carry;
17+
// each digit is treated as a ones place integer; any multiplication that
18+
// exceeds 10 (max possible is 9x9=81) adds the 10s place overflow to the
19+
// next digit and uses the ones place for the present digit.
20+
result[i + j + 1] += num1[i] * num2[j];
21+
result[i + j] += result[i + j + 1] / 10;
22+
result[i + j + 1] %= 10;
23+
}
24+
}
25+
26+
// Then just remove the leading zeroes, and return the result.
27+
result = {
28+
find_if_not(result.begin(), result.end(), [](int a) { return a == 0; }),
29+
result.end()};
30+
if (result.empty()) {
31+
return {0};
32+
}
33+
result[0] *= neg;
34+
return result;
835
}
936

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

elements-of-programming-interviews/problem_mapping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ problem_mapping = {
214214
},
215215
"5.03 Multiply two arbitrary-precision integers": {
216216
"C++: int_as_array_multiply.cc": {
217-
"passed": 0,
217+
"passed": 1000,
218218
"total": 1000
219219
},
220220
"Java: IntAsArrayMultiply.java": {
221221
"passed": 0,
222222
"total": 1000
223223
},
224224
"Python: int_as_array_multiply.py": {
225-
"passed": 0,
225+
"passed": 1000,
226226
"total": 1000
227227
}
228228
},

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

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

55

66
def multiply(num1: List[int], num2: List[int]) -> List[int]:
7-
# TODO - you fill in here.
8-
return []
7+
neg = -1 if (num1[0] < 0) ^ (num2[0] < 0) else 1
8+
num1[0] = abs(num1[0])
9+
num2[0] = abs(num2[0])
10+
11+
total = 0
12+
place = 1
13+
for digit1 in num1[::-1]:
14+
curr_place = place
15+
curr_total = 0
16+
for digit2 in num2[::-1]:
17+
curr_total += digit1 * digit2 * curr_place
18+
curr_place *= 10
19+
total += curr_total
20+
place *= 10
21+
22+
solution = [] if total else [0]
23+
while total:
24+
solution.append(total % 10)
25+
total = total // 10
26+
solution[-1] *= neg
27+
28+
return solution[::-1]
929

1030

1131
if __name__ == '__main__':

0 commit comments

Comments
 (0)