# Returns the number of elements in the given list whose values # are between the given min and max, inclusive. def count_between(list, min, max): occurrences = 0 for n in list: if min <= n <= max: occurrences += 1 return occurrences # Returns the index of the last occurrence of the given target # value in the given list, or raises a ValueError if not found. def last_index(list, target): for i in range(len(list) - 1, -1, -1): if list[i] == target: return i raise ValueError(str(target) + " is not in list") # Returns a list of the unique words in the given file, # excluding any duplicate words. def unique_words(filename): with open(filename) as file: words = [] for word in file.read().split(): if word not in words: words.append(word) return words # Replaces the first occurrence of the given target value # with the given replacement value in the given list. def replace(list, target, replacement): if target in list: index = list.index(target) list[index] = replacement # Replaces all occurrences of the given target value with the # given replacement value in the given list. def replace_all(list, target, replacement): for i in range(len(list)): if list[i] == target: list[i] = replacement # Swaps the list elements at indexes i and j. def swap(list, i, j): temp = list[i] list[i] = list[j] list[j] = temp # Reverses the order of the elements in the given list. def reverse(list): for i in range(len(list) // 2): # swap list[i] with list[j] j = len(list) - i - 1 temp = list[i] list[i] = list[j] list[j] = temp # Moves each element left by 1 index in the given list, # wrapping the front element to the back of the list. def rotate_left(data): first = data[0] for i in range(len(data) - 1)): data[i] = data[i + 1] data[-1] = first # Moves each element right by 1 index in the given list, # wrapping the last element to the front of the list. def rotate_right(data): last = data[-1] for i in range(len(data) - 1, 0, -1): data[i] = data[i - 1] data[0] = last def main(): numbers = [8, 7, 19, 2, 82, 8, 7, 25, 8] print(count_between(numbers, 1, 10)) print(last_index(numbers, 7)) replace_all(numbers, 8, -1) print(numbers) reverse(numbers) print(numbers) main()