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

List Comprehension


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

The goal of these exercises is to practice using Python's shorthand syntaxes, so please try to solve each of these without using traditional control structures.

Word List

As a data set for the first set of challenges, we revisit the use of the words list from a previous activity.

  1. Give code that prints all strings that have 6 or more occurrences of the character 'i'.

    Spoiler: my code

  2. Give code that prints all palindromes having length 7 or more.

    Spoiler: my code

  3. Give code that prints the total number of lowercase 'z' characters that occur in the combined list of words. (I find a total of 17428.)

    Spoiler: my code

  4. Give code that prints the total number of list entries that include nonalphabetic characters. (I find a total of 48101.)

    Spoiler: my code

  5. We noted that some words (e.g., Missouri) are capitalized in the data set because they are proper nouns or adjectivies. There are some words that appear both in proper and imporper form (e.g. Apple and apple). Because of the way our list is sorted, it is guaranteed that if both of those exist, they will be neighbors with the proper one followed by the improper.

    Use list comprehension to produce a list of all such proper/improper pairs that start with the letter 'Z'. Note well that you will need to use an index-based approach in order to evaluate a pair words[k] and words[k+1].

    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: use the empty string to perform a join over a generated sequence of strings produced using comprehension syntax.
    Spoiler: my code

  2. Ask the user for a word and then output that word with all vowels (aeiou) removed.

    Enter a word: university
    nvrsty
    Hint: use the empty string to perform a join over a generated sequence of the non-vowels.
    Spoiler: my code

  3. Ask the user for a word and then output that word with all vowels (aeiou) replaced by an underscore character.

    Enter a word: university
    _n_v_rs_ty
    Hint: rely on a conditional expression for the replacement.
    Spoiler: my code

  4. When introducing for loops, we gave the following code fragment to convert from DNA to RNA:

     1    dnaCodes = 'ACGT'
     2    rnaCodes = 'UGCA'
     3    
     4    dna = input('Enter a DNA sequence: ')
     5    rnaList = []
     6    for base in dna:
     7        whichPair = dnaCodes.index(base)          # index into dnaCodes
     8        rnaLetter = rnaCodes[whichPair]           # corresponding index into rnaCodes
     9        rnaList.append(rnaLetter)
    10    rna = ''.join(rnaList)                        # join on empty string
    11    print('Transcribed into RNA:', rna)

    Lines 5 through 10 of that program can be replaced with a single command if using list comprehension. Determine such a line.
    Spoiler: my code

Additional list processing

  1. Assume that data is a generic list of values. Create a new list named uniq that contains only the first occurrence of any given value from the original list. For example, if data = ['e', 'a', 'b', 'e', 'd', 'e', 'b', 'a', 'c'] then the resulting list should be data = ['e', 'a', 'b', 'd', 'c']

    Hint: You'll likely need an index-based approach
    Spoiler: my code

Math

  1. Write a program (using list comprehension rather than a traditional for loop) 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: version 1, version 2

  2. Use list comprehension to produce a list of powers of two, such as [1, 2, 4, 8, 16, 32, 64, ...].

    Spoiler: my code

  3. 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. Use math.factorial for computing the factorial, and use the sum function and comprehension syntax to avoid using a traditional loop for the summation.

    Enter n: 10
    2.7182815255731922

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

  4. The precise value of the mathematical constant $\pi$ is equal to the following infinite series: $$\pi = 4 \cdot \left( \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots \right).$$ While we cannot actually wait for an infinite loop to finish, we can get an approximation by computing the first n terms for some n.

    Write a program that lets the user pick the number of terms, $n$, to compute, and output the approximation that results.

    Enter n: 10
    2.7182815255731922

    Hint: The alternating nature of the sequence can either be done with a conditional expression or by computing the term $-1^k$ for appropriate $k$.
    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