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



Solo Hw7

  1. valuesInCol(L, c) (sets) [20 pts]
    Write the function valuesInCol(L, c) that takes a 2d list L and an index c of a column in L, and returns a set containing the unique values in the given column of L. If there is no such column, return None. For example:
        L = [ [ 'dog', 'cat', 'cow' ],
              [ 'cat', 'fox', 'cow' ],
              [ 'dog', 'elk', 'pig' ],
              [ 'cub', 'bat', 'pig' ] ]
        assert(valuesInCol(L, 0) == { 'dog', 'cat', 'cub' })
        assert(valuesInCol(L, 3) == None)
    

  2. colHasDuplicateValues(L, c) (sets) [20 pts]
    Hint: you should use valuesInCol(L, c) as a very helpful helper function here!

    Write the function colHasDuplicateValues(L, c) that takes a 2d list L and an index c of a column in L, and returns True if the given column contains any duplicate values, and False otherwise. If there is no such column, return None.

    For example:
        L = [ [ 'dog', 'cat', 'cow' ],
              [ 'cat', 'fox', 'cow' ],
              [ 'dog', 'elk', 'pig' ],
              [ 'cub', 'bat', 'pig' ] ]
        assert(colHasDuplicateValues(L, 0) == True)
        assert(colHasDuplicateValues(L, 1) == False)
        assert(colHasDuplicateValues(L, 2) == True)
        assert(colHasDuplicateValues(L, 3) == None)
    
    Again, use valuesInCol(L, c) as a very helpful helper function here. Hint: what would be True about the set of values in a column if that column has any duplicate values?

  3. valueCountsInCol(L, c) (dicts/maps) [20 pts]
    Write the function valueCountsInCol(L, c) that takes a 2d list L and an index c of a column in L, and returns a dictionary mapping each value in the given column of L to a count of the number of times that value appears in that column. If there is no such column, return None.

    For example:
        L = [ [ 'dog', 'cat', 'cow' ],
              [ 'cat', 'fox', 'cow' ],
              [ 'dog', 'elk', 'pig' ],
              [ 'cub', 'bat', 'pig' ] ]
        assert(valueCountsInCol(L, 0) == { 'dog':2, 'cat':1, 'cub':1 })
        assert(valueCountsInCol(L, 3) == None)
    

  4. mostFrequentValueInCol(L, c) (dicts/maps) [20 pts]
    Hint: you should use valueCountsInL(L, c) as a very helpful helper function here!

    Write the function mostFrequentValueInCol(L, c) that takes a 2d list L and an index c of a column in L, and returns the value in the given column of L that occurs the most. If there is a tie, return a sorted list of all such values. If there is no such column, return None.

    For example:
        L = [ [ 'dog', 'cat', 'cow' ],
              [ 'cat', 'fox', 'cow' ],
              [ 'dog', 'elk', 'pig' ],
              [ 'cub', 'bat', 'pig' ] ]
        assert(mostFrequentValueInCol(L, 0) == 'dog')
        assert(mostFrequentValueInCol(L, 1) == ['bat', 'cat', 'elk', 'fox'])
        assert(mostFrequentValueInCol(L, 2) == ['cow', 'pig'])
        assert(mostFrequentValueInCol(L, 3) == None)
    
    Again, use valueCountsInL(L, c) as a very helpful helper function here.

  5. namesOfTwentySomethings(people) (objects and sets) [20 pts]
    Background: here we will use objects (instances of Struct) to describe people. You can assume each person object has at least two properties: person.name (a lowercase string) and person.age (an integer).

    With this in mind, write the function namesOfTwentySomethings(people) that takes a list of people (objects, as just described), and returns a sorted list of the names of those people who are in their 20's (so their age is between 20 and 29 inclusive). If no such people exist, return the empty list.

    Hint: it's a good idea to use a set here to track unique names!

    For example:
        person1 = Struct()
        person1.name = 'wilma'
        person1.age = 25
    
        person2 = Struct()
        person2.name = 'betty'
        person2.age = 35
    
        person3 = Struct()
        person3.name = 'fred'
        person3.age = 28
    
        person4 = Struct()
        person4.name = 'barney'
        person4.age = 31
    
        person5 = Struct()
        person5.name = 'fred'
        person5.age = 22
    
        people = [ person1, person2, person3, person4, person5 ]
    
        assert(namesOfTwentySomethings(people) == ['fred', 'wilma'])
    
    Hint: be sure to use the Struct definition from here.