Add final test material

This commit is contained in:
Isaac
2022-12-20 21:49:28 -04:00
parent fcef1277f3
commit 4ecaa1a216
8 changed files with 259 additions and 0 deletions

View 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

View 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