Q6

In this question, we're just going to go crazy with data structures and looping.

A

In this question, you'll write some code that takes two lists, and creates a third list that is a collection of sums from the corresponding elements in the first two lists.

As an example, if the two input lists are [1, 2, 3] and [4, 5, 6], then the summed third list would be [5, 7, 9], where each element of the list is the sum of the corresponding elements from the other two lists.


In [ ]:
def sum_lists(list1, list2):
    sum_list = []
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return sum_list

In [ ]:
import numpy as np
a1 = np.random.random(50)
a2 = np.random.random(50)
np.testing.assert_allclose(a1 + a2, np.array(sum_lists(a1.tolist(), a2.tolist())))

B

In this problem, you'll set up a word counter!

The input string can be of arbitrary length; use the split() method with no arguments to split the string into a list of words that you'll then iterate through. The words will be the keys, and the number of times a word appears will be the value. Make sure you convert all the words to lowercase; use the lower() method (so "the" and "The" are counted as the same word). Use the dictionary to count the words.

Important note: if you try to update the value for a key that doesn't already exist, a dictionary will throw an error. However, if you do dictionary.get(some_key, default_value), it will return the value for some_key if that key exists, and if it doesn't, will return default_value instead. Think about how you could use this to count words.

Put the word counts in word_counts.


In [ ]:
def count_words(text):
    word_counts = {}
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return word_counts

In [ ]:
from collections import defaultdict
s = "Curabitur facilisis eu nibh eu efficitur. Fusce venenatis ligula eu gravida efficitur. Vivamus accumsan mi quis magna consequat, eu tempor tellus commodo. Integer varius massa non velit sagittis, vel posuere justo mattis. Nunc nec scelerisque risus, nec dapibus quam. Etiam non convallis arcu. Vestibulum a sapien vel risus pretium vestibulum sit amet dignissim est. Vivamus in tortor eget libero volutpat sollicitudin. Cras nec mauris eu nunc congue pharetra. Fusce quis leo vitae nulla finibus accumsan. Aliquam tincidunt, ipsum id mattis scelerisque, eros augue rutrum neque, non eleifend leo purus porta nibh. Fusce malesuada auctor urna vitae facilisis. Vivamus accumsan urna ut bibendum dignissim. In fermentum, felis quis malesuada vulputate, ligula felis consectetur massa, quis pulvinar eros enim ut dolor. Phasellus non nunc imperdiet augue placerat pulvinar eget vitae eros. Donec nec felis ex."
d = defaultdict(int,
            {'a': 1,
             'accumsan': 2,
             'accumsan.': 1,
             'aliquam': 1,
             'amet': 1,
             'arcu.': 1,
             'auctor': 1,
             'augue': 2,
             'bibendum': 1,
             'commodo.': 1,
             'congue': 1,
             'consectetur': 1,
             'consequat,': 1,
             'convallis': 1,
             'cras': 1,
             'curabitur': 1,
             'dapibus': 1,
             'dignissim': 1,
             'dignissim.': 1,
             'dolor.': 1,
             'donec': 1,
             'efficitur.': 2,
             'eget': 2,
             'eleifend': 1,
             'enim': 1,
             'eros': 2,
             'eros.': 1,
             'est.': 1,
             'etiam': 1,
             'eu': 5,
             'ex.': 1,
             'facilisis': 1,
             'facilisis.': 1,
             'felis': 3,
             'fermentum,': 1,
             'finibus': 1,
             'fusce': 3,
             'gravida': 1,
             'id': 1,
             'imperdiet': 1,
             'in': 2,
             'integer': 1,
             'ipsum': 1,
             'justo': 1,
             'leo': 2,
             'libero': 1,
             'ligula': 2,
             'magna': 1,
             'malesuada': 2,
             'massa': 1,
             'massa,': 1,
             'mattis': 1,
             'mattis.': 1,
             'mauris': 1,
             'mi': 1,
             'nec': 4,
             'neque,': 1,
             'nibh': 1,
             'nibh.': 1,
             'non': 4,
             'nulla': 1,
             'nunc': 3,
             'pharetra.': 1,
             'phasellus': 1,
             'placerat': 1,
             'porta': 1,
             'posuere': 1,
             'pretium': 1,
             'pulvinar': 2,
             'purus': 1,
             'quam.': 1,
             'quis': 4,
             'risus': 1,
             'risus,': 1,
             'rutrum': 1,
             'sagittis,': 1,
             'sapien': 1,
             'scelerisque': 1,
             'scelerisque,': 1,
             'sit': 1,
             'sollicitudin.': 1,
             'tellus': 1,
             'tempor': 1,
             'tincidunt,': 1,
             'tortor': 1,
             'urna': 2,
             'ut': 2,
             'varius': 1,
             'vel': 2,
             'velit': 1,
             'venenatis': 1,
             'vestibulum': 2,
             'vitae': 3,
             'vivamus': 3,
             'volutpat': 1,
             'vulputate,': 1})
assert count_words(s) == d

