0% found this document useful (0 votes)
3 views3 pages

PDF

The document outlines a structured assignment solution for a debugging challenge, including three specific coding tasks in Python and Java. Each task identifies a bug, explains the fix, and provides corrected code examples. Additionally, it emphasizes the importance of systematic debugging, hypothesis testing, and best practices in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

PDF

The document outlines a structured assignment solution for a debugging challenge, including three specific coding tasks in Python and Java. Each task identifies a bug, explains the fix, and provides corrected code examples. Additionally, it emphasizes the importance of systematic debugging, hypothesis testing, and best practices in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

I cannot directly generate or provide a downloadable .pdf file.

However, I have formatted the


complete assignment solution below in a clear, structured layout that you can easily copy into a
document (like Microsoft Word or Google Docs) and then select File > Export/Save as PDF.

Assignment: The Debugging Dojo Solutions

Activity Type: Small Group Challenge Time Allotment: 45-60 minutes Learning Objectives: Practice
systematic debugging, testing hypotheses, and utilizing peer discussion.

Challenge 1: The Logical Labyrinth (Python)

Task: Fix the find_largest function to correctly identify the maximum value in any list of numbers.

• The Bug: The variable largest is initialized to 0. This creates a flaw in the initial assumption
because if the input list contains only negative numbers, the function will incorrectly return 0
(which is larger than any negative number) despite 0 not being in the list.

• The Fix: Initialize the largest variable using the first element of the list (numbers[0]). This
ensures the comparison starts with a valid value from the dataset.

Corrected Code:

Python

def find_largest(numbers):

if not numbers:

return None

# Initialize with the first element of the list

largest = numbers[0]

for num in numbers:

if num > largest:

largest = num

return largest

Challenge 2: The Off-by-One Odyssey (Java)

Task: Correct the countdown function to print numbers from $n$ down to $1$ followed by
"Blastoff!".

• The Bug: The loop condition is defined as i >= 0. This causes an "off-by-one" error where the
loop executes an extra iteration, printing the number 0 before the final string.

• The Fix: Change the loop condition to i > 0. This ensures the loop terminates immediately
after printing 1.
Corrected Code:

Java

public class Counter {

public static void countdown(int n) {

// Updated condition to stop at 1

for (int i = n; i > 0; i--) {

[Link](i);

[Link]("Blastoff!");

public static void main(String[] args) {

countdown(5);

Challenge 3: The Resource Riddle (Python)

Task: Optimize the file-reading function for safety and memory efficiency.

• The Bug: The original code opens a file but lacks a [Link]() statement, which can lead to
resource leaks if an exception occurs. Additionally, using [Link]() is inefficient because
it loads the entire file into memory at once.

• The Fix: Use a with block to handle the file. This is a Python best practice that ensures the
resource is automatically closed. Furthermore, iterating directly over the file object is more
memory-efficient.

Corrected Code:

Python

def print_file_lines(filename):

# 'with' ensures the file is closed automatically even if an error occurs

with open(filename, 'r') as file:

# Iterating directly over the file object is memory-efficient

for line in file:

print([Link]())
print_file_lines('[Link]')

Group Debrief and Reflection

• Localization: Identifying the precise line or condition causing the failure.

• Hypothesis Testing: Using test cases (like negative numbers) to prove or disprove
assumptions.

• Best Practices: Implementing language-specific standards like the with block in Python to
prevent non-crashing bugs like resource leaks.

You might also like