Question 1:
The following program should read from a text file called "names.txt" that contains last names, one per line. The program should then calculate the most common first letter of the names in the file and output that letter to the screen. Complete the following program by filling in the blanks.
import string
first_letters = ""
infile = Term 1 ___________
for Term 2 ___________
whole_line = line.strip()
first_letter = Term 3 ___________
first_letter = first_letter.lower()
first_letters += Term 4 ___________
infile.close()
max_count = 0
max_count_letter = 'a'
for letter in string.ascii_lowercase:
letter_count = Term 5 ___________
if letter_count > Term 6 ___________
max_count = letter_count
Term 7 ___________
print(max_count_letter)
Question 2:
Assume the following input file, called "input.txt":
0001 23 3.2 Smith
0002 28 2.9 Jones
0003 18 3.8 Washington
Here, each line contains the ID number, credit hour count, GPA, and last name of a student. What will the output file contain after running the following program?
infile = open("input.txt", "r")
average_gpa = 0
average_hrs = 0
count = 0
for line in infile:
parts = line.split()
id = parts[0].strip()
credit_hours = int(parts[1].strip())
gpa = float(parts[2].strip())
name = parts[3].strip()
average_gpa += gpa
average_hrs += credit_hours
count += 1
infile.close()
average_hrs /= float(count)
average_gpa /= float(count)
outfile = open("output.txt", "w")
outfile.write("%.1f %s" % (average_hrs, name))
outfile.close()
___________
Question 3: Assume that the input file "input.txt" looks like the following:
Sam 92
Zoe 80
Ted 45
Sue 74
What is the output of the following code?
x = ""
infile = open("input.txt", "r")
for line in infile:
parts = line.split()
x += parts[1].strip()
print(x)
___________