Programming

MATH 230 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 7 - Dictionaries

Due Date: November 13, 2023

Upload

The assignment is due by 11:59 pm on the due date.

Problems

  1. Create the following dictionary in the Python console:
    >>> letters_count = {"a": 1, "b": 2, "c": 3}
    In the Python console, type letters_count. and then press the TAB key (note the period after count). You should see a list of methods that can be applied to a dictionary. What are all the methods? Then, choose four methods, describe what they do, and apply them to letters_count. Hint: You may find the following site useful: Dictionaries in Python
  2. Write a function that takes one parameter which is supposed to be a list and returns a dictionary whose keys are the distinct elements of the input list and a value is the number of times a key appears in the list. For example, if the input list is

    ['red', 4, 'green', 'cat', 'cat', 4, 4, 'green', 8]
    then your function would return the following dictionary:
    {'red' : 1 , 4 : 3, 'green' : 2, 'cat' : 2, 8 : 1}

    Apply your function to the following list (copy-paste into Python):

    ['red', 'blue', 'red', 'green', 'cyan', 'blue', 'green', 
    'blue', 'red', 'blue', 'red', 'blue', 'red', 'green', 'blue', 
    'blue', 'green', 'red', 'red', 'cyan', 'green', 'red', 'cyan', 
    'cyan', 'cyan', 'cyan', 'red', 'blue', 'blue', 'cyan', 'green', 
    'green', 'green', 'red', 'red', 'blue', 'cyan', 'green', 
    'cyan', 'green']
  3. Consider the following list of pairs of names that represents friends in a social network:

    [('ron', 'tim') , ('bob', 'anna'), ('anna', 'tim'), 
    ('tom', 'kate'), ('anna', 'tom'), ('kate', 'ron')]

    From the list above, we gather that Ron and Tim are friends, Bob and Anna are friends, Anna and Tim are friends, and so on. Another way to represent the network of friends is to use a dictionary whose keys are the names of the people in the network and a value is a list containing the friends of the key. For example, a representation of the above network of friends as a dictionary is:

    {
    'ron' : ['tim', 'kate'], 
    'tim' : ['ron', 'anna'], 
    'bob' : ['anna'], 
    'anna' : ['bob', 'tim', 'tom'], 
    'tom' : ['kate', 'anna'], 
    'kate' : ['tom', 'ron'] 
    }

    Write a function that converts a list of friends into a dictionary as described above. Thus, the parameter to your function will be a list containing two-element tuples and the return value of the function is a dictionary. Your function should work on lists whose tuples contain any immutable value and not just strings. For instance, instead of strings for the names we could simply use integers. Try your function on the following list:

    [(1, 22), (1, 5), (1, 10), (22, 3), 
    (22, 93), (88, 10), (10, 7), (93, 7), 
    (88, 6), (93, 6), (5, 6), (5, 4), 
    (4, 7), (88, 3), (4, 3)]

    The return value should be

    {
     1: [22, 5, 10],
     22: [1, 3, 93],
     5: [1, 6, 4],
     10: [1, 88, 7],
     3: [22, 88, 4],
     93: [22, 7, 6],
     88: [10, 6, 3],
     7: [10, 93, 4],
     6: [88, 93, 5],
     4: [5, 7, 3]
    }
    
  4. To communicate securely over a network, the following code book is used:

    code_book = {
     'a': 414,
     'b': 356,
     'c': 441,
     'd': 269,
     'e': 412,
     'f': 490,
     'g': 391,
     'h': 446,
     'i': 211,
     'j': 243,
     'k': 267,
     'l': 271,
     'm': 314,
     'n': 479,
     'o': 341,
     'p': 403,
     'q': 266,
     'r': 482,
     's': 396,
     't': 417,
     'u': 485,
     'v': 322,
     'w': 214,
     'x': 222,
     'y': 220,
     'z': 371,
     '$': 261,
     '1': 262,
     '2': 342,
     '3': 426,
     '4': 203,
     '5': 300,
     '6': 292,
     '7': 287,
     '8': 272,
     '9': 380,
     '0': 420,
     ',': 254,
     ' ': 392
    }

    Write two functions, one called make_secret and the other called get_message. The make_secret function will take one string parameter and return an encoded version of the string as a list of numbers by converting each character in the input string to its equivalent 3-digit number in the code_book. For example, if the input to make_secret is the string 'hello world' (the message) then make_secret would return the following list (the secret):

    [446, 412, 271, 271, 341, 392, 214, 341, 482, 271, 269]

    The get_message function will perform the reverse operation of make_secret, in other words, get_message will take as input a list of numbers (the secret) and returns the corresponding string (the message). All variables that you need to implement your function must be generated with code, that is, do not type 'by hand' the values of any variables (you can copy-paste code_book into Python). Use your get_message function to decode the following secret message (copy-paste into Python):

    secret_message = [
     417,
     446,
     412,
     392,
     314,
     412,
     269,
     211,
     414,
     479,
     392,
     414,
     479,
     479,
     485,
     414,
     271,
     392,
     214,
     414,
     391,
     412,
     392,
     490,
     341,
     482,
     392,
     441,
     341,
     314,
     403,
     485,
     417,
     412,
     482,
     392,
     403,
     482,
     341,
     391,
     482,
     414,
     314,
     314,
     412,
     482,
     396,
     392,
     214,
     414,
     396,
     392,
     261,
     380,
     287,
     254,
     272,
     420,
     420,
     392,
     211,
     479,
     392,
     342,
     420,
     342,
     342]

    And use your make_secret function on the string 'learn to code'. Hint: Here is a quick tutorial reminder on how to access individual characters in a string.