Section #4: Lists & Animations

May 3rd, 2020

Written by Brahm Capoor, Andrew Tierno and Juliette Woodrow

Lists Review


Implement the following function:

def collapse(lst):
    Accepts a list of integers as a parameter and returns a new 
    list containing the result of replacing each pair of integers 
    with the sum of that pair.

    If the list stores an odd number of elements, the final element 
    is not collapsed

    >>> nums = [7, 2, 8, 9, 4, 13, 7, 1, 9, 10]
    >>> collapse(nums)
    [9, 17, 17, 8, 19]
    >>> nums = [1, 2, 3, 4, 5]
    >>> collapse(nums)
    [3, 7, 5]

Distinct Elements

Implement the following function:

def distinct_elements(lst):
    Returns a list of all the disinct elements in lst, in the order that 
    they first occur in lst. 
    >>> distinct_elements([1, 2, 3, 4])
    [1, 2, 3, 4]
    >>> distinct_elements([1, 1, 2, 2, 3])
    [1, 2, 3]
    >>> distinct_elements(['hello', 'hello', 'hello', 'hello', 'hello'])
    >>> distinct_elements([])


Implement the following function:

def rotate_list_right(lst, n):
    returns a 'rotate' version of the list that rotates numbers 
    to the right n times. Each element in numbers is shifted
    forward n places, and the last n elements are moved to 
    the start of the list.

    Your function should not change the list that is passed as a 

    >>> rotate_list_right([1, 2, 3, 4, 5], 2)
    [4, 5, 1, 2, 3]

Building a News Ticker

News channels often operate what's called a News Ticker: a banner on the bottom of the screen across which breaking news scrolls whilst the anchor is speaking.

The program will first read in a sequence of 'headlines', or messages from the user and will then make a new window and scroll each of the messages across the bottom of the window in turn.

This is not a long program, but it is dense: it puts together a lot of the concepts you've been learning about in the last few lectures, so we encourage you to approach it using the milestones we've suggested below. All the code for this problem should be written in

Milestone 1: Animating a single message

To get started, write a program that animates a single label across the screen.

The scroll doesn't need to repeat in this milestone. The text just needs to scroll by once.

Here's a few tools that you might find helpful as you write this:

Milestone 2: Repeating the message

Having a message scroll across the window once is progress, but we're not quite there yet. Currently, once our label has scrolled past the left edge of the screen, it just keeps going, leaving us starting at an unspiring blank window.

In this milestone, edit your animation loop to reset your label once it moves past the left edge of the screen. We've provided you with the get_right_x helper function, which takes in the canvas and label ID as parameters and returns the rightmost x-coordinate of the label, which you might find handy.

By the end of this milestone, you should have a program that scrolls the message of your choosing across the window repeatedly.

Milestone 3: Getting Messages from the User

Now, we'll start working on getting messages from the user, as opposed to hardcoding them into the program. To get started, implement the following function:

def get_messages():
    Reads a series of messages from the user, ending when they
    input a blank message. Returns a list of all the messages 
    the user typed.

Here's the run of this function that produced the messages in the sample run above (user input is italicized):

Type a message here or press enter to finish: CS 106A Students are crushing it
Type a message here or press enter to finish: So long and thanks for all the fish
Type a message here or press enter to finish: Avengers Assemble
Type a message here or press enter to finish: User presses enter immediately

Although you can't see it in the sample output, the function returned the list ["CS 106A Students are crushing it", "So long and thanks for all the fish", "Avengers Assemble"] to the function that called it once the user entered an empty message.

Milestone 4: Making messages appear one after the other

All the pieces are in place now: we have a list of messages that we want to display, and we've developed infrastructure for a label with fixed text to scroll across the screen. Now, we just need to put these jigsaw pieces together to have the messages scroll across the screen in turn.

There are a few steps we need to take to achieve this: