Short Exercises #2

Due: Sunday, Oct 18 at 3pm CDT

The following short exercises are intended to help you practice some of the programming concepts introduced in weeks 1, 2, and 3. These exercises should not take more than 1-2 hours in total to complete.

Exercises

Basic Functions

  1. Complete the function is_pythagorean_triple, which takes three integer parameters a, b, and c, returns True if \(a^2 + b^2\) is equal to \(c^2\), and False otherwise. For example, the call is_pythagorean_triple(4, 3, 5) should return True.

Multiple return values

  1. Complete the function characterize_nums, which takes a list of integers, and returns a tuple with the number of negative values, zeros, and positive values. For example, the call characterize_nums([-8, 0, 5, -5]) should yield (2, 1, 1).

Returning lists

  1. Complete the function compute_matching, which takes two lists of equal length and returns a list of the same length where the ith element is True if the ith elements of the two lists are equal. For example, given the lists [10, 20, 30] and [10, 30, 30], the function would return [True, False, True].

  2. Complete the function compute_matching_indices, which takes to lists of equal length and returns a list of the indices where the elements of the two lists are equal. For example, given the lists [10, 20, 30] and [10, 30, 30], the function would return [0, 2].

Modifying lists in place

  1. Complete the function destructive_negate, which negates the value of each element in the list in place. For example, if the variable lst has the value [-1, 2, -3, -4], calling destructive_negate(lst) will update the value of lst to be [1, -2, 3, 4].

Lists of lists

  1. Complete the function win_lose_or_draw, which takes a list of lists of integers, an integer row number, and integer column number and returns “Win”, “Lose”, or “Draw” depending on whether sum of the values in the row is larger, smaller, or equal to the sum of the values in the column. Here are some sample uses of the function and expected results:

    board =  [[3, 2, 3, 2],
              [3, 2, 2, 3],
              [3, 2, 2, 2],
              [3, 2, 2, 10],
              [3, 3, 3, 3]]
    
     win_lose_or_draw(board, 4, 1)    #  expected result: "Win" (12 > 11)
    
     win_lose_or_draw(board, 0, 2)    #  expected result: "Lose" (10 < 12)
    
     win_lose_or_draw(board, 4, 2)    #  expected result: "Draw" (12 == 12)
    

    As you can see from these examples, you should not assume that the number of rows and the number of columns are the same.

Testing and Submitting your Solutions

Like the previous short exercises, you will need to pull some instructor files to your repository, and then add your code to one of those files. You can find detailed instructions on how to do this in our Coursework Basics page. For instructions on how to test your code, please see our Testing Your Code

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). In the “Short Exercises #2” assignment, simply upload file se2.py (do not upload any other file!). Please note:

  • You are allowed to make as many submissions as you want before the deadline.

  • There are no extensions for the short exercises. The two free extensions you get for the programming assignments cannot be applied towards the short exercises. Please note that, if you need an extension due to extraordinary circumstances, you should alert us via a private message on Piazza.

  • Your score on the short exercises is determined solely based on the automated tests, but we may adjust your score if you attempt to pass tests by rote (e.g., by writing code that hard-codes the expected output for each possible test input).

  • Gradescope will report the test score it obtains when running your code. If there is a discrepancy between the score you get when running our grader script, and the score reported by Gradescope, please let us know so we can take a look at it.