# 6. RECURSIVE METHOD. Write a function called first_names. It takes a list of s
ID: 3832964 • Letter: #
Question
# 6. RECURSIVE METHOD. Write a function called first_names. It takes a list of strings.
# Each string is someone's full name. The function returns
# a list of the people's first names.
#
# >>> first_names(['Ljubomir Perkovic', 'Steve Lytinen', 'Amber Settle'])
# ['Ljubomir', 'Steve', 'Amber']
#
def first_names(names):
pass
# 7. RECURSIVE METHOD. Write a function called squares, which passed an integer n
# and returns the squares of each of the first n positive integers
#
# >>> squares(5)
# [1, 4, 9, 16, 25]
#
def squares(n):
pass
# 8. RECURSIVE METHOD. Write a function called only_caps, which is passed a word
# (i.e., a Python str) and returns a string with only the
# capital letters in the original word. For example:
#
# >>> only_caps('DePaul')
# 'DP'
# >>> only_caps('UIC')
# 'UIC'
# >>> only_caps('programming')
# ''
# >>> only_caps('CSC242')
# 'CSC'
# >>> only_caps('Ph.D.')
# 'PHD'
def only_caps(word):
pass
Explanation / Answer
PROGRAM CODE:
def first_names(names, list=[]):
if len(names) == 0:
return list
name = names[0]
list.append(name.split(" ")[0])
names.remove(name)
return first_names(names, list)
print(first_names(['Ljubomir Perkovic', 'Steve Lytinen', 'Amber Settle']))
OUTPUT:
PROGRAM CODE:
def squares(n, i=1, list=[]):
if i>n:
return list
list.append(i*i)
return squares(n , (i+1), list)
print(squares(5))
OUTPUT:
[1, 4, 9, 16, 25]
PROGRAM CODE:
def only_caps(word, newWord=''):
if len(word) == 0:
return newWord;
if(word[0].isupper()):
newWord = newWord + word[0];
return only_caps(word[1:], newWord);
print(only_caps('DePaul'))
OUTPUT:
DP
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.