1
Final Exam Handout
for
e02
CS8 M17

Example pytest test cases from lab03

import pytest

def test_perimRect_1():
   assert perimRect(4,5)==18

def test_perimRect_2():
   assert perimRect(7,3)==20

def test_perimRect_3():
   assert perimRect(2.1,4.3)==pytest.approx(12.8)
   

Code for indexShortest_a

No hints on this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
def indexShortest_a(slist):
    if type(slist)!=list:
        raise ValueError("not a list")
    if slist==[]:
        raise ValueError("it's empty")

    soFar = 0
    for i in range(0,len(slist)): 
        if type(slist[i])!=str:     
            raise ValueError("found non-string")
        if len(slist[i]) < len(slist[soFar]):
            soFar = i
    return soFar

Code for indexShortest_b

Hint: Pay attention to the indentation of line 13

1
2
3
4
5
6
7
8
9
10
11
12
13
def indexShortest_b(slist):
    if type(slist)!=list:
        raise ValueError("not a list")
    if slist==[]:
        raise ValueError("it's empty")

    soFar = 0
    for i in range(0,len(slist)): 
        if type(slist[i])!=str:   
            raise ValueError("found non-string")
        if len(slist[i]) < len(slist[soFar]):
            soFar = i
        return soFar

Handout for CMPSC 8, Final Exam, M17, Page 2

Code for indexShortest_c

Hint: pay attention to line 11

1
2
3
4
5
6
7
8
9
10
11
12
13
def indexShortest_c(slist):
    if type(slist)!=list:
        raise ValueError("not a list")
    if slist==[]:
        raise ValueError("it's empty")

    soFar = 0
    for i in range(0,len(slist)): 
        if type(slist[i])!=str:     
            raise ValueError("found non-string")
        if len(slist[i]) <= len(slist[soFar]):
            soFar = i
    return soFar

Code for indexShortest_d

Hint: again, pay attention to line 11

1
2
3
4
5
6
7
8
9
10
11
12
13
def indexShortest_d(slist):
    if type(slist)!=list:
        raise ValueError("not a list")
    if slist==[]:
        raise ValueError("it's empty")

    soFar = 0
    for i in range(0,len(slist)):
        if type(slist[i])!=str:  
            raise ValueError("found non-string")
        if slist[i] < slist[soFar]:
            soFar = i
    return soFar
End of Handout