|
| 1 | + |
| 2 | +# Practice Problems: Lists |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below. |
| 7 | + |
| 8 | +```python |
| 9 | +def add(a, b): |
| 10 | + return a + b |
| 11 | +#print(add(5, 2)) |
| 12 | +#print(add(8, 1)) |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Problem 1 |
| 18 | + |
| 19 | +Write a function using random.randint to generate an index, use that index to pick a random element of a list and return it. |
| 20 | + |
| 21 | +``` |
| 22 | +def random_element(a): |
| 23 | + ... |
| 24 | +
|
| 25 | +fruits = ['apples', 'bananas', 'pears'] |
| 26 | +random_element(fruits) could return 'apples', 'bananas' or 'pears' |
| 27 | +``` |
| 28 | + |
| 29 | +## Problem 2 |
| 30 | + |
| 31 | +Write a REPL which asks users for a list of numbers, which they enter, until they say 'done'. Then print out the list. |
| 32 | + |
| 33 | +``` |
| 34 | +Enter a number (or 'done'): 5 |
| 35 | +Enter a number (or 'done'): 34 |
| 36 | +Enter a number (or 'done'): 515 |
| 37 | +Enter a number (or 'done'): done |
| 38 | +[5, 34, 515] |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Problem 3 |
| 43 | + |
| 44 | +Write a function that takes a list of numbers, and returns True if there is an even number of even numbers. |
| 45 | + |
| 46 | +```python |
| 47 | +eveneven([5, 6, 2]) → True |
| 48 | +eveneven([5, 5, 2]) → False |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +## Problem 4 |
| 53 | + |
| 54 | +Print out every other element of a list, first using a while loop, then using a for loop. |
| 55 | + |
| 56 | +``` |
| 57 | +>>> nums = [0, 1, 2, 3, 4, 5, 6, 7, 8] |
| 58 | +>>> print_every_other(nums) |
| 59 | +0, 2, 4, 6, 8 |
| 60 | +``` |
| 61 | + |
| 62 | +## Problem 5 |
| 63 | +Write a function that returns the reverse of a list. |
| 64 | + |
| 65 | +`def reverse(nums):` |
| 66 | + |
| 67 | +## Problem 6 |
| 68 | +Write a function to move all the elements of a list with value less than 10 to a new list and return it. |
| 69 | + |
| 70 | +`def extract_less_than_ten(nums):` |
| 71 | + |
| 72 | +## Problem 7 |
| 73 | +Write a function to find all common elements between two lists. |
| 74 | + |
| 75 | +`def common_elements(nums1, nums2):` |
| 76 | + |
| 77 | + |
| 78 | +## Problem 8 |
| 79 | +Write a function to combine two lists of equal length into one, alternating elements. |
| 80 | + |
| 81 | +```python |
| 82 | +def combine(nums1, nums2): |
| 83 | + ... |
| 84 | +combine(['a','b','c'],[1,2,3]) → ['a', 1, 'b', 2, 'c', 3] |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +## Problem 9 |
| 89 | + |
| 90 | +Given a list of numbers, and a target number, find a pair of numbers from the list that sum to a target number |
| 91 | + |
| 92 | +```python |
| 93 | +nums = [5, 6, 2, 3] |
| 94 | +target = 7 |
| 95 | +find_pair(nums, target) → [5, 2] |
| 96 | +``` |
| 97 | + |
| 98 | +Optional: return a list of all pairs of numbers that sum to a target value. |
| 99 | + |
| 100 | + |
| 101 | +## Problem 10 |
| 102 | + |
| 103 | +Write a function that merges two lists into a single list, where each element of the output list is a list containing two elements, one from each of the input lists. |
| 104 | + |
| 105 | +```python |
| 106 | +merge([5,2,1], [6,8,2]) → [[5,6],[2,8],[1,2]] |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +## Problem 11 |
| 111 | + |
| 112 | +Write a function `combine_all` that takes a list of lists, and returns a list containing each element from each of the lists. |
| 113 | + |
| 114 | +```python |
| 115 | +nums = [[5,2,3],[4,5,1],[7,6,3]] |
| 116 | +combine_all(nums) → [5,2,3,4,5,1,7,6,3] |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +## Problem 12 |
| 121 | + |
| 122 | +Write a function that takes `n` as a parameter, and returns a list containing the first `n` [Fibonacci Numbers](https://en.wikipedia.org/wiki/Fibonacci_number). |
| 123 | + |
| 124 | +```python |
| 125 | +fibonacci(8) → [1, 1, 2, 3, 5, 8, 13, 21] |
| 126 | +``` |
| 127 | + |
| 128 | +## Problem 13 |
| 129 | + |
| 130 | +Write functions to find the minimum, maximum, mean, and (optionally) mode of a list of numbers. |
| 131 | + |
| 132 | +```python |
| 133 | +def minimum(nums): |
| 134 | + ... |
| 135 | + |
| 136 | +def maxmimum(nums): |
| 137 | + ... |
| 138 | + |
| 139 | +def mean(nums): |
| 140 | + ... |
| 141 | + |
| 142 | +def mode(nums): # (OPTIONAL) |
| 143 | + ... |
| 144 | +``` |
| 145 | + |
| 146 | + |
| 147 | +## Problem 14 |
| 148 | + |
| 149 | +Write a function which takes a list as a parameter and returns a new list with any duplicates removed. |
| 150 | + |
| 151 | +```python |
| 152 | +def find_unique(nums): |
| 153 | + ... |
| 154 | +nums = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155] |
| 155 | +unique_nums = find_unique(nums) → [12, 24, 35, 88, 120, 155] |
| 156 | +``` |
0 commit comments