Debugging Worksheet

Back to Week 11 Notes

Problem 1

The function min_max is intended to take a tuple of numbers and return a string. The string is supposed to match the style of “Min: <min value>, Max: <max value>” where and are replaced by the appropriate numbers.

Exempli gratia:

  • min_max((2, 3, 4)) returns string 'Min: 2, Max: 4'
  • min_max((6, 6.5, 7)) returns string 'Min: 6, Max: 7'
def min_max(tup):
    min = tup{0}
    max = tup[0]
    for i in tup:
        if (i > Min):
            min += i
        else:
            max = i
            
    result = "Min:" + min + " Max: " + str(max)
    return result
assert min_max((0,)) == "Min: 0, Max: 0"
assert min_max((9, 3, 5, 1, 6, 15)) == "Min: 1, Max: 15"
assert min_max((4, 5)) == "Min: 4, Max: 5"
assert min_max((5, 4)) == "Min: 4, Max: 5"

Problem 2

The function absolute_value is intended to take a string representation of an integer and return its absolute value as an integer. The function cannot use any build-it absolute value methods.

E.g.,

  • absolute_value('3') returns int 3
  • absolute_value('-4') returns int -4
def absolute_value(num):
    if (num >= 0):
        retun int(num)
    else:
        abs_value = int(num) - (-1)
        return abs_Value
assert absolute_value('0') == 0
assert absolute_value('-1') == 1
assert absolute_value('-34') == 34
assert absolute_value('1') == 1
assert absolute_value('34') == 34

Problem 3

The function vowels is intended to take a string argument and return a dictionary containing the first index where each vowel appears. If a vowel does not appear in the string, its value in the dictionary should be -1. It should be case insensitive, i.e., lower and upper case are considered the same. The dictionary keys will all be lower case.

Examples:

  • vowels("a") returns dict {'a': 0, 'e': -1, 'i': -1, 'o': -1, 'u': -1}
  • vowels("Able") returns dict {'a': 0, 'e': 3, 'i': -1, 'o': -1, 'u': -1}
  • vowels("root") returns dict {'a': 0, 'e': -1, 'i': -1, 'o': 1, 'u': -1}
def vowels(string):
    result = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
    for i in range(len(string):
        if (i not in result):
            result[string[i]] = i
            
    return result
assert vowels("UOIEA") == {'a': 4, 'e': 3, 'i': 2, 'o': 1, 'u': 0}
assert vowels("") == {'a': -1, 'e': -1, 'i': -1, 'o': -1, 'u': -1}
assert vowels("UOIEAaeiou") == {'a': 4, 'e': 3, 'i': 2, 'o': 1, 'u': 0}

Problem 4

The function sorter is intended to take two sorted lists of integers as arguments. The desired output is one new combined and sorted list.

Examples:

  • sorter([1,4],[2,3]) returns new list [1, 2, 3, 4]
  • sorter([1,2,5],[4]) returns new list [1, 2, 4, 5]
def sorter(L1, L2):
    result = []
    count1 = 0
    count2 = 0
    while count1 < len(L1) or count2 <= len[L2]):
        if (L1[count1] < L2[count2]):
            result.append(L1[count1])
        elif (L1[count1] < L2[count2]):
            result.append(count2)
            count2 -= 1
            
        return result
assert sorter([], []) == []
assert sorter([], [1]) == [1]
assert sorter([1, 2], [1]) == [1, 1, 2]
assert sorter([1], [0, 4]) == [0, 1, 4]
assert sorter([0, 3], [1, 2]) == [0, 1, 2, 3]
assert sorter([1, 2, 3, 5, 9], [2, 6, 7, 8, 10]) == [1, 2, 2, 3, 5, 6, 7, 8, 9, 10]

Problem 5

The longest_count function is meant to find how many times the longest word appears in the argument string. Assume that there is a single unique longest word, and do not include punctuation. The function should return an integer, representing the number of times the longeast word appears.

  • longest_count("a a bb c") should return 1 (since 'bb' appears once)
  • longest_count("aaa aaa bb c") should return 2 (since 'aaa' appears twice)
def longest_count(S):
    words = []
    word = ""
    for char in s:
        if char != " ":
            word += char
        if char not in ".?,:":
            word += char
        else:
            words.append(word)
            word = ""

    word_counts = {}
    for word in words:
        if word not in word_counts:
            word_counts[word] = 0
        else:
            word_counts[word] += 1

    longest_word = 0
    for word in word_counts:
        if word > longest_word:
            longest_word = word
    return longest_word        
assert longest_count('ABC ABC ABCD ABCD AA BB C ABCD') == 3
assert longest_count('The longest word is longest.') == 2
assert longest_count('a a a a a bb a a a a bb! a a a a a bb.') == 3
assert longest_count('xy! xy! z yyz z xy z, z') == 1

Back to Week 11 Notes