15110 Summer 2017

Programming Assignment 3

Note: You are responsible for protecting your solutions to these problems from being seen by other students both physically (e.g., by looking over your shoulder) and electronically. In particular, since the lab machines use the Andrew File System to share files worldwide, you need to be careful that you do not put files in a place that is publicly accessible.

If you are doing the assignment on the Gates-Hillman Cluster machines we use in lab or on unix.andrew.cmu.edu, our recommendation is that you create a pa3 directory under ~/private/15110 for this assignment. That is, the new directory pa3 is inside a directory called 15110, which is inside the directory called private, which is inside your home directory. (Every new andrew account is set up with a private subdirectory within the account's home directory that only that account can access.) Please refer to this guide for information about managing your directories.

Overview

For this assignment, you will create a Python source file (that is, a text file containing Python code) for each of the problems below. You should store all of these files in a folder named pa3, which you will zip up and submit.

As you will discover this semester, computer programs are rarely correct when they are first written. They often have bugs. In addition to writing the code, you should test your code on multiple inputs, for which you independently know the correct output.

You should run Python and test your functions to make sure they behave as shown in the examples. Before submitting your code, you should also download this file: test_pa3.py and do the following:

> python3 -i test_pa3.py
>>> test_all()

Some of the tests will print out what your program's printed output should be and ask you to visually compare. Others will automatically check that your function returns the correct value. If the printed output doesn't match, and/or you don't see the message "All tests completed", then there is something that needs to be fixed for you to get full credit. You can isolate the problem as follows:

>>> test_largest()
Test complete
>>> test_pay()
Test complete
...

and so forth. There is a test function for each of the functions you are to write. Please note: Passing the tests does not guarantee you full credit. In the case of printed output, you must examine the output yourself to judge whether it's correct. In addition, there may be cases not covered in the tests we give you, and there may be other requirements for your code that are stated in this assignment but not checked by the test file. It is your responsibility to think about whether your function definition satisfies the requirements.

Problems

    Part One

    In this part of the assignment you will practice programming with conditionals.

  1. [1 point] In a file named largest.py define a Python function largest(x,y,z) that takes three integers as arguments and returns the largest one among them. You may assume that the function will always be called with three numbers that are not equal to one another. You can use the Boolean operator and if you need to use a compound Boolean expression in the test of a conditional.

    Example Usage:

    > pyhton3 -i largest.py
    >>> largest(300,100,500)
    500
    
    				

  2. [1 pt] Hourly workers typically earn overtime when they work more than 40 hours per week. Suppose that overtime pay is 150 percent of the regular salary for the additional hours. In a file named wages.py write a Python function called pay(wage,hours) that takes as input wage and hours, respectively, representing the hourly wage and the hours worked for a week and returns the wages earned for the week. You can assume that hours are recorded as whole numbers. For example, 4.5 hours is not a legal argument while 4 is.

    Example usage:

    > python3 -i wages.py
    >>> pay(10,15)
    150
    >>> pay(10,45)
    475.0
    >>> pay(10,44)
    460.0
    

    Part Two

    In this part of the assignment you will practice writing programs that use collections of data represented as lists, and practice the control flow structures for iteration. In some of the questions you will be using the functions you wrote in Part One.

  3. [2 points] In the file wages.py, write another Python function called pay_table(wage, hours_list) that takes a list containing hours worked for a sequence of weeks and prints a table showing wages earned in each week. If the length of list hours_list is 4, then it is to be understood as showing hours for 4 weeks of work. You may assume that hours_list is always non-empty. You are free to choose your own format for the table as long as the output clearly marks what numbers correspond to hours worked and what numbers corresponds to pay. This function must use, that is, call the function pay(wage,hours) from the previous function, which is in the same file wages.py and must return None.

    Example usage:

    > python3 -i wages.py
    >>> pay_table(10,[15,45])
    Week 1 : 150
    Week 2 : 475.0
    >>> pay_table(10,[15,44,45])
    Week 1 : 150
    Week 2 : 460.0
    Week 3 : 475.0
    
  4. [3 points] Too Cold in Pittsburgh

    The weather is getting too cold in Pittsburgh and you want to travel to a warmer place for vacation this spring break. You decide that you are going to use the skills you learnt in 15-110 to help you plan your travel destination. So you create a list of places you want to visit and gather information on their temperature and air ticket costs then write a function to determine your travel destination. You have 2 requirements for your travel destination:

    1. The temperature at the location has to be higher or equal to a certain temp_min that you set.
    2. It has to be the minimum cost out of all the locations that fulfill condition.

    In a file called vacation.py, define a function vacation(places, temps, costs, temp_min) which returns the place you will choose for vacation. The parameter places is a list of places, temps is a list of their temperatures, costs is a list of costs, and temp_min is the minimum temperature you are willing to accept. Note that the input is such that the lists places, temps, and costs are non-empty and are of equal length, and the lists places, temps, and costs correspond to each other in the expected way. For example, places[0] has a temperature of temp[0] and a cost of cost[0], and places[1] has a temperature of temp[1] and a cost of cost[1] etc. If there is no place that satisfies your criteria the function should return "Home". If there are multiple places that have the same cost and that have a tolerable temperature then your function should return the last one in the list that satisfies your criteria. You are not allowed to use the min function on lists as that will defeat the purpose of the problem.

    > python3 -i vacation.py
    >>>  places = ["San Francisco", "New York", "Orlando", "Philadelphia", "Chicago"]
    >>> temps = [50,14,44,12,3]
    >>> costs = [400,242,281,233,304]
    >>> vacation(places,temps,costs,0)
    'Philadelphia'
    >>> vacation(places,temps,costs,12)
    'Philadelphia'
    >>> vacation(places,temps,costs,13)
    'New York'
    >>> vacation(places,temps,costs,45)
    'San Francisco'
    >>> vacation(places,temps,costs,51)
    'Home'
    >>> costs = [400,250,250,250,304]
    >>> vacation(places,temps,costs,10)
    'Philadelphia'
    
  5. [3 points] ASCII-art.

    By printing out different characters at different locations, it is possible to create images. This is sometimes called ASCII art, and works best in a terminal that uses a fixed-width font. Regular shapes, such as the triangle shown below, are easy to create algorithmically.

    				*
    				* *
    				*   *
    				*     *
    				*       *
    				* * * * * *
    				

    Given a \(height\), which is the number of rows in the triangle, the triangle can be created by

    1. Printing 1 asterisk("*") on the first row,
    2. Printing 1 asterisk("*"), (2*row-3\) spaces, and another asterisk for the next \(height-2\) rows,
    3. and printing \(height\) copies of an asterisk followed by a space ("* ") for the last line.

    In a file named make_triangle.py., define a Python function make_triangle(height) that will create a triangle as decsribed above. You may assume that that height is non-negative. Note that a degenerate case of a right triangle would arise when $height =1$. In that case, we want our function to print a single asterisk. There are many ways to solve the problem. One of them involves a nested loop (a loop inside of a loop) and that is what we want you to use to get full credit in solving this problem.

    Example usage:

    > python3 -i make_triangle.py
    >>> make_triangle(5)
    *
    * *
    *   *
    *     *
    * * * * *
    
    >>> make_triangle(8)
    *
    * *
    *   *
    *     *
    *       *
    *         *
    *           *
    * * * * * * * *
    

Submission

You should now have a pa3 directory that contains the files largest.py, wages.py, vacation.py, and make_triangle.py. Zip up your directory and hand it in.