Type: Group Assignment

Due: by 10:00 PM on Friday, February 8, 2019

Prologue

This is a group assignment. Be sure to review our Code of Conduct and be careful to adhere to it when working on the assignment. Keep in mind that you both must be authors of all code submitted and are strongly encouraged to only work on the assignment when you are with your partner.

Problem 1

Sometimes programmers write functions that are difficult to read. Let’s take a look at the following function.

def apple(orange, banana):
    """Given an orange and a banana, will return a pear"""
    pear, kiwi = [], 0
    while kiwi < len(orange) or kiwi < len(banana):
        mango = pear + [5]
        if kiwi < len(orange) and len(mango) > len(pear):
            pear.append(orange[kiwi])
        if kiwi < len(banana) or False:
            pear.append(banana[kiwi])
        kiwi = kiwi * 1 + (17 - 4*4)
    return pear

a. Determine what the function is doing and give it a more appropriate name.

b. The variable names aren’t very helpful. Give them names that are more descriptive of what they are.

c. Clean up the function by removing any unnecessary code that does not serve a purpose.

d. Write a more appropriate docstring for the function and include the following sections

  • A short one-line purpose statement
  • A Parameters section listing the types of the parameters
  • A Returns section briefly summarizing the return value and its type
  • A Preconditions section if there are more restrictions on the input other than their type
  • A Postconditions section that describes, in more detail, the exact relationship between the output and the inputs

Problem 2

Consider the following function.

def join_with_spaces(str_list):
    """Joins the list of strings into one space-separated string

    Parameters:
        str_list: A list of strings

    Returns:
        A string that is the concatenation of all the strings in
        str_list but with a single space character in-between each
        string

    Postconditions:
        * The output should be identical to " ".join(str_list)
        * If str_list = [s1, s2, s3, ..., sn], then the result
          will be:
            s1 + " " + s2 + " " + s3 + ... + " " + sn
    """
    combined_str = str_list[0] + " "
    for i in range(1, len(str_list)):
        combined_str = combined_str + str_list[i] + " "

    # Removes the trailing whitespace at the end of the string
    combined_str = combined_str.strip()

    return combined_str

a. Save the function above in a file named join_with_spaces.py.

b. Write a comprehensive test suite for the join_with_spaces function and place your tests in a file named join_with_spaces_test.py. Your tests should be runnable from the Terminal using pytest. Below is some starter code for your tests:

from join_with_spaces import join_with_spaces

def check(lst):
    """A helper function that compares join_with_spaces to " ".join"""
    assert join_with_spaces(lst) == " ".join(lst)

def test_simple_lists():
    """Checks really basic lists that are most common"""
    check(["abc", "def"])
    check(["abc", "def", "ghi"])
    check(["a", "b", "c"])

c. The join_with_spaces function contains errors. You should use your test suite to find these errors and place the corrected code in a file named join_with_spaces_fixed.py.

Problem 3

Write a function called grid_points(n, k) that takes two integers and returns a list of lists that consists of all the points [x,y] where 0 <= x < n and 0 <= y < k. For example,

>>> grid_points(2,3)
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
>>> grid_points(1,1)
[[0, 0]]
>>> grid_points(5, 1)
[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]]

Your grid_points function should also raise a TypeError exception if one of the parameters is the wrong type and a ValueError if either parameter is less than or equal to zero. Be sure to give helpful error messages!

You should save your function in a file named grid_points.py and include a docstring description of it that has

  • A short one-line purpose statement
  • A Parameters section listing the types of the parameters
  • A Returns section briefly summarizing the return value and its type
  • A Preconditions section if there are more restrictions on the input other than their type
  • A Postconditions section that describes, in more detail, the exact relationship between the output and the inputs
  • A Raises section that describes what exceptions it raises and under what conditions

How to Turn in Your Code

a. Make sure that you and your partner are clearly identified at the top of every file you have created for this assignment.

b. All of your files need to be placed into the Hand-in/USERNAME/assignment5 directory. You can do this visually in Finder by copying all of your files in your StuWork/USERNAME/assignment5 directory into your Hand-in/USERNAME/assignment5 directory. (Note that you only need to do this once—it is not necessary for every member of your group to submit separately.)

c. Send all of your files to your partner so they have access to them, too!

Important Grading Criteria

We will evaluate your work with the following criteria in mind.

  1. Correctness. Does your code do what is asked? (e.g. named correctly, input and outputs correct values, etc.)
  2. Formatting. Is your code formatted so that it is easy to understand?
  3. Elegance. Is your code concise and elegant? Does it use subroutines when appropriate rather than copying and pasting certain parts?