-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweighted_sum_integer_list.cpp
78 lines (54 loc) · 1.77 KB
/
weighted_sum_integer_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Given a nested list of integers, return the sum of all
* integers in the list weighted by their depth.
*
* Example: Given {{1,1},2,{1,1}}, return 10
* (four 1s at depth 2 + one 2 at depth 1)
*/
#include <iostream>
#include <vector>
using namespace std;
int char_to_int(char c) {
return c - '0';
}
int list_sum(string input) {
int depth = 0, sum = 0, cur_val = 0;
for(int i = 0; i < input.length(); i++) {
//if at end of int (either , or })
if(input[i] == ',' || input[i] == '}') {
//add to total
sum += cur_val * depth;
cur_val = 0;
}
//update depth if at brace
if(input[i] == '{')
depth++;
else if(input[i] == '}')
depth--;
else if(input[i] == ',')
continue;
//if we're looking at an int
else {
int tmp = char_to_int(input[i]);
cur_val *= 10; //shift number
cur_val += tmp;
}
}
return sum;
}
int main() {
//assume input is well formed
vector<pair<string, int>> inputs;
inputs.push_back({"{{1,1},2,{1,1}}", 10});
inputs.push_back({"{{1,1},2,{1,1}}", 10});
inputs.push_back({"{1,2,3,4,5}", 15});
inputs.push_back({"{{{{{1}}}}}", 5});
inputs.push_back({"{}", 0});
inputs.push_back({"{1,{2,{3,{4,{5}}}}}", 55});
for(int i = 0; i < inputs.size(); i++) {
int sum = list_sum(inputs[i].first);
if(sum == inputs[i].second)
cout << "CORRECT: sum of " << inputs[i].first << " is " << sum << endl;
else
cout << "WRONG: sum of " << inputs[i].first << " should be " << inputs[i].second << " (function returned " << sum << ")" << endl;
}
}