python data structures coursera week 3 quiz answers

Chapter 7 Quiz

1. Given the architecture and terminology we introduced in Chapter 1, where are files stored?

  • Central Processor
  • Motherboard
  • Main Memory
  • Secondary memory

2. What is stored in a "file handle" that is returned from a successful open() call?

  • The handle contains the first 10 lines of a file
  • The handle is a connection to the file’s data
  • The handle has a list of all of the files in a particular folder on the hard drive
  • All the data from the file is read into memory and stored in the handle

3. What do we use the second parameter of the open() call to indicate?

x = ‘From marquard@uct.ac.za’
  • The list of folders to be searched to find the file we want to open
  • How large we expect the file to be
  • What disk drive the file is stored on
  • Whether we want to read data from the file or write data to the file

4. What Python function would you use if you wanted to prompt the user for a file name to open?

  • alert()
  • input()
  • file_input()
  • gets()

5. What is the purpose of the newline character in text files?

  • It enables random movement throughout the file
  • It allows us to open more than one files and read them in a synchronized manner
  • It adds a new network connection to retrieve files from the network
  • It indicates the end of one line of text and the beginning of another line of text

6. If we open a file as follows:

xfile = open(‘mbox.txt’)

What statement would we use to read the file one line at a time?

  • for line in xfile:
  • while line = xfile.gets
  • while ((line = xfile.readLine()) != null) {
  • while (<xfile>) {

7. What is the purpose of the following Python code?

fhand = open(‘mbox.txt’)
x = 0
for line in fhand:
x = x + 1
print(x)
  • Remove the leading and trailing spaces from each line in mbox.txt
  • Count the lines in the file ‘mbox.txt’
  • Convert the lines in mbox.txt to lower case
  • Reverse the order of the lines in mbox.txt

8. If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?

From: stephen.marquard@uct.ac.za

From: louis@media.berkeley.edu

From: zqian@umich.edu

From: rjlowe@iupui.edu

  • split()
  • ljust()
  • rstrip()
  • trim()

9. The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered?

fname = input(‘Enter the file name: ‘)
fhand = open(fname)
  • try / except
  • setjmp / longjmp
  • try / catch / finally
  • signal handlers

10. What does the following Python code do?

fhand = open(‘mbox-short.txt’)
inp = fhand.read()
  • Checks to see if the file exists and can be written
  • Prompts the user for a file name
  • Reads the entire file into the variable inp as a string
  • Turns the text in the file into a graphic image like a PNG or JPG

Assignment 7.1

7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below. You can download the sample data at http://www.py4e.com/code3/words.txt

file_name = input(“Enter the file name: “)

try:
file_handle = open(file_name)

for line in file_handle:
print(line.strip().upper())

file_handle.close()

except FileNotFoundError:
print(“File not found:”, file_name)

except Exception as e:
print(“An error occurred:”, e)

Assignment 7.2

7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below enter mbox-short.txt as the file name.

file_name = input(“Enter the file name: “)

try:
file_handle = open(file_name)

count = 0
total = 0

for line in file_handle:
if line.startswith(“X-DSPAM-Confidence:”):
colon_pos = line.find(“:”)
value = float(line[colon_pos + 1:])

count += 1
total += value

if count > 0:
average = total / count
print(“Average spam confidence:”, average)
else:
print(“No lines starting with ‘X-DSPAM-Confidence:’ found.”)

file_handle.close()

except FileNotFoundError:
print(“File not found:”, file_name)

except Exception as e:
print(“An error occurred:”, e)

Leave a Reply