In this task, we will develop a program to detect whether a target number is the sum of k elements in the given list num
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
In this task, we will develop a program to detect whether a target number is the sum of k elements in the given list num
Case 2 INPUT: 32 392 93022 349895 -1 1280298482 2 OUTPUT: No Explanation: It print "No" because we cannot find 2 numbers in the given list [32, 293, 93022, 349895] to sum up to 1288298402 Given program 1. number - int(input) 2. numbers - 1 3. while number 1 = -1: 4. numbers.append(number) 5. number - int(input()) 6. target = int(input) 7. k int(input()) 8. 9. 10. def isExist(# define parameters): # your implementation 12. 13. 14. if isExist(# input the appropriate variables): 15. print("Yes") 16. else: 17. print("No") 11. 1. You are expected to use recursion algorithm to solve this problem. It will have a 20% deduction if your submission is not a recursion solution. 2. Hint: If the current number x should be included in finding the target number, then summing up k elements to find the target will be equivalent to summing up [k-1] elements to find (target -x] Similarly, try to figure out the recurrence relationship for the situation where x should not be included in the process. 3. Please read the given code carefully for better understanding. 4. You can start the recursion on is_exist(), or you can define another recursive function and use is_exist() to call it. You may define as many functions as you need but you are not required to do 5. You must show the output of the function using the print) provided above. 6. You are not allowed to change any line of code in the given program. 7. Make sure your program can handle input range 1< < 6, where k is a positive integer. For example, in the above case ([1, 2, 8, 9, 39, 92, 161), 109, 3), the kis 3. 8. You can use any built-in functions in Python. so.