COSC 101: Exam 3 Practice Problem Solutions

Download the code for these solutions: exam3_practice.py

Problem #1 Solution

problem description

  1. 1

  2. IndexError: list index out of range

  3. 'doc'

  4. True

  5. 3

  6. AttributeError: 'NoneType' object has no attribute 'sort'

  7. False

  8. 'r r'

 

Problem #2 Solution

problem description

  1. print( s[len(s)//2] )

  2. print( s[:len(s)//2] )

  3. print( s[len(s)//2+1:] )

 

Problem #3 Solution

problem description

  1. print( s[-1::-1] )

  2. print( s[0:len(s):3] )

  3. print( s[-2::-2] )

 

Problem #4 Solution

problem description

def countsubstr(s, sub):
    count = 0
    for i in range(len(s)-len(sub)+1):
        slice = s[i:(i+len(sub))]
        if slice == sub:
            count += 1
    return count

 

Problem #5 Solution

problem description

def makeAcronym(phrase):
    acro = ''
    words = phrase.split()
    for w in words:
        acro += w[0].upper()
    return acro

 

Problem #6 Solution

problem description

def removeAs(s):
    newstr = ''
    for ch in s:
       if ch.lower() != 'a':
           newstr += ch
    return newstr

 

Problem #7 Solution

problem description

def arrange_names(names):
    name_list = names.split()
    name_list.sort()
    ordered_names = " ".join(name_list)
    return ordered_names

 

Problem #8 Solution

problem description

def unique_letters(word):
    result = ''
    for char in word:
        if char not in result:
            result += char
    return result

def guess_letters(word):
    word = unique_letters(word).upper()
    guesses = 0
    correct = []
    incorrect = []
    while len(correct) < len(word):
        if correct != []:
            print("Correct Letters:   ", ", ".join(correct))
        if incorrect != []:
            print("Incorrect Letters: ", ", ".join(incorrect))
        print(len(word)-len(correct), "letters to be guessed")

        guess = input('\nGuess a letter: ').upper()       
        if len(guess) == 1:
            guesses += 1
            if guess in word and guess not in correct:
                print("Good guess!\n")
                correct.append(guess)
            elif guess not in word and guess not in incorrect:
                print("Nope.\n")
                incorrect.append(guess)
            else:
                print("You already guessed "+guess+"!\n")

        elif guess == 'QUIT' or guess == 'EXIT' or guess == 'I GIVE UP!':
            print('You give up? Okay, the word was "'+word+'".')
            return guesses

        else:  
            print("Enter only one letter at a time.")

    return guesses

 

Problem #9 Solution

problem description

This program can be written in a single line!

def is_anagram(s, t):
    return sorted(list(s)) == sorted(list(t))

This solution uses a for loop:

def is_anagram_loop(s, t):
    for char in s:
        if s.count(char) != t.count(char):
            return False
    return True

 

Problem #10 Solution

problem description

def is_rotation(s, t):
    for i in range(len(s)):
        front = s[:i]
        back = s[i:]
        if back+front == t:
            return True
    return False

 

Problem #17 Solution

problem description

def countevens(lst):
    count = 0
    for val in lst:
        if val % 2 == 0:
            count += 1
    return count

 

Problem #18 Solution

problem description

def sum67(lst):
    sum = 0
    ignore = False
    for val in lst:
        if val == 6:
            ignore = True
        
        if not ignore:
            sum += val
        
        if ignore and val == 7:
            ignore = False
    return sum

 

Problem #19 Solution

problem description

def sumSquares(x):
    sum = 0
    for val in x:
        sum += val ** 2
    return sum

 

Problem #20 Solution

problem description

def removeOdd1(mylist):
   '''
   Remove odd elements; don't modify the parameter list, just
   return a new list.
   '''
   newlist = []
   for value in mylist:
       if value % 2 == 0:
           newlist.append(value)
   return newlist

 

Problem #21 Solution

problem description

def removeOdd2(mylist):
    '''
    Remove odd elements from the list passed as a parameter.
    '''
    i = 0
    while i < len(mylist):
        if mylist[i] % 2 == 1:
            del mylist[i]
            # don't increment i when we remove
            # an element!
        else:
            i += 1

 

Problem #22 Solution

problem description

def mostFrequent(mylist):
    maxvals = [ mylist[0] ]
    maxcount = mylist.count(maxvals[0])
    for i in range(1,len(mylist)):
        thiscount = mylist.count(mylist[i])
        if thiscount == maxcount and mylist[i] not in maxvals:
            maxvals.append(mylist[i])
        elif thiscount > maxcount:
            maxvals = [ mylist[i] ]
            maxcount = thiscount
    return maxvals

 

Problem #23 Solution

problem description

def sumOddsLt7(mylist):
    s = 0
    for value in mylist:
        if value % 2 == 1 and value < 7:
            s += value
    return s

 

Problem #24 Solution

problem description

def removeOddsLe7(mylist):
    newlist = []
    for value in mylist:
        if value % 2 == 0 and value <= 7:
            newlist.append(value)
    return newlist

 

Problem #25 Solution

problem description

def secondLargest(mylist):
    largest = max(mylist)
    tmplist = []
    for value in mylist:
        if value != largest:
            tmplist += [value]
    return max(tmplist)

An alternate solution:

def secondLargest_v2(mylist):
    largest = max(mylist)
    second = min(mylist)
    for value in mylist:
        if second < value < largest:
            second = value
    return second

 

Problem #26 Solution

problem description

def fractionsmaller(lst, x):
    lst = sorted(lst)
    n = len(lst)
    i = 0 
    while i < n and x > lst[i]: 
        i += 1
    return i/n

 

Problem #27 Solution

problem description

[5, 6, 4]

 

Problem #28 Solution

problem description

2
[4, 42]
[-1, -1]

 

Problem #29 Solution

problem description

def find_reverse_pairs(filename):
    infile = open(filename)
    L = []
    for line in infile:
        word = line.strip()
        L.append(word)
    infile.close()
    word_pairs = []
    for word in L:
        reverse_word = word[-1::-1]
        pair = [word, reverse_word]
        reverse_pair = [reverse_word, word]
        if reverse_word in L and pair not in word_pairs \
           and reverse_pair not in word_pairs:
            word_pairs.append(pair)
    return word_pairs

 

Problem #30 Solution

problem description

def is_subset(list1, list2):
    index = 0
    while index < len(list2):
      val = list2[index]
        if val not in list1:
            return False
        index += 1
    return True

 

Problem #31 Solution

problem description

def average_of_freeze_streak(templist):
    maxstreak = []
    streak = []
    inside = False
    index = 0
    while index < len(templist):
        if templist[index] <= 32:
            if not inside:
                inside = True
                streak = []
            streak += [ templist[index] ]
        else:
            if inside:
                if len(streak) > len(maxstreak):
                    maxstreak = streak
                inside = False
        index += 1

    if inside:
        if len(streak) > len(maxstreak):
            streak = maxstreak

    if len(maxstreak) > 0:
        return sum(maxstreak)/float(len(maxstreak))
    return None

 

Problem #32 Solution

problem description

def one_string(s, char):
    i = 0
    count = 0
    while i < len(s):
        if s[i] == char:
            count += 1
        i += 1
    return count

def count_chars(stringlist, char):
    i = 0
    count = 0
    while i < len(stringlist):
        count += one_string(stringlist[i], char)
        i += 1
    return count

 

Problem #33 Solution

problem description

def unique_chars(s):
    chars = []
    i = 0
    while i < len(s):
        if s[i] in chars:
            return False
        chars = chars + [ s[i] ]
        i += 1
    return True

 

Problem #34 Solution

problem description

def numBefore(s, c):
    count = 0
    index = 0
    while index < len(s):
        if s.lower()[index] < c.lower():
            count += 1
        index += 1
    return count

 

Problem #35 Solution

problem description

def cartCategorySum(cartlist, category):
    cartsum = 0
    for sublist in cartlist:
        if sublist[0].lower() == category.lower():
            cartsum += sublist[2]
    return cartsum

Problem #36 Solution

problem description

def cartCategoryProducts(cartlist, categorylist):
    product_list = []
    for sublist in cartlist:
        for cat in categorylist:
            if sublist[0].lower() == cat.lower():
                product_list.append(sublist[1].title())
    return product_list

Problem #37 Solution

problem description

def sortCartProducts(cartlist):
    # first, a list where price is first to get the sorting correct
    byprice = []
    for sublist in cartlist:
        byprice.append([sublist[2], sublist[1]])
    byprice.sort(reverse=True)

    # a "final" list to return; just want to
    # swap/reverse product name and price
    for i in range(len(byprice)):
        byprice[i].reverse()
    return byprice