Skip to content

Commit 7d4562d

Browse files
[test/feat]: Add self-test implementations and...
...namespace (`dynamic_programming`). Thanks to @manncodes for the idea and help! Co-authored-by: Mann Patel <manncodes@users.noreply.github.com>
1 parent 2c12a59 commit 7d4562d

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

dynamic_programming/longest_increasing_subsequence.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111
* @author [David Leal](https://github.com/Panquesito7)
1212
*/
1313

14+
#include <cassert> /// for assert
1415
#include <iostream> /// for IO operations
1516
#include <climits> /// for std::max
1617
#include <vector> /// for std::vector
1718

19+
/**
20+
* @namespace dynamic_programming
21+
* @brief Dynamic Programming algorithms
22+
*/
23+
namespace dynamic_programming {
1824
/**
1925
* @brief Calculate the longest increasing subsequence for the specified numbers
2026
* @param a the array used to calculate the longest increasing subsequence
2127
* @param n the size used for the arrays
2228
* @returns the length of the longest increasing
2329
* subsequence in the `a` array of size `n`
2430
*/
25-
int LIS(const std::vector<uint64_t> &a, const uint32_t &n) {
31+
uint64_t LIS(const std::vector<uint64_t> &a, const uint32_t &n) {
2632
std::vector<int> lis(n);
2733
for (int i = 0; i < n; ++i) {
2834
lis[i] = 1;
@@ -39,6 +45,21 @@ int LIS(const std::vector<uint64_t> &a, const uint32_t &n) {
3945
}
4046
return res;
4147
}
48+
} // namespace dynamic_programming
49+
50+
/**
51+
* @brief Self-test implementations
52+
* @returns void
53+
*/
54+
static void test(){
55+
std::vector<uint64_t> a = {15,21,2,3,4,5,8,4,1,1};
56+
uint32_t n = a.size();
57+
58+
uint32_t result = dynamic_programming::LIS(a, n);
59+
assert(result == 5); ///< The longest increasing subsequence is `{2,3,4,5,8}`
60+
61+
std::cout << "Self-test implementations passed!" << std::endl;
62+
}
4263

4364
/**
4465
* @brief Main function
@@ -59,6 +80,8 @@ int main(int argc, char const *argv[]) {
5980
std::cin >> a[i];
6081
}
6182

62-
std::cout << "The result is: " << LIS(a, n) << std::endl;
83+
std::cout << "\nThe result is: " << dynamic_programming::LIS(a, n) << std::endl;
84+
test(); // run self-test implementations
85+
6386
return 0;
6487
}

0 commit comments

Comments
 (0)