December 2017
Beginner
412 pages
10h 8m
English
Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/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:
| | from typing import List |
| | |
| | def double_preceding(values: List[float]) -> None: |
| | """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] ... |
Read now
Unlock full access