From 7d4562d6ebd47ad487392b23e9dd8f262843faa9 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 8 Jun 2021 13:11:06 -0500 Subject: [PATCH] [test/feat]: Add self-test implementations and... ...namespace (`dynamic_programming`). Thanks to @manncodes for the idea and help! Co-authored-by: Mann Patel --- .../longest_increasing_subsequence.cpp | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.cpp b/dynamic_programming/longest_increasing_subsequence.cpp index 2d0d03ea7..e3bb42e1a 100644 --- a/dynamic_programming/longest_increasing_subsequence.cpp +++ b/dynamic_programming/longest_increasing_subsequence.cpp @@ -11,10 +11,16 @@ * @author [David Leal](https://github.com/Panquesito7) */ +#include /// for assert #include /// for IO operations #include /// for std::max #include /// for std::vector +/** + * @namespace dynamic_programming + * @brief Dynamic Programming algorithms + */ +namespace dynamic_programming { /** * @brief Calculate the longest increasing subsequence for the specified numbers * @param a the array used to calculate the longest increasing subsequence @@ -22,7 +28,7 @@ * @returns the length of the longest increasing * subsequence in the `a` array of size `n` */ -int LIS(const std::vector &a, const uint32_t &n) { +uint64_t LIS(const std::vector &a, const uint32_t &n) { std::vector lis(n); for (int i = 0; i < n; ++i) { lis[i] = 1; @@ -39,6 +45,21 @@ int LIS(const std::vector &a, const uint32_t &n) { } return res; } +} // namespace dynamic_programming + +/** + * @brief Self-test implementations + * @returns void + */ +static void test(){ + std::vector a = {15,21,2,3,4,5,8,4,1,1}; + uint32_t n = a.size(); + + uint32_t result = dynamic_programming::LIS(a, n); + assert(result == 5); ///< The longest increasing subsequence is `{2,3,4,5,8}` + + std::cout << "Self-test implementations passed!" << std::endl; +} /** * @brief Main function @@ -59,6 +80,8 @@ int main(int argc, char const *argv[]) { std::cin >> a[i]; } - std::cout << "The result is: " << LIS(a, n) << std::endl; + std::cout << "\nThe result is: " << dynamic_programming::LIS(a, n) << std::endl; + test(); // run self-test implementations + return 0; }