Книга: Practical Programming, Fourth Edition
Назад: Let’s Reiterate
Дальше: Chapter 17: Handling Exceptions

Exercises

Here are some exercises for you to try on your own.

  1. Given a list of string tokens data, produce a single output string that reverses the order of tokens in the list, and reverses the characters within each token. You may use any iteration tools you like. Example:

     data = [​'#Mary'​, ​'had'​, ​'a'​, ​'little'​, ​'#lamb'​]
     # You code should produce 'bmal# elttil a dah yraM#'
  2. Solve the previous problem, but if a token begins with #, keep the # at the start of that token in the final result. In other words, reverse each token’s characters except a leading #, which should remain in front. Example result:

     data = [​'#Mary'​, ​'had'​, ​'a'​, ​'little'​, ​'#lamb'​]
     # You code should produce '#bmal elttil a dah #yraM'
  3. Write a single expression that uses the dir function (see ), the filter function, and a custom lambda to count the number of functions defined in the module itertools whose names do not start with an underscore _.

  4. You are given a list heights that contains numbers and positive infinities. The float('inf') entries indicate measurements that were too large to be recorded. Example:

     heights = [1, 2.2, 7, float(​'inf'​), 1.1, 2, float(​'inf'​)]

    Write a code fragment that returns a new list in which every float('inf') is replaced with the largest finite value found in the original list. For the example above, the result should be:

     [1, 2.2, 7, 7, 1.1, 2, 7]

    Use the function math.isfinite to test whether a value is finite. Solve the problem two ways: using a list comprehension, and using the map/filter tools. Assume the list contains at least one finite value.

  5. Implement the generator function my_enumerate that mimics enumerate without calling enumerate. The function should yield (index, item) pairs, with the index starting at 0 and increasing by 1 for each element. Use as a guide. Assume data is a sequence that supports len. Test the function.

  6. Write a function most_frequent_three that returns the three most frequent characters in text, treating letters case-insensitively. Ignore whitespace characters (spaces, tabs, newlines); to detect them, use str.isspace. Count all other characters (letters, digits, punctuation). Assume that text contains at least three distinct non-whitespace characters. Test your function on a substantial text sample (for example, a Shakespeare play).

  7. Write a function swap_two that uses multiple assignment to swap the first two elements of the list l. Do not use a temporary variable. Assume the list has at least two elements.

  8. An RGB (“red--green--blue”) color is written as a hash sign (#) followed by six characters. Think of them as three two-character codes placed back-to-back: the first pair controls the amount of red, the second pair controls the amount of green, and the third pair controls the amount of blue. Each pair encodes an integer intensity from 0 (none of that color) to 255 (maximum intensity). In Python, you can convert each pair to an integer with int(pair, 16). For example, the color #DF2073 (pink) has int('DF', 16) of red (=223), int('20', 16) of green (=32), and int('73', 16) of blue (=115).

    Assume the brightness of a color is a weighted sum of the three component intensities: (3 * R + 10 * G + B) / 14. Write a function brightness that returns the brightness of a well-formed color string #RRGGBB. Use this function as a key to sort a list of colors from darkest to brightest, as shown in the following example:

     >>>​​ ​​colors​​ ​​=​​ ​​[​​'#4526FA'​​,​​ ​​'#0000FF'​​,​​ ​​'#E34530'​​]
     >>>​​ ​​sorted(colors,​​ ​​key=brightness)
     ['​#0000FF', '#4526FA', '#E34530']
  9. Solve the previous problem without defining a named function by using a lambda.

  10. Implement a generator function signals that yields a sequence of n traffic-light signals cycling in this order: “red”, “green”, and “amber”, then repeats as needed. For example, signals(5) should yield “red”, “green”, “amber”, “red”, “green.” Use a for loop to demonstrate the correctness of the function.

Footnotes

Назад: Let’s Reiterate
Дальше: Chapter 17: Handling Exceptions