All Coursera Quiz Answers

Python for everybody Week 9 Quiz Answer

Hello Friends in this article i am gone to share Coursera Course: Python Data Structures Week 9 Quiz Answers with you..

Python Data Structures

Go to this Course: Python Data Structures


Also visit this link: Python Data Structures Week 8 Quiz Answers


 

Python Data Structures Week 9 Quiz Answers

Question 1)
How are Python dictionaries different from Python lists?
  • Python dictionaries are a collection and lists are not a collection
  • Python lists can store strings and dictionaries can only store words
  • Python lists store multiple values and dictionaries store a single value
  • Python lists are indexed using integers and dictionaries can use strings as indexes

 

Question 2)
What is a term commonly used to describe the Python dictionary feature in other programming languages?
  • Lambdas
  • Closures
  • Sequences
  • Associative arrays
Question 3)
What would the following Python code print out?
stuff = dict()
print stuff['candy']
  • The program would fail with a traceback
  • 0
  • -1
  • candy
Question 4)
What would the following Python code print out?
  • 0
  • -1
  • stuff = dict()
  • print stuff.get(‘candy’,-1)
  • The program would fail with a traceback
  • ‘candy’
Question 5)
When you add items to a dictionary they remain in the order in which you added them.
  • True
  • False
Question 6)
What is a common use of Python dictionaries in a program?
  • Computing an average of a set of numbers
  • Sorting a list of names into alphabetical order
  • Splitting a line of input into words using a space as a delimiter
  • Building a histogram counting the occurrences of various strings in a file
Question 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
else:
ย  ย  counts[key] = 1
  • counts[key] = key + 1
  • counts[key] = (counts[key] * 1) + 1
  • counts[key] = counts.get(key,0) + 1
  • counts[key] = counts.get(key,-1) + 1
  • counts[key] = (key in counts) + 1
Question 8)
In the following Python, what does the for loop iterate through?
x = dict()
...
for y in x :
ย  ย  ...
  • It loops through the values in the dictionary
  • It loops through the keys in the dictionary
  • It loops through the integers in the range from zero through the length of the dictionary
  • It loops through all of the dictionaries in the program
Question 9)
Which method in a dictionary object gives you a list of the values in the dictionary?
  • values()
  • items()
  • keys()
  • all()
  • each()
Question 10)
What is the purpose of the second parameter of the get() method for Python dictionaries?
  • The key to retrieve
  • The value to retrieve
  • To provide a default value if the key is not found
  • An alternate key to use if the first key cannot be found

 

Week 9 Assignment

9.4 Write a program to read through the mbox-short.txt and figure out who has the 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.
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
text = open(name)
maxauthor = dict()

for line in text:
  ย  line.rstrip()
  ย  if not line.startswith("From "): continue
  ย  words = line.split()
ย  ย  maxauthor[words[1]] = maxauthor.get(words[1],0)+1

largest = None
largest_author = None

for key in maxauthor:
  ย  if largest is None: largest = maxauthor[key]
  ย  if largest < maxauthor[key]:
  ย  ย  ย  largest = maxauthor[key]
  ย  ย  ย  largest_author = key

print(largest_author, largest)