September 2013
Intermediate to advanced
350 pages
9h 38m
English
Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy2/practical-programming.
Your lab partner claims to have written a function that replaces each value in a list with twice the preceding value (and the first value with 0). For example, if the list [1, 2, 3] is passed as an argument, the function is supposed to turn it into [0, 2, 4]. Here’s the code:
| | def double_preceding(values): |
| | """ (list of number) -> NoneType |
| | |
| | Replace each item in the list with twice the value of the |
| | preceding item, and replace the first item with 0. |
| | |
| | >>> L = [1, 2, 3] |
| | >>> double_preceding(L) |
| | >>> L |
| | [0, 2, 4] |
| | """ |
| | |
| | |
| | if values != []: |
| | temp = values[0] |
| | values[0] ... |
Read now
Unlock full access