1
Midterm Handout B
for
e02
CS8 S18

Test cases for indexLargestEvenInt

indexLargestEvenInt should return the index in the list of the largest even integer. If there are no even integers in the list, it should return None.

1
2
3
4
5
6
7
8
9
10
11
12
def test_1():      # 88 is at index 2
    assert indexLargestEvenInt([22,55,88,33,66])==2

def test_2():      # there are no even ints
    assert indexLargestEvenInt([33,11,77])==None 

def test_3():      # 66 is at index 3
    assert indexLargestEvenInt([33,34,11,66,99])==3 

def test_4():      # 44 is at index 0
    assert indexLargestEvenInt([44,11,22])==0     
    

Code for indexLargestEvenInt_a

1
2
3
4
5
6
7
8
9
10
def indexLargestEvenInt_a(ilist):
    "index of largest even integer; None if no even ints"
    if type(ilist)!=list:
        raise ValueError("not a list")
    answer=None
    for x in ilist:
      if x%2 == 0:
        if answer==None or x > answer:
          answer = x
    return answer	  

Code for indexLargestEvenInt_b

1
2
3
4
5
6
7
8
9
def indexLargestEvenInt_b(ilist):
    if type(ilist)!=list:
        raise ValueError("not a list")
    answer=None
    for i in range(len(ilist)):
      if ilist[i]%2 == 0:
        if answer==None or ilist[i] > ilist[answer]:
          answer = i
    return answer

Midterm Handout B for CS8, Final Exam, S18, Page 2

Code for indexLargestEvenInt_c

1
2
3
4
5
6
7
8
9
10
def indexLargestEvenInt_c(ilist):

    if type(ilist)!=list:
        raise ValueError("not a list")
    answer=None
    for i in range(len(ilist)):
      if ilist[i]%2 == 0:
        if answer==None or ilist[i] > ilist[answer]:
          answer = i
      return answer

Code for indexLargestEvenInt_d

1
2
3
4
5
6
7
8
9
def indexLargestEvenInt_d(ilist):

    if type(ilist)!=list:
        raise ValueError("not a list")
    answer=None
    for x in ilist:
      if (x%2 == 0) and x > answer:
        answer = x
    return answer
End of Handout