Homework #6

Objectives

Docstrings

For this homework, and all of the remaining homeworks, all of your modules and functions must exhibit correct documentation. Submissions that don’t pass docstring checks will receive no credit.

You should read the Docstring Requirements carefully to see how it should be done.

Exercise 6.1 Positive or Negative?

If statements can be used to differentiate between different types of numbers. Write a program, sign.py, which contains a function positive_negative, which prints out either:

  • NUMBER is negative.
  • 0 is neither negative nor positive.
  • NUMBER is positive.

depending on the sign of the number passed as a parameter.

For example,

>>> positive_negative(35)
35 is positive.
>>> positive_negative(0)
0 is neither negative nor positive.
>>> positive_negative(-4)
-4 is negative.

Exercise 6.2 Even or Odd?

Sometimes our program might get a lot of data and we’d like to sort it into multiple categories. In this problem, you will write a function that sorts integers into even and odd.

Start with the code in even_odd.py. Write the function sort_even_and_odd according to the docstring provided.

Note that a number n is even if n % 2 == 0. It is odd if n % 2 == 1. If n % 2 is anything else (i.e., a decimal), then n is a floating point number.

Exercise 6.3 Sales Tax

In a certain state, sales tax is computed as follows:

  • Everything except food and baby items is taxed at 5%.
  • Food is taxed at 3% for up to $200. The amount above $200 is taxed at 4%.
  • Baby items are taxed at 3.5%.
  • Senior citizens (age 65 and older) get a 50% discount on sales tax, unless they are 90 or over, in which case they pay no sales tax.

In a file named taxes.py, write a function sales_tax which takes the following parameters in order:

  • amount: the dollar amount to be taxed.
  • category: a string representing the category of the goods to be taxed:
    • the food category is represented as 'food'
    • the baby items category is represented as 'baby items'
    • any other string is permissible, taxed at the base rate of 5%.
  • age: the age of the customer in years.

The function should return the customer’s sales tax in dollars according to the rules above.

Example:

>>> sales_tax(100, 'baby items', 46)
3.5000000000000004
>>> sales_tax(53, 'office supplies', 65)
1.3250000000000002

Note that the answers are in dollar amounts, but have long decimals due to imprecision in floating point arithmetic.

Exercise 6.4 Blackjack

The card game Blackjack is played with a standard 52 card deck.  The rules are as follows:

  • At the start of the game, the player is dealt two cards face up. The dealer gets one card face up and one face down.
  • On a player’s turn they decide whether they want another card (“hit”) or if they want to stop (“stand”). They do this until they decide to stand or until their card values go over 21 (“bust”). You can read about card values below.
  • The dealer then does the same, under strict rules. The dealer must ‘hit’ if their combined card value is under 17, and stand otherwise.
  • The winner is the one who gets closest to 21 without going over.
  • An Ace and a royal together count as 21.

Create a file named blackjack.py. For this exercise your will define two functions:

  • get_value: this function should take a string card value and return the integer value of the card. Numbered playing cards are valued 2-10; Aces are valued as 1 and royal cards (Jack, Queen, and King) are valued at 10.
  • dealer_hits: this function should take two card string values (card1 and card2) and return a Boolean result of True if the dealer should hit and False if the dealer should stand. You should call your get_value function to determine card value for this function. Rules for the dealer hitting are as follows:
    • if the card value is under 17 the dealer should hit (True).
    • If either card is an Ace(‘A’) and the other card is a royal(‘J’, ‘Q’, ‘K’) then the dealer should not hit (False), as they have a winning hand of 21.
    • Aces can be considered 1 in value unless paired with a royal card.

You should use the following tuples in setting up your comparisons / values:

card_tuple = ('0', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10')
royal_cards = ('J', 'Q', 'K')

Hint #1: It may seem odd that the first entry in card_tuple is '0', which doesn’t correspond to any card. This is done as a convenience so that the index of each card type corresponds to its value: 'A' is at index 1, '2' is at index 2 etc. This fact can be used to avoid creating a long ugly conditional block in your get_value function.

Hint #2: The code for this problem will be much shorter and cleaner if you take advantage of the membership operators described in section 6.9 of our textbook.

Exercise 6.5 Image Manipulations

Computers typically represent images as a grid of pixels where each pixel is a tiny rectangular region of a particular color. The color of a pixel may be represented as a three-entry tuple where the first entry represents the amount of red, the second represents the amount of green and the third represents the amount of blue. These three color values (often referred to as channels) are restricted to be in the range 0-255. For example:

# Create some variables to store tuples representing common colors...
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)

Complete the following two functions for working with color values:

  • adjust_brightness This function will take two parameters: the first is a length-three tuple representing a color. The second is an integer representing a value (positive or negative) that will be added to each color channel. The return value must be a tuple where each color channel has been adjusted by the requested quantity, with values clipped to stay in the range 0-255. For example:

    >>> %Run color_utils.py
    >>> adjust_brightness((0, 0, 255), 10)
    (10, 10, 255)
    >>> adjust_brightness((100, 0, 255), -10)
    (90, 0, 245)

    Advice: As you program this function, you will probably find yourself repeating much of the code three times: once for each color channel. This is a good opportunity to use a helper function. Helper functions are functions that simplify the implementation, but aren’t explicitly required by the specification. In this case, it would be useful to have a helper function named adjust_channel that would handle the logic for increasing or decreasing the value of a single color. You can then avoid code duplication by calling this function three times in adjust_brightness.

  • same_color This function returns a Boolean value indicating whether two provided color tuples are similar enough to be considered the same color. It must take three parameters: the first two are color tuples, and the third is an integer value representing the maximum allowable difference between corresponding color channels in the two pixels. For example:

    >>> same_color((128, 25, 80), (128, 25, 80), 0)
    True
    >>> same_color((129, 25, 80), (128, 25, 80), 0) # Red differs by 1!
    False
    >>> same_color((129, 25, 80), (128, 25, 80), 1) # Allow a difference of 1!
    True

Just for fun: We’ve provided some simple demo programs that you can use to try out these functions once you have them working. In order to execute these programs, store then in the same directory as your color_utils.py file and run them using Thonny. You will need to install the Pillow (Python Imaging Library) package through the Thonny package manager.

  • adjust_brightness.py: This application allows the user to select an image (which must be in JPG format), and increase or decrease the brightness by a user-specified amount. This is accomplished by applying your adjust_brightness function to each pixel.

  • green_screen.py: This application allows the user to select an image with a green-screen background and a second image that will be used as a background replacement. It then applies a green-screen effect by checking each pixel in the original image against the green-screen color using your same_color function. Pixels that are determined to be the same color as the green screen are replaced with the corresponding pixel from the background image. You can test the application using the following two images. Right click them and download them into the same folder as your Python code.

    Sprague’s Face

    King Hall

    If you want to create your own green-screen images, this web-based image viewer will allow you to view the colors of individual pixels so you can determine the appropriate color values for the “green” in your background: https://imagecolorpicker.com.

Back to Top