Homework 1
Due Sunday 20-Jan, at 8pm
- Reminder: all problems that are not explicitly marked COLLABORATIVE must be completed individually, as stated in the course syllabus.
- To start:
- Create a folder named ‘week1’
- Download both hw1.py and cs112_s19_week1_linter.py to that folder
- Edit hw1.py using Pyzo
- When you are ready, submit hw1.py to autolab. For this hw, you may submit up to 20 times (which is way more than you should require), but only your last submission counts.
- Do not use strings, loops, lists, or recursion this week.
- Do not hardcode the test cases in your solutions.
-
isEvenPositiveInt(n) [10 pts]
Write the function isEvenPositiveInt(n) which, given a value n, returns True if it is even, positive, and an integer, and False otherwise. -
hotdogPurchase(numHotdogs) [10 pts]
Hot dogs are an American tradition. Each year, Americans eat up to 20 Billion hot dogs. A classic hot dog is made up of two components: A frank (the meat) and a bun. Yet, for reasons that mystify mankind, the franks are typically sold in packs of ten and the buns in packs of eight. And, of course, you must buy full packages. Write the function hotdogPurchase(numHotdogs) that takes the total number of hot dogs you want to make, and returns the number of packages of franks and the number of packages of buns you need to purchase. You may assume that the argument, numHotdogs, is a non-negative int and the function returns as ints the smallest number of packages of franks and buns that must be purchased.
For example:
hotdogPurchase(50) returns 5, 7. (Meaning 5 packs of franks and 7 packs of buns.) -
hotdogExcess(numHotdogs) [10 pts]
Write the function hotdogExcess(numHotdogs) that takes the total number of hot dogs you want to make (as a non-negative integer) and returns the number of excess franks and buns you will need to purchase. Hint: you may want to use hotDogPurchase, which you just wrote!
For example:
hotdogExcess(50) returns 0, 6. -
playGuessingGame() [10 pts]
Write the function playGuessingGame() which runs a short guessing game with the user. The program should ask the user two Yes-or-No questions, then guess what the user is thinking based on their answers. You must use the specific prompts shown in the example exchanges below to pass the test cases (where the bolded words are user input), so you should copy the prompts directly into your code to avoid typos.
Let's play a guessing game! Think of a type of pet. Does it have fur?Yes Can you teach it to play fetch?Yes It's a dog!Let's play a guessing game! Think of a type of pet. Does it have fur?Yes Can you teach it to play fetch?No It's a cat!Let's play a guessing game! Think of a type of pet. Does it have fur?No Can it swim?Yes It's a fish!Let's play a guessing game! Think of a type of pet. Does it have fur?No Can it swim?No It's a bird!
Hint 1: For this problem, instead of using parameters and return, you'll want to use input() and print().
Hint 2: Make sure that you don't leave any trailing whitespace after the prompts or responses. This will mess up the autograder. -
threeLinesArea(m1, b1, m2, b2, m3, b3) and helpers [20 pts]
Write the function threeLinesArea(m1, b1, m2, b2, m3, b3) that takes six int or float values representing the 3 lines:
y = m1*x + b1 y = m2*x + b2 y = m3*x + b3
First find where each pair of lines intersects, then return the area of the triangle formed by connecting these three points of intersection. If no such triangle exists (if any two of the lines are parallel), return 0.
To do this, you must write three helper functions:- lineIntersection(m1, b1, m2, b2) to find where two lines intersect (which you will
call three times)
- This function takes four int or float values representing two lines and returns the x value of the point of intersection of the two lines. If the lines are parallel, or identical, the function should return None.
- distance(x1, y1, x2, y2) to find the distance between two points (again called
three times)
- This function takes four int or float values representing two points and returns the distance between those points.
- triangleArea(s1, s2, s3) to find the area of a triangle given its
side lengths (which you will call once).
- This function takes three int or float values representing
side lengths of a triangle, and returns the area of that
triangle. To do this, you may wish to to use
Heron's Formula.
- This function takes three int or float values representing
side lengths of a triangle, and returns the area of that
triangle. To do this, you may wish to to use
Heron's Formula.
Note that helper functions help in several ways. First, they are logically simpler; they break down your logic into smaller chunks that are easier to reason over. Second, they are independently testable, so you can more easily isolate and fix bugs. And third, they are reusable, so you can use them as helper functions for other functions in the future. All good things! - lineIntersection(m1, b1, m2, b2) to find where two lines intersect (which you will
call three times)
-
COLLABORATIVE: getKthDigit(n, k) [10 pts]
NOTE: This problem is collaborative. That means you can work on it with other students! However, you must follow the collaboration rules as specified by the syllabus, and you must list all of your collaborators' andrewIDs in a comment above your solution.
Write the function getKthDigit(n, k) that takes a possibly-negative int n and a non-negative int k, and returns the kth digit of n, starting from 0, counting from the right. So:
getKthDigit(789, 0) == 9 getKthDigit(789, 1) == 8 getKthDigit(789, 2) == 7 getKthDigit(789, 3) == 0 getKthDigit(-789, 0) == 9 -
COLLABORATIVE: setKthDigit(n, k, d=0) [10 pts]
NOTE: This problem is collaborative. That means you can work on it with other students! However, you must follow the collaboration rules as specified by the syllabus, and you must list all of your collaborators' andrewIDs in a comment above your solution.
Write the function setKthDigit(n, k, d=0) that takes three integers -- n, k, and d -- where n is a possibly-negative int, k is a non-negative int, and d is a non-negative single digit (between 0 and 9 inclusive) with a default value of 0. This function returns the number n with the kth digit replaced with d. Counting starts at 0 and goes right-to-left, so the 0th digit is the rightmost digit. For example:
setKthDigit(468, 0, 1) == 461 setKthDigit(468, 1, 1) == 418 setKthDigit(468, 2, 1) == 168 setKthDigit(468, 3, 1) == 1468 setKthDigit(468, 1) == 408 -
COLLABORATIVE: colorBlender(rgb1, rgb2, midpoints, n) [20 pts]
NOTE: This problem is collaborative. That means you can work on it with other students! However, you must follow the collaboration rules as specified by the syllabus, and you must list all of your collaborators' andrewIDs in a comment above your solution.
This problem implements a color blender, inspired by this tool. In particular, we will use it with integer RGB values (it also does hex values and RGB% values, but we will not use those modes). Note that RGB values contain 3 integers, each between 0 and 255, representing the amount of red, green, and blue respectively in the given color, where 255 is "entirely on" and 0 is "entirely off".
For example, consider this case. Here, we are combining crimson (rgb(220, 20, 60)) and mint (rgb(189, 252, 201)), using 3 midpoints, to produce this palette (using our own numbering convention for the colors, starting from 0, as the tool does not number them):
color0: rgb(220, 20, 60) color1: rgb(212, 78, 95) color2: rgb(205, 136, 131) color3: rgb(197, 194, 166) color4: rgb(189, 252, 201)
There are 5 colors in the palette because the first color is crimson, the last color is mint, and the middle 3 colors are equally spaced between them.
So we could ask: if we start with crimson and go to mint, with 3 midpoints, what is color #1? The answer then would be rgb(212, 78, 95).
One last step: we need to represent these RGB values as a single integer. To do that, we'll use the first 3 digits for red, the next 3 for green, the last 3 for blue, all in base 10 (decimal, as you are accustomed to). Hence, we'll represent crimson as the integer 220020060, and mint as the integer 189252201.
With all that in mind, write the function colorBlender(rgb1, rgb2, midpoints, n), which takes two integers representing colors encoded as just described, a non-negative integer number of midpoints, and a non-negative integer n, and returns the nth color in the palette that the tool creates between those two colors with that many midpoints. If n is out of range (too small or too large), return None.
For example, following the case above: colorBlender(220020060, 189252201, 3, 1) returns 212078095
Hint: RGB values must be ints, not floats. When calculating midpoint colors, you can mostly use the built-in round function. However, the built-in round function has one major flaw: it varies in whether it chooses to round .5 up or down (ugh!). You can fix this by doing an extra check for whether a number is <number>.5 and choosing to always round up in that case.