# 1. RECURSIVE METHOD. Write a function which prints all the plural # words in a
ID: 3832960 • Letter: #
Question
# 1. RECURSIVE METHOD. Write a function which prints all the plural
# words in a list of words. For our purposes, plural words
# end in 's'. For example,
#
# >>> print_plurals(['computer', 'computers', 'science,', 'sciences'])
# computers
# sciences
def print_plurals(words):
if words == [ ]: # base case
return
elif False: # recursive case -- replace "False"
pass
else: # you may need a second recursive case
pass
# 2. RECURSIVE METHOD. Write a function which returns the first vowel
# in a word. For example:
#
# >>> first_vowel('computer')
# o
# >>> first_vowel('science')
# i
def first_vowel(word):
if word == '': # base case 1
return ''
elif word[0] in 'aeiou': # base case 2 -- replace "False"
return False
else: # recursive case
pass
# 3. RECURSIVE METHOD. Write a function which **returns** all the plural words in a list of words
#
# >>> plurals(['computer', 'computers', 'science', 'sciences'])
# ['computers', 'sciences']
def plurals(words):
if words == []: # base case
pass
elif words[0][-1] == 's': # recursive case 1
pass
else: # recursive case 2
pass
Explanation / Answer
def print_plurals(words):
if words == [ ]: # base case
return
elif words[0][-1] != 's': # recursive case -- replace "False"
print_plurals(words[1:])
else: # you may need a second recursive case
print(words[0])
print_plurals(words[1:])
print_plurals(['computer', 'computers', 'science,', 'sciences'])
def first_vowel(word):
if word == '': # base case 1
return ''
elif word[0] in 'aeiou': # base case 2 -- replace "False"
return word[0]
else: # recursive case
return first_vowel(word[1:])
print(first_vowel('computer'))
print(first_vowel('science'))
def plurals(words):
lis = []
pluralHelp(words, lis)
return lis
def pluralHelp(words, lis):
if words == [ ]: # base case
return
elif words[0][-1] != 's': # recursive case -- replace "False"
pluralHelp(words[1:], lis)
else: # you may need a second recursive case
lis.append(words[0])
pluralHelp(words[1:], lis)
print(plurals(['computer', 'computers', 'science', 'sciences']))
# code link https://paste.ee/p/SlAN0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.