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).
You will only need to modify the following files:
lab_debug.py
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?
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 one right but for some reason it only actually ‘works’ if only the very last value is odd! (Isnt that odd?) Can you figure out what’s wrong with this block of code?
HINT: 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. More directly – what problems show up when I shrink the size of a list while looping through the list?
HINT 2: If you are struggling to solve this one, print each value in the list as you loop through it and see if you can spot at least one error.
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 then 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.
HINT: There are two versions of geqThan(). How can you check which one is being called for a given input?
HINT 2: How can you rewrite the code so that both one-arg and two-arg inputs work? Can this be done in one function rather than two?
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
In a final desperate attempt to get help on his code, Brad attempted to reach out to the CS 277 class for assistance. Brad knew that his code works because he even ran it successfully beforehand! Unfortunately his student class had an unresolved bug in it – one that ironically doesn’t show up the first time you run the program! 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.