Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Hands-on Day

For Loops


All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.


Problems

Math

  1. Write a program that allows the user to enter any number of integers on the same line (separated by spaces), and which prints out the sum of those numbers. A sample execution might appear as

    Enter numbers: 3 18 5 2 11
    Their sum is 39
    Spoiler: my code

  2. Write a program that allows the user to enter any number of integers on the same line (separated by spaces), and which prints out the sum of those numbers. A sample execution might appear as

    Enter numbers: 3 18 5 2 11
    Their average is 7.8
    Spoiler: my code

  3. Write a program that calculates the factorial, $k!$ of a given integer $k$. The fatorial is the product of the first $k$ positive integers, defined as $$k! = 1 \cdot 2 \cdot \ \cdots \ \cdot k.$$ For example $4! = 1 \cdot 2 \cdot 3 \cdot 4 = 24$.

    Enter k: 4
    24
    Spoiler: my code

    For full disclosure, there is already a factorial function in Python's math module.

  4. The base, $e$, of the natural log can be computed as an infinite sum $$e = \sum_{k=0}^\infty \frac{1}{k!}.$$ While we cannot actually wait for an infinite loop to finish, we can get a pretty good approximation for $e$ by computing a finite portion of this sum.

    Write a program that lets the user pick the number of terms, $n$, to compute, and output the approximation that results. While this could require need for a nested loop (if you need another loop to do a factorial), we'll let you use math.factorial in your solution.

    Enter n: 10
    2.7182815255731922

    For what it's worth the more precise approximation math.e is 2.718281828459045, and if we use $n=18$ terms with our program, we get the same precision.
    Spoiler: my code

String Processing

  1. Ask the user for a word and then output a new word which has two copies of each character of the original.

    Enter a word: argh
    aarrgghh
    Hint: Make a goal of creating the new string internally, and then print it. (The challenge with trying to print a little bit at a time is that the print function will by default print separating spaces or newlines.)
    Spoiler: my code, my code with intermediate list, my code with actual print function

  2. Ask the user for a word and a number (that is assumed to be less than the word length) and then output the following pattern.

    Enter word: example
    Enter k: 3	
    exa
     xam
      amp
       mpl
        ple

    Hint: each line is produced as j spaces (computed as ' '*j followed by k chosen characters from the word. But it takes some care to figure out what the loop range should be.
    Spoiler: my code

List Processing

  1. Given a list of strings named words, write a loop that produces a new list of strings that are the capitalized versions of the original words. For example, if the original list were ['apple', 'banana', 'fig'] then the new list should be ['Apple', 'Banana', 'Fig']
    Spoiler: my code

  2. As a variant of the previous problem, this time you are to instead write code that mutates the existing list to replace each element with the capitalized version. Note well, it is not that we want you to reassign the identifier words to a new list; you should actually be changing the contents of the original list.
    Spoiler: my code

  3. Given a list of strings, words, compute the cumulative number of characters used by those words. For example, if

    words = ['apple', 'banana', 'fig']
    the total is 14.

    Note: please use a for loop to compute this, even though wise Python programers might realize that this could be done more easily by joining the list of strings into a single string.

    Spoiler: my code

  4. Given a list of strings, words, compute the length of the longest individual word in the list. For example, if

    words = ['apple', 'banana', 'fig']
    the longest word has length 6.

    While this would be a classic example of a problem that might require a conditional ("if") statement, we can avoid need for that structure by recognizing that the max function can be used to effectively compute the running max with a command that effectively determines
    newmax = max(oldmax, current value)

    Spoiler: my code


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Monday, 01 October 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring