From 02d89bde679488e97cdb077c511b3dbfb660e2b8 Mon Sep 17 00:00:00 2001 From: Ajinkya Chikhale <86607732+ajinkyac03@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:42:42 +0530 Subject: [PATCH] Added implementation for Tribonacci sequence using dp (#6356) * Added implementation for Tribonacci sequence using dp * Updated parameter name * Apply suggestions from code review --------- Co-authored-by: Tianyi Zheng --- dynamic_programming/tribonacci.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 dynamic_programming/tribonacci.py diff --git a/dynamic_programming/tribonacci.py b/dynamic_programming/tribonacci.py new file mode 100644 index 000000000..58e15da91 --- /dev/null +++ b/dynamic_programming/tribonacci.py @@ -0,0 +1,24 @@ +# Tribonacci sequence using Dynamic Programming + + +def tribonacci(num: int) -> list[int]: + """ + Given a number, return first n Tribonacci Numbers. + >>> tribonacci(5) + [0, 0, 1, 1, 2] + >>> tribonacci(8) + [0, 0, 1, 1, 2, 4, 7, 13] + """ + dp = [0] * num + dp[2] = 1 + + for i in range(3, num): + dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3] + + return dp + + +if __name__ == "__main__": + import doctest + + doctest.testmod()