Homework Solution

Conditionals

Problems to be Submitted (20 points)

  1. (6 points)

    Assume that tokens is a list of strings read from a text file, that represent numeric values (e.g., ['23', '35', '8', '200']).

    Give a single line that uses comprehension syntax to produce a new list, named vals, which are the corresponding integer values (e.g., [23, 35, 8, 200]).

    vals = [int(t) for t in tokens]

  2. (6 points)

    Assume that nouns is a list of strings. Given a line that produces a new list, plural, containing all of the original words that end with the character 's'. (Yes, I realize that these might not actually represent plurals, but this is just an exercise.)

    plural = [w for w in nouns if w.endswith('s')]

  3. (8 points)

    On our recent exam there was a question that involved converting a phrase such as 'laughing out loud' to an acronym such as 'LOL'. (Actually, the question was to use a loop to do this converstion for many different phrases, but we will focus on a single phrase for this question.)

    We could accomplish the goal using traditional techniques as in the following:

    initials = []
    for word in phrase.split():
        initials.append(word[0].upper())
    acronym = ''.join(initials)
    

    Your task is to give a single line of code of the form

    acronym = ???
    that accomplishes the above task.

    acronym = ''.join(word[0].upper() for word in phrase.split())


Last modified: Wednesday, 03 October 2018