Homework Solution

Conditionals

Problems to be Submitted (20 points)

  1. (10 points)

    Lists support a method to count the number of times that a specified value appears in a list. Show that you can compute such a count without relying upon that method. Specifically, assume that you have a list named collection and a target value. You are to write a code fragment that computes the number of times that value appears in the given collection.

    count = 0
    for item in collection:
        if item == value:
            count += 1
    

  2. (10 points)

    Assuming that words is a list of strings. Write a code fragment that computes a new list named palindromes that contains all words having length five or greater that are spelled the same backward and forward (e.g., 'kayak').

    palindromes = []
    for w in words:
        if w == w[::-1]:
            paindromes.append(w)
    


Last modified: Wednesday, 26 September 2018