Write a recursive function numbers(s) that satisfies the following requirements:
ID: 3765174 • Letter: W
Question
Write a recursive function numbers(s) that satisfies the following requirements:
Takes a string s as a parameter.
Accumulates all of the digits present in the input argument string into a string and then returns the
string of digits.
The digits should be accumulated in the order found in the original string, moving from left to right.
If the string provided as a input argument does not contain any digits or is empty, the function returns empty string.
Must be recursive, no loops.
Sample executions of numbers(s)in interactive shell:
>>> numbers( ' 3 2 1 blastoff!' ) '321'
>>> numbers( '' )
''
in python 3.4
Explanation / Answer
def recv(string,pos):
if(pos>=len(string)):
return ""
if(string[pos]>='0' and string[pos]<='9'):
return string[pos]+recv(string,pos+1)
else:
return recv(string,pos+1)
string = raw_input("Enter a string ")
print recv(string,0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.