CMU 15-110 Fall 2018: Principles of Computing
Homework 5 (Due Tuesday 9-Oct at 8pm)



Team Hw5


Solo Hw5

  1. median(L) [10 pts]
    Write the function median(L) that takes a list of numbers, and returns the median of the list. The median of a list is the middle element when the list is sorted. For lists of even length, it's the average of the two middle elements. If the list is empty, return None. See the test cases in the starter code for some examples.

  2. checkKaladont(wordList) [20 pts]
    Write the function checkKaladont(wordList) that takes a list of case-insensitive words and verifies that the words in the wordList follow the rules of Kaladont (see here).

    Each word has to start with the last 2 letters of the previous word. Basically, if all the words follow this pattern, then the function returns 'passed'. However, the function also has to handle these special cases:
    • If the wordList does not have at least 2 words, return 'tooShortList'
    • If a word has non-letters in it, return 'non-alphabetic'
    • If a word occurs twice, return 'duplicateWord'
    • If a word has fewer than 2 letters in it, return 'tooShortWord'
    • If a word does not start with the last 2 letters of the word before it, return 'mismatch'

    If a wordList has more than one error, return the first error to occur working in order through the list. If a single word has more than one error, return the first error to occur in order from the list above (so if a word has a non-letter AND it is too short, you would return 'non-alphabetic', not 'tooShortWord'). See the test cases in the starter code for some examples.

  3. upDownLeftRight(steps) [20 pts]
    Write the function upDownLeftRight(steps) that takes a list of strings, which are steps telling a bot how to move. The bot starts at (0,0) and each step is a string with a case-insensitive direction followed by a space followed by a distance, as in 'up 2' or 'RIGHT 5'. The bot moves the given distance in the given direction. If no distance is included, the default distance is 1. The function returns a string describing the x,y location of the bot at the end. For example:
         assert(upDownLeftRight(['up 5', 'right 2']) == '2,5')
         assert(upDownLeftRight(['up 5', 'right 2', 'down']) == '2,4')
         assert(upDownLeftRight(['up 5', 'right 2', 'DOWN']) == '2,4')
    
    If the case-insensitive direction is not one of 'up', 'down', 'left', or 'right', return 'illegal direction: ' followed by the illegal value, as in:
        assert(upDownLeftRight(['up', 'WRONG! 2']) == 'illegal direction: WRONG!')
    
    Also, if the distance is not a legal int value, return 'illegal int: ' followed by the illegal value, as in:
        assert(upDownLeftRight(['up', 'right ACK']) == 'illegal int: ACK')
    
    Hint: you may want to use try/except to deal with illegal int values (though you can do it without that, too). See the test cases in the starter code for some examples.

  4. bonus/optional: histogram(a) [2.5 pts]
    Write the function histogram(a) that takes a list of integers between 0 and 100, inclusive, representing exam scores, and returns a string representing a histogram of that data. The details can be gleaned from this example:
       histogram([73, 62, 91, 74, 100, 77])
    
    returns this multi-line string:
    60-69: *
    70-79: ***
    80-89:
    90++ : **
    
    As usual, see the test cases in the starter code for some examples.

  5. bonus/optional: multiplyPolynomials(p1, p2) [2.5 pts]
    Background: we can represent a polynomial as a list of its coefficients. For example, [2, 3, 0, 4] could represent the polynomial 2x**3 + 3x**2 + 4. With this in mind, write the function multiplyPolynomials(p1, p2) which takes two polynomials as just defined, and returns a third polynomial which is the product of the two. For example, multiplyPolynomials([2,0,3], [4,5]) represents the problem (2x**2 + 3)(4x + 5), and: (2x**2 + 3)(4x + 5) = 8x**3 + 10x**2 + 12x + 15 And so this returns [8, 10, 12, 15]. See the test cases for some more examples.