# 
# recursion_examples.py
# 
# Example recursive functions from lecture
#
#

def power(b, p):
    """ returns b raised to the p power
        inputs: b is a number (int or float)
                p is a non-negative integer
    """
    if p == 0:
        return 1
    else:
        pow_rest = power(b, p-1)
        return b * pow_rest

def mylen(s):
    """ returns the length of the sequence s
        input: s is a sequence (a string or list)
    """
    if s == '' or s == []:
        return 0
    else:
        len_rest = mylen(s[1:])
        return 1 + len_rest

def num_vowels(s):
    """ returns the number of vowels in the string s
        input: s is a string of 0 or more lowercase letters
    """

    # note that this code has extra print statements for debugging
    # remove the print statements for a final version
    if s == '':
        print('num_vowels(' + s + '), returning 0')
        return 0
    else:
        num_in_rest = num_vowels(s[1:])
        if s[0] in 'aeiou':
            print('num_vowels(' + s + '), returning', 1 + num_in_rest)
            return 1 + num_in_rest
        else:
            print('num_vowels(' + s + '), returning', 0 + num_in_rest)
            return 0 + num_in_rest


def replace(s, old, new):
    """ returns a version of the string s
        in which all occurrences of old
        are replaced by new
    """
    if old not in s:     # s == '' would also work
        return s
    else:
        # make the recursive call first
        # and store its return value
        rest = replace(s[1:], old, new) 

        # do your one step!
        if s[0] == old:
            return new + rest   # replace s[0]
        else:
            return s[0] + rest  # don't replace it


## test cases here:
if __name__ == '__main__':

    print("num_vowels('testing')", num_vowels('testing'))
    # print("num_vowels('the song remains the same')", 
    #         num_vowels('the song remains the same'))