lab_debug

Disastrous Devious Debugging

Due: Jan 29, 23:59 PM

Learning Objectives

  • Practice Python debugging skills
  • Introduce useful Python shorthand
  • Review Python fundamentals (conditionals, loops, objects)

Submission Instructions

Using the Prairielearn workspace, test and save your solution to the following exercises. (You are welcome to download and work on the files locally but they must be re-uploaded or copied over to the workspace for submission).

Assignment Description

Understanding how to debug a program is probably the most important skill you can develop in computer science. That said, while a written guide such as Debug Guide can point you in the right direction for a workflow, the best way to learn how to debug code is by actually debugging code.

In this lab you will be responsible for debugging the provided file such that all given functions work as intended. If you are unclear about what a specific function should be doing, please reach out to course staff or your peers.

getGrade()

str getGrade(int score)
# INPUT:
# score is a number representing a student's score from 0 to 1000 (int)
# OUTPUT:
# A single character with the letter grade based on their score (str)
#
# Example: 
# getGrades(700) -> "C"
# getGrades(899) -> "B"

It’s the end of the semester and Brad has a problem – for some reason the grade breakdown he set up at the beginning of the semester just aren’t matching what his code is outputting. Can you figure out what Brad has done wrong and ensure that students get the grade they actually are supposed to?

The grade breakdown should match the syllabus for the class. That is to say:

900+ A
800-899 B
700-799 C
600-699 D
599 or less F

NOTE: To make the intended boundaries more clear each (non-F) letter grade should be assigned for scores starting at X00 and ending at X99 including those values. (For example, 799 is a C and 800 is a B)

checkSorted()

bool checkSorted(List[int] numbers)
# Given a list, check if the number is sorted in ascending order
# INPUT:
# numbers is an array of integers (list[int])
# OUTPUT:
# A boolean value on if the numbers are sorted (bool)
# NOTE: You may assume the input list are all numbers
# You may NOT assume a minimum or maximum size to the list

Unfortunately Brad’s troubles dont end with grades. It turns out his autograder function for checking if a list is sorted or not is broken! How can you fix this function to correctly check if all possible lists of integers are sorted in ascending order?

NOTE: Ascending order means that the list should start with the smallest value and every value is greater than the previous. (You may assume no ties for the input list)

NOTE 2: You may not assume a minimum or maximum size to a list. For the purpose of the solution, you should consider a size 0 or size 1 list to be sorted.

removeOdds()

List[int] removeOdds(List[int] list1d)
# Remove all the odd values from a list of integers
# INPUT:
# list_1d, a 1d list storing ints (List[int])
# OUTPUT:
# A 1d list storing only the even valued ints (in the same relative order)

Brad swears he implemented this function correctly but he only tested it on a few possible inputs. It turns out his solution only actually ‘works’ if the only odd value is the very last value! (Isnt that odd?) Can you figure out what’s wrong with this block of code?

HINT: You are strongly encouraged to start by looking at how the values in the list change. What built-in function will allow you to check the value of a variable at a particular point in time in the code base?

HINT 2: There are many ways to correct this code and almost all of them requires a significant adjustment to the code itself. Don’t expect a one-line fix as there’s something fundamentally flawed about the current implementation.

geqThan()

bool geqThan(int inVal, int boundary)
# Check if a given number is greater than or equal to a pre-defined boundary
# INPUT:
# inVal, an input value (int)
# boundary, the threshold being compared against. If no boundary is given, the default is 10.
# OUTPUT:
# A boolean value, true if the input value is the same value or larger than boundary and false otherwise
# EXAMPLE:
# in: 18
# out: True
# in: 18, 30
# out: False

Having spent a lot of time working with other programming languages, Brad attempted to write a fairly straightforward function checking whether or not an input number is greater than a threshold. However for some reason despite coding up two versions of the function – one for if boundary is provided and one for if boundary isn’t – the code just doesnt seem to work. What is Brad forgetting?

HINT: You should always start by checking your assumptions. Brad is assuming both functions are being called when passed the correct input. Can you confirm this? How might you modify the code to check?

HINT 2: Python is capable of handling a single function with different potential input arguments. Do you recall what we covered in class about this topic?

Class CS277Student

class `CS277Student`
# An object to model a student taking the class. 
# FIELDS:
# name is the preferred name of the student (str)
# netid is the unique id of each student (str)
# The main functionality of this class is to link the student's preferred name with their email address
# Accordingly, getEmail() and __str__() are both necessary functions for this assignment

WARNING: Due to some issues with importing student-written classes into the autograder, this assignment has its own problem on PL with its own file submission! Be sure you do both problems for the lab!

In a final desperate attempt to get help on his code, Brad attempted to reach out to the CS 277 class for assistance. Brad was very careful when testing his code this time and checked it for any number of possible inputs. Unfortunately, the error is one that will only show up after you call a function at least twice. Can you figure out what’s going wrong here and correct it?

HINT: Try printing the name and netid of a student before and after each function to figure out where in the code something is going wrong.