5.6. ExercisesΒΆ

  1. Give the Python interpreter’s response to each of the following from a continuous interpreter session:

    1. >>> from toolz import get, assoc, dissoc
      >>> d = {'apples': 15, 'bananas': 35, 'grapes': 12}
      >>> get('bananas',d)
      
    2. >>> d = assoc(d, 'oranges', 20)
      >>> len(d)
      
    3. >>> 'grapes' in d
      
    4. >>> get('pears', d)
      
    5. >>> get('pears', d, 0)
      
    6. >>> fruits = sorted(list(d.keys()))
      >>> fruits
      
    7. >>> d = dissoc(d, 'apples')
      >>> 'apples' in d
      
  1. Write a function takes a body of text as input and returns a dictionary that maps each unique word in the text to its last position in the text, where the position is the based on word order. Apply this function to the text version of Alice’s Adventures in Wonderland. (You can obtain a free plain text version of the book, along with many others, from http://www.gutenberg.org.)

    What is the last position of the word, alice, occur in the book?

    # be sure to clean up the string first
    # (lowercase and no punctuation).
    In [1]: s = "the only thing we have to fear is fear itself"
    
    In [2]: words = s.split()
    
    # You should compose this function with your functions for
    # cleaning the text
    In [3]: position_to_end = lambda words: {word: index
       ...:                                  for index, word in enumerate(words)}
       ...: 
    
    In [4]: position_to_end(words)
    Out[4]: 
    {'fear': 8,
     'have': 4,
     'is': 7,
     'itself': 9,
     'only': 1,
     'the': 0,
     'thing': 2,
     'to': 5,
     'we': 3}
    
  1. Refer to the last problem. This time write a function that maps each unique word in the text to its first position in the text. Apply this function to the text version of Alice’s Adventures in Wonderland. Your solution should use a dictinary comprehension along with enumerate.

    What is the first position of the word, alice, occur in the book?

  1. Refer to the last two problems. This time we will map each unique word to a tuple that gives the line number and word position on the line for the first occurance of a word. For example (3,5) means that the corresponding word is first occurs as the 5th word on line 3. Write a function that creates this dictionary. Apply this function to the text version of Alice’s Adventures in Wonderland.

    Hint: You will need to apply the dictionary comprehension trick from problem 2 twice, once for lines and again for words.

    Find the line-word pair for alice.

  1. Write a function that uses a set comprehension to determine how many unique words are in the body of a text.

    Apply this function to Alice’s Adventures in Wonderland.

  1. Here’s a table of English to Pirate translations

    English Pirate
    sir matey
    hotel fleabag inn
    student swabbie
    boy matey
    madam proud beauty
    professor foul blaggart
    restaurant galley
    your yer
    excuse arr
    students swabbies
    are be
    lawyer foul blaggart
    the th’
    restroom head
    my me
    hello avast
    is be
    man matey

    Write a function that takes a sentence in English as input and returns the sentence translated to Pirate.

  1. Consider the following set of keys.

    keys = [1, 'a', 2.5]
    

    Use the hash function to determine the hash value for each key then construct a diagram of a balanced binary search tree based on these hash values. Submit a pdf of your image.

  1. Determine the computational complexity of associating a new value to a key that already exists in a dictionary. You can assume that the dictionary always consists of a perfectly balanced binary tree (which is not true in practice).
  1. Consider the JSON examples given on json.org. Read the JSON data from the first example (starts with “glossary” key) into python and use a call to get_in to extract the string “XML” from this data structure.

    s = '''
    <paste JSON here>
    '''
    from toolz import get_in
    from json import loads
    data = loads(s)
    get_in(["glossary", "GlossDiv", "GlossEntry", "GlossDef", "GlossSeeAlso", 2], data)
    
  1. Consider the JSON examples given on json.org. Read the JSON data from the third example (starts with “menu” key) into python and use a call to get_in to extract the string “CloseDoc()” from this data structure.
  1. Consider the JSON examples given on json.org. Read the JSON data from the fourth example (starts with “web-app” key) into python and use a call to get_in to extract the string “toolstemplates/” from this data structure.
Next Section - 6. Reading Data from Files