Write a python function Sum(x) which takes a comma-separated string of numbers and returns the reverse of the sum of the smallest and largest numbers in the given list as shown in example. For ex , for the list 10,28,43,52 the sum of smallest and largest numbers is 62 and the reverse is 26. Write a separate recursive function Reverse-Sum(n)
to compute the reverse of a number and call this function inside the Sum(x). Do not use
input() function, specify the input in fixed form.
def Sum(s):
L = [ int(w) for w in s.split(',') ]
xMin = L[0]
xMax = L[0]
for x in L:
if x < xMin:
xMin = x
if x > xMax:
xMax = x
sum = str(xMax + xMin)
r_sum = ReverseSum(sum)
return int(r_sum)
def ReverseSum(s):
if len(s) <= 1:
return s
sub_s = s[1:-1]
res = s[0] + ReverseSum(sub_s) + s[-1]
return s
def main():
x = Sum('10,28,43,52')
print(x)
main()
Comments
Leave a comment