python data structures coursera week 5 quiz answers

Chapter 9 Quiz

1. How are Python dictionaries different from Python lists?

  • Python lists maintain order and dictionaries do not maintain order
  • Python lists store multiple values and dictionaries store a single value
  • Python dictionaries are a collection and lists are not a collection
  • Python lists can store strings and dictionaries can only store words

2. What is a term commonly used to describe the Python dictionary feature in other programming languages?

  • Associative arrays
  • Closures
  • Sequences
  • Lambdas

3. What would the following Python code print out?

stuff = dict()
  • -1
  • The program would fail with a traceback
  • 0
  • candy

4. What would the following Python code print out?

stuff = dict()
  • -1
  • 0
  • ‘candy’
  • The program would fail with a traceback

5. (T/F) When you add items to a dictionary they remain in the order in which you added them.

  • False
  • True

6. What is a common use of Python dictionaries in a program?

  • Building a histogram counting the occurrences of various strings in a file
  • Sorting a list of names into alphabetical order
  • Splitting a line of input into words using a space as a delimiter
  • Computing an average of a set of numbers

7. Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?

if key in counts:
counts[key] = counts[key] + 1
counts[key] = 1
  • counts[key] = (key in counts) + 1
  • counts[key] = counts.get(key,-1) + 1
  • counts[key] = (counts[key] * 1) + 1
  • counts[key] = key + 1
  • counts[key] = counts.get(key,0) + 1

8. In the following Python, what does the for loop iterate through?

x = dict()
for y in x :
  • It loops through all of the dictionaries in the program
  • It loops through the values in the dictionary
  • It loops through the integers in the range from zero through the length of the dictionary
  • It loops through the keys in the dictionary

9. Which method in a dictionary object gives you a list of the values in the dictionary?

  • items()
  • each()
  • all()
  • keys()
  • values()

10. What is the purpose of the second parameter of the get() method for Python dictionaries?

  • The value to retrieve
  • An alternate key to use if the first key cannot be found
  • The key to retrieve
  • To provide a default value if the key is not found

Assignment 9.4

9.4 Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

file_name = “mbox-short.txt”
file = open(file_name, “r”)
except FileNotFoundError:
print(f”File {file_name} not found.”)

sender_counts = {}

for line in file:
if line.startswith(‘From ‘):
words = line.split()
sender = words[1]
sender_counts[sender] = sender_counts.get(sender, 0) + 1

most_prolific_sender = None
most_messages = None
for sender, count in sender_counts.items():
if most_messages is None or count > most_messages:
most_prolific_sender = sender
most_messages = count

print(most_prolific_sender, most_messages)

Leave a Reply