Week 8 Homework: Algorithmic Thinking and Debugging

Topic: Slowing down to think about how we write code and how we fix broken code.

Instructions: This assignment is about algorithmic thinking and debugging. Answer in complete sentences where explanations are requested. You may use an AI agent to help you check your reasoning, but you must show your own trace/work for Part 4. The majority of these questions are on the Worksheet. This website is primarily to make it easier to copy/paste the code for Part 4.

Part 1 — Why a whole week on algorithmic thinking and debugging?

So far you've mostly been writing code that works and moving on. This week we slow down on purpose. Professional developers spend far more time reading, tracing, and fixing existing code than writing brand-new code from a blank page — and that skill depends on the same computational thinking habits you've been building since Week 1: breaking a problem into steps, predicting what a program should do, and comparing that prediction to what it actually does.

In your own words, answer:

Part 2 — Revisiting Week 2

Do not redo your Week 2 homework. Instead, reread your own solution and reflect on it with fresh eyes.

Part 3 — Debugging Your Own Work

Without rewriting the Week 2 program, describe how you would go about debugging it — or convincing yourself it has no bugs — if you had to defend its correctness to a skeptical classmate.

Part 4 — Debugging Worksheet

Below are two short Python programs. You will not be graded on writing Python — you will be graded on your process: tracing carefully, running deliberately, fixing precisely, and verifying honestly. For each program, work through all four stages in order. Do not skip ahead to running the code before you've traced it by hand.

Program 1: calculate_class_average

This function is supposed to compute the average of a list of quiz scores.

def calculate_class_average(scores):
	total: int = 0
	for i in range(len(scores) - 1):
		total = total + scores[i]
	average: float = total / len(scores)
	return average

quiz_scores: list[int] = [82, 91, 76, 88, 95]

print("Class average:", calculate_class_average(quiz_scores))

1.Trace this program by hand using quiz_scores = [82, 91, 76, 88, 95] before you run anything. What bug(s), if any, did you find?

2. Run the code. What bug(s), if any, did you find?

3. Fix the code What did you change, and why?

4. Verify the fix How do you know the bug is gone?

Program 2: find_highest_grade

This function is supposed to return the highest grade in a list of grades.

def find_highest_grade(grades):
	highest: int = grades[0]
	for i in range(len(grades)):
		if grades[i + 1] > highest:
			highest = grades[i + 1]
	return highest

test_grades: list[int] = [70, 85, 60, 99, 72]
print("Highest grade:", find_highest_grade(test_grades))

 

1.Trace this program by hand using test_grades = [70, 85, 60, 99, 72] before you run anything. What bug(s), if any, did you find?

2. Run the code. What bug(s), if any, did you find?

3. Fix the code What did you change, and why?

4. Verify the fix How do you know the bug is gone?