28.6k views
3 votes
The function below takes one parameter: a list of strings (string_list). Complete the function to return a single string from string_list. In particular, return the string that contains the most copies of the word the as upper case or lower case. Be sure, however, to return the string with its original capitalization. You may want to use the count() method of sequences to implement this function.

User Conquester
by
3.2k points

1 Answer

2 votes

Answer:

def string_with_most_the(string_list):

index = 0

count = []

max_length = 0

for i in range(len(string_list)):

s1 = string_list[i].lower()

count.append(s1.count("the"))

if count[i] > max_length:

max_length = count[i]

index = i

return string_list[index]

Step-by-step explanation:

Create a function called string_with_most_the that takes one parameter, string_list

Inside the function:

Initialize the variables

Initialize a for loop that iterates through string_list

Set the lower case version of the strings in string list to s1

Count how many "the" each string in string_list contain and put the counts in the count list

If any string has more "the" than the previous one, set its count as new maximum, and update its index

When the loop is done, return the string that has maximum number of "the", string_list[index]

User Surfealokesea
by
3.4k points