From 0d1c5de6a40cd92d2a9f79f90bebe6611c907f41 Mon Sep 17 00:00:00 2001 From: Akanksha10029 Date: Thu, 26 Oct 2023 15:07:51 +0530 Subject: [PATCH] added a new function called domino_sum and the user is prompted to enter the value of n In the provided code, I added a new function called domino_sum, which calculates the sum of the first n terms in the domino sequence using both the domino and domino_fast functions. The results of this sum are printed in the if name == "main": block. and the user is prompted to enter the value of n, and the code will calculate and display the results for the provided value of n. --- python/domino.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/python/domino.py b/python/domino.py index b28c9a44..6deaa039 100644 --- a/python/domino.py +++ b/python/domino.py @@ -13,6 +13,12 @@ def domino_fast(n): def domino(n): + + if n == 0: + return 0 + elif n == 1: + return 1 + DP = [None] * n DP[0], DP[1] = 1, 1 @@ -21,9 +27,19 @@ def domino(n): return DP[-1] +def domino_sum(n): + sum1 = sum(domino(i) for i in range(1, n + 1)) + sum2 = sum(domino_fast(i) for i in range(1, n + 1)) + return sum1, sum2 if __name__ == "__main__": - result1 = domino(40) - result2 = domino_fast(40) - - print(result1, result2) + n = int(input("Enter the value of n: ")) + result1 = domino(n) + result2 = domino_fast(n) + sum_result1, sum_result2 = domino_sum(n) + + print("Result for domino({}): {}".format(n, result1)) + print("Result for domino_fast({}): {}".format(n, result2)) + print("Sum of the first {} terms using domino: {}".format(n, sum_result1)) + print("Sum of the first {} terms using domino_fast: {}".format(n, sum_result2)) +