69.1k views
0 votes
Write the function listBuilder(l1, length) that takes a list of strings called l1 and an int called length and returns another list that contains all the strings in l1 that have a length of at least length.Example:Input Outputl1 = ["Hello", "I", "am", "a", "list"] length = 4 ["Hello", list"]l1 = ["Homework", "is", "fun"] length = 8 ["Homework"]l1 = ["CS103", "is", "the", "best"} length = 8 []

1 Answer

6 votes

Answer:

The solution code is written in Python 3:

  1. def listBuilder(l1, length):
  2. result = []
  3. for x in l1:
  4. if(len(x) >= length):
  5. result.append(x)
  6. return result

Step-by-step explanation:

Firstly, we create a variable result to hold the list will be returned at the end of the function. (Line 1)

Next, we create a for-loop to traverse the string in the l1 list (Line 4) and check for each string if their length is equal or bigger than the input length (Line 5). If so, we use append method to add the string to the result list.

At the end, return the result list as output (Line 8).

User Scott Boston
by
3.9k points