15110 SUMMER SESSION TWO 2014

Lab 5 - Thursday, July 10

Goals

The purpose of this lab is to give you more practice debugging code. Even though it may seem easy to come up with an algorithm to solve a problem, when you translate that algorithm to a program, you may end up with a number of logical errors. Learning how to debug efficiently (rather than randomly hacking at your code) will help you write correct programs more quickly. Here are some steps to remember as you debug:

  1. Run your code with python and check for syntax errors.
  2. Run your functions on specific inputs and check if you get your expected output.
  3. Read through your code and observe what the function is doing at each step. See if this matches your expectation of what it should do.
  4. Put print statements at logical points in your code to print. Check if the print outputs are what you expect your program to be doing.

Step 1 and 2 are steps to check that your program is working. If either of these steps fail, step 3 and 4 will be helpful in locating the problem. If you fail because python crashes, it is also helpful to read the error message printed. It even tells you which line the error is on.

Deliverables

  1. answers.txt
  2. contains.py
  3. get_index.py
  4. square_evens.py
  5. pow.py
  6. list_mult.py

Place these files in a lab5 folder. Before leaving lab, zip up the lab5 folder and hand in the zip file.

CA Demonstration (as necessary)

Activities

For each of the following program, find the bug(s) in it. Create a text file named answers.txt that contains the answers to the following questions for each program:

  1. If you got a syntax error, how did you figure out what was wrong?
  2. What arguments did you use to demonstrate any logical bugs?
  3. What output did you expect and what did you get instead?
  4. How did this help you find the logical bugs?
Also, you should correct the code after you run your debugging test(s) and save the file as specified by the question. To save time, copy and paste the original code into your editor before modifying it.
  1. The function contains(list,key) should return True if a list contains the key and False otherwise.

    def contains(list, key):
        for item in list:
            if item == key:
                print(True)
        print(False)
    

    Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in contains.py.

  2. The function get_index(list,key) should return the first index of the key in the list. It should return -1 if the key is not in the list. (Note: In this case, we want to return -1 if the key is not found. Returning -1 is not the bug.)

    def get_index(list, key):
        for i in range(0, len(list)):
            if list(i) == key:
                return i
        return -1
    

    Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in get_index.py.

  3. The function square_evens(num_list) should square all the even numbers in num_list and then print the result.

    def square_evens(num_list):
        for num in num_list:
            if num % 2 == 0:
                num = num ** 2
        print(num_list)
    

    Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in square_evens.py.

  4. The pow(base,exp) is the power (exponentiation) function, defined recursively. It should raise the base to the power of the exponent. In other words, it should be equivalent to base ** exp. However, this function must work recursively; you can't just correct it by returning base ** exp to solve this problem.

    To compute baseexp recursively, we note that

    baseexp = base × baseexp - 1

    with the special case that

    baseexp = base, if exp = 1.

    def pow(base, exp):
        return base * pow(base,exp - 1)
        if exp == 1:
            return base
    

    Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in pow.py.

  5. The function list_mult(list, multiplier) should multiply all of the numbers in the given list by the given multiplier. The function test_mult(list) should use list_mult to create 2 different modifications of the given list: two_mult should be a list made up of all of the original list elements multiplied by 2, and five_mult should be a list made of all of the original list elements multiplied by 5.

    def list_mult(list, multiplier):
        for i in range(0,len(list)):
            list[i] = list[i] * multiplier
        return list
    
    def test_mult(list):
        two_mult = list_mult(list, 2)
        five_mult = list_mult(list, 5)
        print(two_mult)
        print(five_mult)
    

    Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in list_mult.py.