4.8. ExercisesΒΆ

  1. In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order. Save your answer in a variable named words and make sure that this value doesn’t get overwritten later in your program.

    prefixes = "JKLMNOPQ"
    suffix = "ack"
    
    words = [first + suffix for first in prefixes]
    

    Of course, that’s not quite right because Ouack and Quack are misspelled. Can you fix it?

  1. Write a function that will return the number of digits in an integer.

  1. Write a function named reverse_str that reverses its string argument.

  1. Write a function that takes a string and mirrors its argument. For example the mirror of "good" is "gooddoog".

  1. Write a function named remove_char that removes all occurrences of a given letter from a string. Do not use the replace string method.

  1. Write a function that recognizes palindromes. (Hint: use your reverse function to make this easy!). Hint: Use all, reversed and zip.

  1. Write a function that removes all occurrences of a string from another string. Do not use the remove string method in your solution.

  1. Create a list containing 100 random integers between 0 and 1000 (use iteration, a comprehension, and the random module). Write a function called average that will take the list as a parameter and return the average.

  1. Write a Python function that takes n and m as input and returns the maximum value of a the list of n random integers between 0 and m. (Note:. there is a builtin function named max.)

  1. Write a function called mean_normal(n, m, sd) that computes the mean of n randomly selected values taken from a normal distribution with mean m and standard deviation sd.

  1. Write a function sum_of_squares(xs) that computes the sum of the squares of the numbers in the list xs. For example, sum_of_squares([2, 3, 4]) should return 4+9+16 which is 29:

  1. Write a function to count how many odd numbers are in a list.

  1. Write a function called sum_even that sums up all the even numbers in a list.

  1. Sum up all the negative numbers in a list.

  1. Write a function called num_greater_5 that counts how many words in a list have length 5.

  1. Use the function from the last problem to count how many words in Emma have length of at least 5. Save your result in a variable named num_words_emma.

    Note: You should clean up such that

    1. Hyphenated words count as separate words.
    2. The words contracted with an apostrophe count as two words.
  1. Create a function that takes a value n as input and constructs a multiplication table for whole numbers up to \(n\).
  1. Print out a neatly formatted multiplication table, up to 12 x 12. You should do this by constructing a string. For full credit, each column should be right-justified and your solution should include only comprehensions and lambda functions. Hint: Write a lambda function pads a number with the right number of spaces.

  1. Use list comprehensions to filter the hours table to include only managers. In SQL this would be performed using SELECT and WHERE. Hint: Start by creating a list of the names of all managers. .. actex:: select-where

    hours = [[“Alice”, 43],
    [“Bob”, 37], [“Fred”, 15]]
    titles = [[“Alice”, “Manager”],
    [“Betty”, “Consultant”], [“Bob”, “Assistant”]]
  1. Use list comprehensions to decide if the following tables contain a manager that worked at least 40 hours.

  1. Use list comprehensions to perform a right outer join on the following lists.

  1. Use a list comprehension and lambda expression to create a sequence of functions that combine to average two matrices. A complete solution will provide functions for each level of abstraction.
Next Section - 5. Reading Data from Files