Introduction to Computer Science
CompSci 101 : Spring 2014

Problem Solving

After you have worked out an algorithm for solving a problem, write a program that implements your algorithm.

Text Roll

Take some text. Put a small ball at the top of the first letter of the first word of the first sentence. The ball is drawn via gravity downwards. The text is also at a slight angle, so the ball wants to also move towards the right. The ball can freely move between the lines, and can drop through spaces or off the end. Considering the first column to be column 0, what column will the ball end up in?

In this example, the ball ends up in column 7.

Given a list of strings representing the lines of text, return an int, indicating the column from which the ball will drop.

Here is the function declaration in Python:

def textRoll (lines):
    """
    Returns int, representing the last column through which a
    ball falls through the given lines of text.
    """

Examples

  1. lines = [ "Lorem ipsum dolor sit amet, consectetur adipisicing elit,",
              "sed do eiusmod tempor incididunt ut labore et dolore magna",
              "aliqua. Ut enim ad minim veniam, quis nostrud exercitation",
              "ullamco laboris nisi ut aliquip ex ea commodo consequat." ]
    
    Returns: 7
    

    This is the example in the image above.