Python Homework #12
Instructions:
Remember:
- You can modify elements of a list like so: L[0] = 5
- Modifying a list will also modify any aliases of it. For example:
L = [7, 8]
Q = L
Q[0] = 100
print(Q) # [100, 8]
print(L) # [100, 8], even though L was not modified directly
You'll be writing two functions. Both of these functions will take a list of
integers, and add 1 to every element, e.g. [10, 20, 30] becomes [11, 21, 31].
-
Write a function add1Copy(L). L is a list of integers.
This function should return a copy of L, with each element increased by 1.
This function should not modify L.
-
Write a function add1Modify(L). L is a list of integers.
This function should modify each element of L
(using L[i] =) to increase each by 1.
This function should return L.
Use the following test cases to test your two functions:
A = [10,20,30]
B = add1Copy(A) # B should be [11,21,31]
C = [20,30,40]
D = add1Modify(C) # D should be [21,31,41]
print("Copied A, B: ", A, B)
print("Modified C, D: ", C, D)
-
Make sure that A is not incremented, since add1Copy() shouldn't modify it.
-
Make sure that C is incremented, since C and D should be the same list (doesn't create a copy)