1. Write a function partition() that takes a list of strings and a character as
ID: 639474 • Letter: 1
Question
1. Write a function partition() that takes a list of strings and a character as parameters. It prints the strings in the list that begin with a letter at or after the character (according to dictionary order), one per line. The capitalization of the character and the first letter of the string should not make a difference in whether the string is printed. Empty strings should be skipped. You may assume that the character given as the second parameter is an upper- or lowercase letter. The following shows several sample runs of the function:Explanation / Answer
definition of function partition():
def partition(array=[], x=''):
for i in range(len(array)):
if ' ' in array[i]:
if array[i][0].lower()==x.lower():
print(array[i])
else:
if x.lower() not in array[i].lower():
print(array[i])
partition(['djengo', 'AMBER','Cookie','Sarah','erin'],'b')
Sample output:
sh-4.2# python main.py
djengo
Cookie
Sarah
erin
---------------------------------------------------------------------------------
partition(['djengo', 'AMBER','Cookie','Sarah','erin'],'b')
Sample output:
sh-4.2# python main.py
djengo
Cookie
Sarah
erin
------------------------------------------------------------------------------------------
def partition(array=[], x=''):
for i in range(len(array)):
if ' ' in array[i]:
if array[i][0].lower()==x.lower():
print(array[i])
else:
if x.lower() not in array[i].lower():
print(array[i])
partition(['Bob', 'Mary','Zee',' '],'W')
Sample output:
sh-4.2# python main.py
Bob
Mary
Zee
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.