python question Complete following function according to the specification in th
ID: 3722275 • Letter: P
Question
python question
Complete following function according to the specification in the documentation string. You will need to create your own custom exceptions for these functions. # Write your answers here. Define Change ParameterError and ChangeRemainderError in this code cell as well. def changel (a, d): Computes the change of amount a given denominations d. Parameter a must be of type int, d must be of type list of int, and the elements of d must be in ascending order, otherwise ChangeParameterError is raised. The result is a dictionary with keys from d, mapping to values of the number of coins / bills of each denomination. This is computed by first taking the maximal number of coins / bill of the highest denomination, then the next highest, etc. If no exact change is possible, ChangeRemainderError is raised. return change( 3, [3, 7]) == {3: 1, 7:0} change(15, [1, 3, 7]) == {1: 1, 3: 0, 7: 2} try: change (True, [4]) # raises ChangeParameterError except ChangeParameterError: print("Successful test case") try: change (3, 7) # raises ChangeParameterError except ChangeParameterError: print("Successful test case"). try: change (3, (7, True]) # raises Change ParameterError except ChangeParameterError: print("Successful test case"). try: change (3, [7, 3]) # raises ChangeParameterError except ChangeParameterError: print("Successful test case") try: change (3, [2, 7]) # raises ChangeRemainderError except ChangeRemainderError: print("Successful test case")Explanation / Answer
class changeparametererror(Exception):
def __init__(self,e):
self.e = e
if self.e != self.e.sort():
print "error"
if all(isinstance(item, int) for item in self.e):
print "error"
class changeremaindererror(Exception):
def __init__(self,e):
self.e = e
if all(value == 0 for value in self.e.values()):
print "error"
def change(a,d):
b = {}
k = len(d)
while k > 0:
b[d[k-1]] = a // d[k-1]
a = a % d[k-1]
k = k-1
return b
a = [1,2,3]
try:
fin = change(17,a)
except changeparametererror,a:
print "Successful test case"
except changeremaindererror , fin:
print "Successful test case"
print fin
I am not able to get the exception correctly but the logic applied above is correct try to work aroud th syntax for exception in the above code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.