-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
pascal_triangle.cc
32 lines (26 loc) · 1006 Bytes
/
pascal_triangle.cc
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
#include <vector>
#include "test_framework/generic_test.h"
using std::vector;
vector<vector<int>> GeneratePascalTriangle(int num_rows) {
vector<vector<int>> pascal_triangle;
for (int i = 0; i < num_rows; ++i) {
vector<int> curr_row;
for (int j = 0; j <= i; ++j) {
// Sets this entry to the sum of the two above adjacent entries if they
// exist.
curr_row.emplace_back(0 < j && j < i ? pascal_triangle.back()[j - 1] +
pascal_triangle.back()[j]
: 1);
}
pascal_triangle.emplace_back(curr_row);
}
return pascal_triangle;
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"num_rows"};
return GenericTestMain(args, "pascal_triangle.cc", "pascal_triangle.tsv", &GeneratePascalTriangle,
DefaultComparator{}, param_names);
}
// clang-format on