Your task is to implement a class that maintains a list of numbers and has a method for efficiently incrementing all elements of the list by a given amount.
In a file changelist.py
, implement the class ChangeList
with the following methods:
append(number)
: addnumber
to the end of the listget(index)
: access a number on the listchange_all(amount)
: increment all list numbers byamount
In the method change_all
, the parameter amount
can be negative too, in which case all numbers are decremented. All methods should work in time.
class ChangeList: def __init__(self): # TODO def append(self, number): # TODO def get(self, index): # TODO def change_all(self, amount): # TODO if __name__ == "__main__": numbers = ChangeList() numbers.append(1) numbers.append(2) numbers.append(3) print(numbers.get(0)) # 1 print(numbers.get(1)) # 2 print(numbers.get(2)) # 3 numbers.change_all(2) print(numbers.get(0)) # 3 print(numbers.get(1)) # 4 print(numbers.get(2)) # 5 numbers.append(8) numbers.change_all(-1) print(numbers.get(0)) # 2 print(numbers.get(1)) # 3 print(numbers.get(2)) # 4 print(numbers.get(3)) # 7
Here the numbers , and are first added to the list, after which the list contents are . Then all numbers are incremented by , after which the list contents are . Finally, the number is added to the list and all numbers are changed by , after which the list contents are .
You can test the efficiency of your solution with the following code. In this case too the code should finish almost immediately.
numbers = ChangeList() total = 0 for i in range(10**5): numbers.append(i + 1) numbers.change_all(i % 11 - 5) total += numbers.get(i // 2) print(total) # 2500049970