BYU logo Computer Science

To start this assignment, download this zip file.

Lab 3d — List Patterns

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab3d.

Warmup

5 minutes

Consider the following list:

fruits = ['apple', 'pear', 'banana', 'plum', 'pineapple']

Without writing code, give an example of a function that would take this list as input and return a new value that uses the following patterns:

  • map pattern
  • filter pattern
  • select pattern
  • accumulate pattern

You should be able to describe four functions, one per pattern.

Doubling

10 minutes

Write a program that takes a list of words and “doubles” them by repeating them with a space in between. Print the list of doubled words with ”- ” in front of them. You have starter code in doubling.py:

def double_words(words: list[str]) -> list[str]:
    # Write code here
	pass

def print_words(words: list[str]):
    # Write code here
    pass


def main():
    words = ['really', 'very', 'so', 'crazy']
    doubled = double_words(words)
    print_words(doubled)


if __name__ == '__main__':
    main()

The list of words is coded into the program, so the output should be:

- really really
- very very
- so so
- crazy crazy

Discussion with TA:

  • What pattern is this?
  • How did you double the words?

Long words

10 minutes

Write a program that takes a list of words and prints out a list of the long words. Long words are more than 5 characters long. Print the list of long words with ”- ” in front of them. You have starter code in long_words.py:

def long_words(words: list[str]) -> list[str]:
    # Write code here
    # Delete the return below
    return []


def print_words(words: list[str]):
    # Write code here
    pass


def main():
    words = ['completely', 'fun', 'program', 'to', 'write', 'hahaha']
    long = long_words(words)
    print_words(long)


if __name__ == '__main__':
    main()

The list of words is coded into the program, so the output should be:

- completely
- program
- hahaha

Discussion with TA:

  • What pattern is this?
  • How comfortable are you with lists?

Shortest and total

10 minutes

Write a program that takes a list of words and prints out the shortest word and the total length of all the words. You have starter code in shortest_and_total.py:

def shortest_word(words: list[str]) -> str:
    # Write code here
    pass


def total_lengths(words: list[str]) -> int:
    # Write code here
	pass


def main():
    words = ['the', 'elephant', 'ate', 'twenty', 'bananas', 'and', 'an', 'orange']
    shortest = shortest_word(words)
    total = total_lengths(words)
    print(f'The shortest word is: {shortest}')
    print(f'The total length of all the words is: {total}')


if __name__ == '__main__':
    main()

The list of words is coded into the program, so the output should be:

The shortest word is: an
The total length of all the words is: 38

Discussion with TA:

  • What patterns does this program use?
  • How comfortable are you with all four patterns?

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)