Design and code a python solution for the problems. You must come up with your own algorithm and the honor code applies (no references).
It is strongly recommended that you think about your problem-solving steps and verify that you follow the guidelines of the assignment.
You will be graded on the correctness of your recursive algorithm.Algorithms that are not recursive will earn a score of 0/50. Your solution should not use loops.
HINT:Helper functions are allowed.
Problem 1[50 points] Design arecursive algorithmthat will display the sum of the digits of an integer.Submit a python functionsum_digits(number)that returns the sum.
Examples:
sum_digits(126) # returns 9
sum_digits(-49) # returns 13
sum_digits(3) # returns3
Problem 2[50 points] Design arecursive algorithmthat will display whether it is possible to choose two integers from a list of integers such that the difference between the two equals a given value.Hint: You may wish to invoke another algorithm, (i.e., function), that accepts more parameters that perform the recursion. Submit a python functionis_diff_two(values, diff)that take a list and the desired difference as a non-negative integer and returns true or false.
Examples:
is_diff_two([2, 4, 8], 4) # returns True
is_diff_two([1, 2, 4, 8, 1], 7) # returns True
is_diff_two([2, 4, 4, 8], 7) # returns False
is_diff_two([], 7) # returns False
is_diff_two([-1,-3,5,8,10],11) #returns True
Restrictions
Theonlyfunctions you may use are:print, str, int, float, bool, len, list, range, abs, round,andpow.
- Note that you may not use list slicing (AKA colons in your indices).
- We will not deduct for the use of str(), but you do not actually need it.
- Donotimport libraries.
Submission
- Upload yourHW6.pyfile to Web-CAT via your browser(not eclipse)
- There is a hard limit of 5 submissions to Web-CAT.
- We suggest you run your own tests before submitting them, but thenremovethese before submitting because they will use Web-CAT server time.
Grading
The code should be submitted to Web-CAT for testing. GTAs will award up to 15 additional points after reviewing your code.
They will also deduct points for code that doesn't comply with the above instructions.
Problem 1: 50 pts total, max penalty is -50
- P1: -50 for using forbidden functions.
- P1: -25 for not using any recursion.(up to -50 no matter what this is in combination with)*
- P1: -25 for using ‘for’ or ‘while’ loops.
- P1: -10 for a complex solution that is quite hard to follow. ( use good commenting, good variables names, and concise code that clearly progresses towards the solution)
- P1:-10 for list functions such as append(), prepend(), pop(), etc.
- P1: -5 for using list slicing (e.g., for list a, a[start:end], or a[start:], or a[:end], or a[:]).
Problem 2: 50 pts total, max penalty is -50
- P2: -50 for using forbidden functions.
- P2: -25 for not using any recursion.(up to -50 no matter what this missing recursion is in combination with)*
- P2: -25 for using ‘for’ or ‘while’ loops.
- P2: -25 for using slicing.
- P2: -10 for list functions such as append(), prepend(), pop(), etc.
- P2: -10 for a complex solution that is quite hard to follow. (Use good commenting, good variables names, and concise code that clearly progresses towards the solution.)