CMU 15-112: Fundamentals of Programming and Computer Science
Quiz3b


Quiz3 Version B


See the quiz3 frontmatter.


You MAY NOT use VS Code or any other editor or resource for this quiz. You may not use LISTS (except implicitly in loops), LIST INDEXING, SETS, DICTIONARIES, or RECURSION either.
(15 min.)

Fill-in-the-Blank: averageGrade(gradebook) [50 points]

Background: this function will use a gradebook, which is a multi-line string where each line contains comma-separated values -- a student's name followed by their quiz scores. If a student did not take a quiz, the score will be '--' and will not count in their average. You may assume that no two students have the same name.

Here is a sample gradebook:
    gradebook = '''\
dan,80,78,--
mae,86,87,88
sue,--,--,--'''

For example, we see that dan scored 80 on quiz1, 78 on quiz2, and did not take quiz3. Also, sue did not take any quizzes.

With this in mind, we wrote the function averageGrade(student, gradebook) that takes a student name and a gradebook, as just described, and returns the rounded average quiz score for that student, or None if either the student is not in the gradebook or the student did not take any quizzes. This function is case-insensitive, so 'Dan' and 'dan' are treated as the same student.

Here are some sample test cases:
    gradebook = '''\
dan,80,78,--
mae,86,87,88
sue,--,--,--'''
    assert(type(averageGrade('dan', gradebook) == int))
    assert(averageGrade('dan', gradebook) == 79)
    assert(averageGrade('mae', gradebook) == 87)
    assert(averageGrade('sue', gradebook) == None)
    assert(averageGrade('max', gradebook) == None)
    assert(averageGrade('DAN', gradebook) == 79)

Your task: fill in the blanks with the missing code from our solution. The parts of the solution that we provided contain enough information for you to determine the missing parts.

def averageGrade(student, gradebook):
    student = student.lower()
    gradebook = gradebook.lower()
    for line in ___________blank1_________: 
        name = None
        score = count = 0
        for entry in ______blank2_________:  
            if (name == None):
                name = entry
            elif __________blank3_________: 
                score += int(entry)
                count += 1
        if ________________blank4_________: 
            if (count == 0):
                return None
            else:
                return ____blank5_________:
    return None



CT1 (20 points):
Indicate what this code prints.

def ct1(s):
    t = s.lower()
    s += 'z'
    t *= len(t+s)
    return f'{t+s}t+s'
print(ct1('B'))





CT2 (20 points):
Indicate what this code prints.

def ct2(r, s):
    t = ''
    for i in range(0, len(r), 2):
        t += str(s.find(r[i]))

    result = f'{t}=={eval(t)}'
    return result
print(ct2('doorknob', 'avocado'))





RC1 (10 pts):
Find an argument for rc1 that makes it return TRUE.
def rc1(s):
    n, c = ord(s[0]), 0
    while s != '':
        assert(ord(s[0]) == n)
        n += 2
        c += 1
        s = s[1:]
    return (chr(n) == 'k') and (c == 4)




BonusCT1 [+3 points]

This is BONUS. You will not lose points for not answering or incorrectly answering this question. Indicate what this code prints.
def bonusCt(s):
    if (len(s) < 3):
        return s
    z = 0
    for i in range(10):
        z += ord(str(i))*i%2
    return bonusCt(s[-z::z//2])
print(bonusCt('1234567'))