Section #6 Solutions

July 26th, 2021


Written by Juliette Woodrow, Brahm Capoor, Anna Mistele, John Dalloul, and Nick Parlante

Nested Dictionaries

                    


def print_course_info(explore_courses, course_id, info):
    if course_id in explore_courses:
        if info in explore_courses[course_id]:
            print(explore_courses[course_id][info])
        else:
            print("We do not have that information for the given course.")
    else:
        print("Sorry, that course is not in our system.")


def add_price(name, gas_type, price, gas_prices):
    # add new key/val pair in outer dict if gas station isn't already present
    if name not in gas_prices:
        gas_prices[name] = dict()
    # extract dictionary associated with given gas station
    station_prices = gas_prices[name]
    # set price
    station_prices[gas_type] = price
​
    return gas_prices


def first_list(strs):
    """Return a firsts dict as above."""
    firsts = {}
    for s in strs:
        if len(s) >= 1:
            ch = s[0]
            if ch not in firsts:
                firsts[ch] = []
            firsts[ch].append(s)
    return firsts


def suffix_list(strs):
    """Return a suffixes dict as above."""
    suffixes = {}
    for s in strs:
        if len(s) >= 2:
            # use suffix as key
            suffix = s[len(s)-2:]
            if suffix not in suffixes:
                suffixes[suffix] = []
            suffixes[suffix].append(s)
    return suffixes
                    
                

Word Counts

                
def remove_vowels(s):
    out = ''
    for c in s:
        if c not in 'aieou':
            out += c
    return out

def remove_consonants(s):
    out = ''
    for c in s:
        if c in 'aieou':
            out += c
    return out

def count_lines(filename, keep_vowels):
    counts = {}
    with open(filename) as f:
        for word in f:
            word = word.strip()
            if keep_vowels:
                word = remove_vowels(word)
            else:
                word = remove_consonants(word)
            if word not in counts:
                counts[word] = 0
            counts[word] += 1
    for key in sorted(counts.keys()):
        print(key, '->', counts[key])

def main():
    args = sys.argv[1:]

    if len(args) == 2 and args[0] == '-vowels':
        count_lines(args[1], True)
    else:
        count_lines(args[0], False)
          
                
            

Big Tweet Data

                

def add_tweet(user_tags, tweet):
    user = parse_user(tweet)
    if user == '':
        return user_tags

    # if user is not in there, put them in with empty counts
    if user not in user_tags:
        user_tags[user] = {}

    # counts is the nested tag -> count dict
    # go through all the tags and modify it
    counts = user_tags[user]
    tag = parse_tag(tweet)
    if tag != '':
	    if tag not in counts:
	        counts[tag] = 0
	    counts[tag] += 1
    return user_tags


def parse_tweets(filename):
    user_tags = {}
    # here we specify encoding 'utf-8' which is how this text file is encoded
    # python technically does this by default, but it's better to be explicit
    with open(filename, encoding='utf-8') as f:
        for line in f:
            add_tweet(user_tags, line)
    return user_tags

def user_total(user_tags, user):
    """
    Optional. Given a user_tags dict and a user, figure out the total count
    of all their tags and return that number.
    If the user is not in the user_tags, return 0.
    """
    if user not in user_tags:
        return 0
    counts = user_tags[user]
    total = 0
    for tag in counts.keys():
        total += counts[tag]
    return total
                
            

Extension

                
def flat_counts(user_tags):
    """
    Given a user_tags dicts, sum up the tag counts across all users,
    return a "flat" counts dict with a key for each tag,
    and its value is the sum of that tag's count across users.
    """
    counts = {}
    for user in user_tags.keys():
        tags = user_tags[user]
        for tag in tags:
            if tag not in counts:
                counts[tag] = 0
            counts[tag] += tags[tag]
    return counts