s = "_Hope._ Well Aunt Jane went down to the lake with Miss Morgan as she said she would. Then she took a notion to walk around it. That’s scene two. Scene three she saw a water lily near the edge that she wanted, and she reached for it and slipped in. The water was only a foot deep but of course she got wringing wet. She set up a S. O. S. call or whatever the latest wireless is and Betty and Lucille rushed to the rescue. First aid to the injured you know. _Hilda._ Of course poor Aunt Jane was soaking wet, and then the question was what to do? _Ruth._ Couldn’t you girls have gone to the village to get dry clothing from her suit case? _Kitty._ Nix. For she had let the chauffeur go to Cherry Valley to see his mother. _Hope._ Aunt Jane wanted a blanket wrapper, for of course Miss Morgan’s clothes wouldn’t fit her. _Hilda._ Just imagine how hilarious it would be to see Aunty sitting around all day in a blanket wrapper and worsted slippers. _Hope._ But Betty came to the rescue. She actually coaxed Aunt Jane to accept the loan of a middy blouse and skirt to wear for the rest of the day while her clothes dried in the sun. _Ruth._ Miss Pickett in a middy blouse. Where’s my Kodak? _Hope._ Oh we’ve all got to behave ourselves I can tell you, for if we don’t look out Miss Pickett will get so soured on camps, she won’t let Lucille even mention the word. _Kitty._ I’ll tell you what we must do. Betty is dressing auntie up in camp clothes, and we must do our best to make her have a nice day, and convert her to the joys of camping. She’s mad as a wet hen now. _Hope._ Well we’ll all try our best to rejuvenate her and give her a jolly day."
d = defaultdict(int,
            {'_hilda._': 2,
             '_hope._': 5,
             '_kitty._': 2,
             '_ruth._': 2,
             'a': 11,
             'accept': 1,
             'actually': 1,
             'aid': 1,
             'all': 3,
             'and': 10,
             'around': 2,
             'as': 2,
             'aunt': 4,
             'auntie': 1,
             'aunty': 1,
             'be': 1,
             'behave': 1,
             'best': 2,
             'betty': 3,
             'blanket': 2,
             'blouse': 1,
             'blouse.': 1,
             'but': 2,
             'call': 1,
             'came': 1,
             'camp': 1,
             'camping.': 1,
             'camps,': 1,
             'can': 1,
             'case?': 1,
             'chauffeur': 1,
             'cherry': 1,
             'clothes': 2,
             'clothes,': 1,
             'clothing': 1,
             'coaxed': 1,
             'convert': 1,
             'couldn’t': 1,
             'course': 3,
             'day': 2,
             'day,': 1,
             'day.': 1,
             'deep': 1,
             'do': 1,
             'do.': 1,
             'do?': 1,
             'don’t': 1,
             'down': 1,
             'dressing': 1,
             'dried': 1,
             'dry': 1,
             'edge': 1,
             'even': 1,
             'first': 1,
             'fit': 1,
             'foot': 1,
             'for': 5,
             'from': 1,
             'get': 2,
             'girls': 1,
             'give': 1,
             'go': 1,
             'gone': 1,
             'got': 2,
             'had': 1,
             'have': 2,
             'hen': 1,
             'her': 6,
             'her.': 1,
             'hilarious': 1,
             'his': 1,
             'how': 1,
             'i': 1,
             'if': 1,
             'imagine': 1,
             'in': 4,
             'in.': 1,
             'injured': 1,
             'is': 2,
             'it': 2,
             'it.': 1,
             'i’ll': 1,
             'jane': 4,
             'jolly': 1,
             'joys': 1,
             'just': 1,
             'know.': 1,
             'kodak?': 1,
             'lake': 1,
             'latest': 1,
             'let': 2,
             'lily': 1,
             'loan': 1,
             'look': 1,
             'lucille': 2,
             'mad': 1,
             'make': 1,
             'mention': 1,
             'middy': 2,
             'miss': 4,
             'morgan': 1,
             'morgan’s': 1,
             'mother.': 1,
             'must': 2,
             'my': 1,
             'near': 1,
             'nice': 1,
             'nix.': 1,
             'notion': 1,
             'now.': 1,
             'o.': 1,
             'of': 6,
             'oh': 1,
             'on': 1,
             'only': 1,
             'or': 1,
             'our': 2,
             'ourselves': 1,
             'out': 1,
             'pickett': 2,
             'poor': 1,
             'question': 1,
             'reached': 1,
             'rejuvenate': 1,
             'rescue.': 2,
             'rest': 1,
             'rushed': 1,
             's.': 2,
             'said': 1,
             'saw': 1,
             'scene': 2,
             'see': 2,
             'set': 1,
             'she': 11,
             'she’s': 1,
             'sitting': 1,
             'skirt': 1,
             'slipped': 1,
             'slippers.': 1,
             'so': 1,
             'soaking': 1,
             'soured': 1,
             'suit': 1,
             'sun.': 1,
             'tell': 2,
             'that': 1,
             'that’s': 1,
             'the': 16,
             'then': 2,
             'three': 1,
             'to': 17,
             'took': 1,
             'try': 1,
             'two.': 1,
             'up': 2,
             'valley': 1,
             'village': 1,
             'walk': 1,
             'wanted': 1,
             'wanted,': 1,
             'was': 3,
             'water': 2,
             'we': 3,
             'wear': 1,
             'well': 2,
             'went': 1,
             'wet': 1,
             'wet,': 1,
             'wet.': 1,
             'we’ll': 1,
             'we’ve': 1,
             'what': 2,
             'whatever': 1,
             'where’s': 1,
             'while': 1,
             'will': 1,
             'wireless': 1,
             'with': 1,
             'won’t': 1,
             'word.': 1,
             'worsted': 1,
             'would': 1,
             'would.': 1,
             'wouldn’t': 1,
             'wrapper': 1,
             'wrapper,': 1,
             'wringing': 1,
             'you': 3,
             'you,': 1})
assert count_words(s) == d