CSCI 1133, Spring 2020
Homework 3
Due: Wednesday, February 19 at 11:55 p.m. (100 points)
Purpose: Welcome to Homework 3. The purpose of this homework is to give you a chance to write programs that solve problems requiring Python basics, including if-statements and loops. The homework problems will allow you a chance to practice using Python constructs while also developing computational problem solving skills.
Due Date: Submit your solutions to GitHub by 11:55 p.m., Wednesday, February 19th. We will do a pull from this time point. No late work will be accepted so be sure to do this before the deadline. You will not upload any work to Canvas. In addition, we will not accept any work via email.
Instructions: This assignment consists of two problems worth 100 points total. Solve each problem below by yourself (unlike the labs, where you work collaboratively), and submit each solution as a separate Python source code file. Follow all naming conventions carefully since points will be deducted if you do not follow the instructions.
Here are a few more important details:
• Unlike the computer lab exercises, this is not a collaborative assignment. You must design, implement, and test the solution to each problem on your own without the assistance of anyone other than the course instructor or TAs. In addition, you may not include solutions or portions of solutions obtained from any source other than those provided in class (so you are ONLY allowed to reuse examples from the textbook, lectures, or code you and your partner write to solve lab problems). Otherwise obtaining or providing solutions to any homework problems for this class is considered Academic Misconduct. See the syllabus and read section “Academic Honesty” for information concerning cheating. Always feel free to ask the instructor or the TAs if you are unsure of something. They will be more than glad to answer any questions that you have. We want you to be successful and learn so give us the chance to help you.
• Because all homework assignments are submitted and tested electronically, the following are very important:
ü You follow all naming conventions mentioned in this homework description.
ü You submit the correct files through Github by the due date deadline.
ü You follow the example input and output formats shown.
ü Regardless of how or where you develop your solutions, you should always remember to
push your work into Github under your own repo.
ü Regardless of how or where you develop your solutions, your programs should execute
using the python3 command on CSELabs computers running the Linux operating system.
Checklist of To-Dos:
• Remember to follow all naming instructions carefully. (Following naming convention diligently and carefully is so important that there will be a penalty for incorrectly named files or functions. For example, your Python solution for Problems A and B should be saved in files named HW3_A.py and HW3_B.py (respectively). Do not use the “-” (dash). Use the “_” (underscore).
• Remember to push your work into Github under your own repo. The specific hosting directory should be: repo-
HW3 to this directory:repo-smithx1234/homeworks/HW3. Failure to do this will result in penalties. Please make sure you use small case letters in the X500 name. Notice that the homeworks folder/directory has an “s”
• Please make your source code easy to read. Start each file with comments giving (minimally) your roster name, X500 username, your lab section number, the assignment number and problem letter. And use good coding style to ensure that the graders can understand your code.
• The problem descriptions will usually show at least one test case and the resulting correct output. However, you should test your program on other test cases (that you make up) as well. Making up good test cases is a valuable programming skill and is part of ensuring your code solution is correct.
• We will not be using your main function for our own testing but you will need to have a main in your files to test your pure functions. Since we will be importing your code, you need to include the following code in your program file:
if __name__ == “__main__”: main()
What this snippet of code does is if you are running your code directly, your main function will be called. If we import your file, your main will not be called. We call your functions directly and we do not want your main to automatically run in our code when we import your work.
Problem A: Color Conversion from CMYK to RGB (30 points)
Color representation and processing is important in many areas. The colors in a satellite image might indicate the amount of vegetation in a region; the color of a liquid might tell something about that liquid’s chemical composition; the observed color of an object consisting of known material might tell something about that object’s orientation.
There are many ways to represent colors in computer programs. One common way is by representing color using the tuple (C, M, Y, K), where C is the Cyan component of the color, M is the Magenta component of the color, Y is the Yellow component of the color, and K is the Key black component of the color. The CMYK is a subtractive color system and is based on the concept that a resulting color is a result of reflected light and you subtract ink to subtract blackness from white. We subtract different levels of ink from the C, M, Y, and K to create new colors. For example in CMYK (0, 0, 0, 0) is white and (0, 0, 0, 100) is black. We will use percentages for our numbers from [0 to 100] inclusive.
Your task is to convert the CMYK model into the RGB model that is an additive color model. The R = red, G = green, and B= blue.
The CMYK to RGB conversion algorithm follows
from https://www.rapidtables.com/convert/color/cmyk-to-rgb.html
The C, M, Y, K are converted into the R, G, B coloring system
with the following conversions:
Note: You need to convert the percentages into their decimal equivalent: If cyan = 100%, then C becomes 1 for our calculations
If cyan = 80%, then C becomes .8 for our calculations
If cyan = 0%, then C becomes 0 for our calculations
The color red, R, is calculated from the cyan(C) and black (K)
colors:
R = 255 * (1-C) * (1-K)
The green color, G, is calculated from the magenta (M) and black
(K) colors
G = 255 * (1-M) * (1-K)
The blue color, B, is calculated from the yellow (Y) and black
(K) colors:
B = 255 * (1-Y) * (1-K)
Note: return integers for the [R, G, B] by using the following
code to round and force to integers
int(round(B)) # use this for your B after
# the calculation and do R, G
# the same way
Write a Python program that prompts the user for four integer input values: C, M, Y, and K in the main function and calls a function called CMYK_to_RGB that converts from the CMYK color scheme to the RGB color scheme
Your program may assume that the cyan, magenta, yellow, and black inputs are in the appropriate range of 0 to 100 inclusive (representing percentages). You will convert the CMYK model into the RGB model by creating two functions, main and CMYK_to_RGB.
• The CMYK_to_RBG function must be pure and only does the calculations. You should return a list representing the [R, G, B] values from this function and then use this returned list to print out the R, G, B representation. Notice there are no brackets in the output.
• Only main will ask for input and print. When we test your function, we will compare your list to our own. It must be a list with the form of [R, G, B] exactly. Do not change the colors around.
You can use any type of input prompt that you would like as long as the prompts are in main and the output should follow the format shown in the examples below. Remember, your main is where you do the inputs and printing. This will not be done in the “pure” CMYK_to_RGB function. Be sure to use the formatting provided to print the information and the final RGB representation (no brackets…build the string for output from the list).
Example 1: Input prompts (first four lines) and Print out RGB representation on 5th line:
Cyan component: 0
Magenta component: 0 Yellow component: 0
Black (Key) component: 100 RGB representation: 0 0 0
Example 2 Output:
Cyan component: 80
Magenta component: 100 Yellow component: 0
Black (Key) component: 0 RGB representation: 51 0 255
Example 3 Output:
Cyan component: 80
Magenta component: 100 Yellow component: 59
Black (Key) component: 4 RGB representation: 49 0 100
Follow this output formatting. Test your program thoroughly. Use the example data above to test your program along with other scenarios and use the website to see the results – you can check your work. Revise your program until you are sure it is correct. When you are done, name the source code file HW3_A.py and push to GithHub under the /homeworks/HW3 directory.
Problem B: Updated Rock-Paper-Scissors called Rock-Paper-Scissors-Spock-Lizard Game (70 points) (from www.samkass.com/theories/RPSSL.html)
“Rock-Paper-Scissors” is a simple two-player game in which each player simultaneously chooses to play one choice from the following set: {rock, paper, scissors} The winner is determined according to the following (circular) rules:
rock beats scissors scissors beats paper paper beats rock
Ah, but knowing that we as humans have a tendency to become set in our ways, we have a friend who always seems to tie with us. We want another game that makes it harder for ties to occur. Our new rules are:
Note: A question came up about the values for the RGB representation. We want you to
int(round(R)), int(round(B)), and int(round(G)) for the [R, G, B] values in the returned list. We
want the floats to be rounded integers before you return the list.
rock beats scissors rock beats lizard scissors beats paper scissors beats lizard paper beats rock paper beats Spock Spock beats rock Spock beats scissors lizard beats paper lizard beats Spock
Write a Python program that will determine the winner of “Rock-Paper-Scissors-Spock-Lizard” but with a twist. You need to determine the winner or a tie based on the best out of at most 3 games (called a round).
Rules for determining winner or tie:
• If a player wins 2 games out of the 3, they are declared the winner. If the player wins
the first two games, you will not run the 3rd game since it is not needed.
• A tie is declared if:
o Each person has 1 win and 1 game is a tie (same choice during game). § Player 1: 1 win
§ Player 2: 1 win
§ One game: tie o All games are a tie.
§ Player 1: 0 win § Player 2: 0 win § Three games: tie
The program should contain only three functions called main, play_round, and play_game.
• The main function only makes a call to play_round with the required input parameters. In your main, you will prompt each player for 3 game plays of
either rock, paper, scissors, Spock, or lizard and store each player’s choice in a list with the first position being the player number and the next three positions containing the plays [1, “R”, “R”, “SC”]. Each list becomes an input parameter for the play_round function that will control the playing of the game. The first input parameter will be player 1’s list and the 2nd input parameter will be player 2’s list.
For example, a call in main to play_round:
play_round([1, “R”, “R”, “SC”], [2, “SP”, “L”, “S”])
• The play_round function accepts two input parameters: the first parameter is the list of plays for the first player for a round, and the second parameter is the list of plays for the second player for a round. This function will return a 1 if player 1 wins the round, a 2 if player 2 wins the round, or a “T” if it was a tie. When we call your function, we will expect only a 1, 2, or “T” for the return value.
An input list will follow this template:
[integer (player number), string(game#1 choice), string (game#2 choice), string (game#3 choice)]
Example: [1, “R”, “P”, “SC”]
Each selection/game choice must be one of the following: o “R” for rock
o “P” for paper
o “SC” for scissors o “SP” for Spock
o “L” for lizard.
• The play_game function determines the winner of a game or if it was a tie. The function takes in two input parameters, the first parameter is player 1’s play and the second parameter is player 2’s play. You will determine the winner of the game or if the game was a tie and return the following value from this function:
o Return an integer of 1, if player 1 wins
o Return an integer of 2, if player 2 wins
o Return a string of “T”, if the game was a tie
Constraints:
§ Both play_round and play_game functions are pure. You need to pass
parameters and return the value as described. Only main will ask for user input and print.
Test your program using your own test data. Remember, main is the only place where you can ask for input and print the results. Revise your program until you are sure it is correct. When you are done, name the source code file HW3_B.py and push this to GitHub under the /homeworks/HW3 directory.
Copyright @Shana Watters, 2020