You are on page 1of 2

Task 1:

Take an input from user in string format “12,3,6,9,7,56,7,96,4” and pass that value to a recursive
function which will keep adding the values of that string

“12,3,6,9,7,56,7,96,4”

“15,6,9,7,56,7,96,4”

“21,9,7,56,7,96,4”

“30,7,56,7,96,4”

“37,56,7,96,4”

Conditions:

1. No loops in the function


2. You can’t slice more then 2 values at the time
3. You can’t convert that string into a composite data type(list,dict,tuple,set)

CODE:
def addition(mystring):
if mystring!= ' ':
result = mystring.split(',', maxsplit = 2)
if len(result) > 1:
answer = int(result[0]) + int(result[1])
if len(result) > 2:
mystring = str(answer)+ ',' +result[2]
addition(mystring)
else:
print(answer)
addition("12,3,6,9,7,56,7,96,4")

You might also like