Download CS 106A Practice: File Reading and split() and more Exams Nursing in PDF only on Docsity!
CS 106A Practice: File Reading
and split()
Below is a focused practice set for file reading, line processing, split(), and dictionary
counting—the same skills emphasized in CS 106A’s file-and-dictionary material.
split() with no argument splits on any whitespace, while split(",") splits a line at
commas; CS 106A commonly uses these approaches to parse structured text files.
Table of Contents
1. Warm-Up: Predict the Output
2. Problem 1 — Word Count
3. Problem 2 — Comma-Separated Data
4. Problem 3 — Longest Word
5. Problem 4 — Average Scores
6. Solutions and Explanations
Warm-Up: Predict the Output
Question 1
What is printed?
python line = "Roses are red\nViolets are blue" words = line.split() print (len(words)) print (words[3])
Question 2
What is printed?
python line = "Ada,90,85" parts = line.split(",")
print (parts[0], parts[-1]) Question 3
What is printed?
python line = " python is fun " parts = line.split() print (parts)
Key idea: split() with no argument treats spaces and newline characters as
separators and ignores repeated whitespace. split(",") splits only at commas and
preserves empty fields if they occur.
Problem 1 — Word Count
10 points
Write a function:
python def word_count(filename):
The function reads a text file and returns the total number of whitespace-separated
words in it.
Example: if notes.txt contains:
text python is fun practice file reading
then:
python word_count("notes.txt")
should return 6.
Problem 2 — Comma-Separated Data
15 points
A file called students.txt contains one student per line in this format:
then:
python longest_word("words.txt")
should return "watermelon".
Problem 4 — Average Scores
20 points
A file called scores.txt contains lines in this format:
text Name,Score1,Score2,Score
Example:
text Ada,90,85, Bo,70,80, Cy,100,100,
Write a function:
python def best_average(filename):
The function should return the name of the student with the highest average score.
Rules:
Each line has one name followed by three integer scores.
Compute each student’s average as the sum of the three scores divided by 3.
If multiple students tie, return the first student with that highest average.
Do not modify the file.
Solutions and Explanations Warm-Up Answers
Question 1 text 6 red
split() breaks the text into ["Roses", "are", "red", "Violets", "are", "blue"].
The newline is treated as whitespace, so there are six words; index 3 is "red".
Question 2 text Ada 85
split(",") produces ["Ada", "90", "85"]. Index 0 is "Ada", and index -1 is the final
field, "85".
Question 3 text ['python', 'is', 'fun']
split() with no argument collapses consecutive spaces and ignores leading and trailing
whitespace.
Problem 1 — Word Count python def word_count(filename): count = 0 with open(filename, "r") as f: for line in f: words = line.split() count += len(words) return count
Why this works: The function reads the file line by line, splits each line into words,
and adds the number of words to a running total. This follows the standard CS 106A file-
processing pattern: open the file, process each line, strip or split as needed, and
accumulate a result.
python def best_average(filename): best_name = None best_average = - with open(filename, "r") as f: for line in f: parts = line.strip().split(",") name = parts[0] score1 = int(parts[1]) score2 = int(parts[2]) score3 = int(parts[3]) average = (score1 + score2 + score3) / 3 if average > best_average: best_average = average best_name = name return best_name
Why this works: The function parses each comma-separated line, converts the three
score fields from strings to integers, computes the average, and tracks the highest
average seen so far. Using > rather than >= preserves the first student in the event of a
tie. This combines file reading, split(","), type conversion, loops, and accumulation—
core CS 106A file-processing skills.