Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote