Add final test material
This commit is contained in:
19
tests/Final/Q2 Python/invert.py
Normal file
19
tests/Final/Q2 Python/invert.py
Normal file
@ -0,0 +1,19 @@
|
||||
def invert(lst):
|
||||
lst = list(lst) # Makes sure arugment is a list
|
||||
|
||||
#list_range = range(0, len(lst)) # Code I might have used if I used a dictionary comprehension
|
||||
|
||||
|
||||
ret_dict = dict() # Create empty dict
|
||||
counter = 0 # Create counter
|
||||
|
||||
for i in lst:
|
||||
ret_dict[i] = counter # Assign each element of list to
|
||||
# its postion in list
|
||||
|
||||
counter = counter + 1 # Increment counter
|
||||
|
||||
if (len(lst) > len(ret_dict)): # Check if the length of new dict is less than
|
||||
return None # input list, if so, there is duplicates so return none
|
||||
|
||||
return ret_dict # Return created dictionary
|
13
tests/Final/Q2 Python/test_invert.py
Normal file
13
tests/Final/Q2 Python/test_invert.py
Normal file
@ -0,0 +1,13 @@
|
||||
from invert import invert
|
||||
def test_empty():
|
||||
assert invert([]) == {}
|
||||
def test_simple():
|
||||
invert(["three","two","one"]) == {"three": 0, "two":1, "one":2}
|
||||
def test_duplicate():
|
||||
assert invert(["bob","bob"]) == None
|
||||
def test_numeric():
|
||||
assert invert(range(0,6)) == { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5 }
|
||||
def test_invert():
|
||||
L=[-8,"pineapple",3]
|
||||
D=invert(L)
|
||||
assert [ L[D[j]] for j in L ] == L
|
Reference in New Issue
Block a user