-
Breaking the ice on Piazza [5 pts]
This task is very easy. Make a post on Piazza asking a valid question about this
homework OR answering one of questions posted by other students about this homework.
Make sure your post is public and that it follows the academic integrity policy.
-
isValidYearOfBirth(year) [5 pts]
Imagine that you
are writing a software that validates personal information entered by the users.
Write the function isValidYearOfBirth(year)
which, given a
value year
, returns True
if it is an integer value that
represents a valid year of birth, False
otherwise. Keep in mind
that, according to Wikipedia, the oldest person alive turned 119 years old on
January 1st, 2022.
-
getTheCents(n) [10 pts]
Write the function getTheCents(n)
which takes a
value n
(which represents a payment in US dollars) as input and
returns the number of cents in the payment. If n
is
an int
, the function should return 0, as it has 0 cents; otherwise,
if it isn't a float, it should also return 0, because a non-number payment make no
cents (ha!). You can assume that n will have up to 2 decimal places.
For instance,
getTheCents(3) == 0
getTheCents(3.00) == 0
getTheCents(3.96) == 96
getTheCents(3.95) == 95
getTheCents(3.1) == 10
getTheCents(3.11) == 11
-
getKthDigit(n, k) [15 pts]
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
-
setKthDigit(n, k, d=0) [15 pts]
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
-
Circle Intersection [20 pts]
Two circles in a plane intersect in zero, one, two, or infinitely many points.
The latter case occurs only in the case of two identical circles. The first case
(zero points of intersection) occurs whenever the distance between the centers
of the circles is greater than the sum of the radii or the distance between the
center is less than the absolute value of the difference in their radii.
-
distance(x1,y1,x2,y2)
First, you must write a helper function
distance(x1, y1, x2, y2)
to
find the distance between two points. This function takes
four int
or float
values representing two points and
returns the distance between those points.
-
circleIntersection(xa,ya,ra,xb,yb,rb)
Your task is to write a function
circleIntersection(xa,ya,ra,xb,yb,rb)
that will take six values as input parameters. These values represent the
coordinates (xa,ya)
of the center of circle A, and the
radius ra
of A, followed by the
coordinates (xb,yb)
of the center of circle B, and the
radius rb
of B. Your function should return the number of
intersection points (only the number, not the points). There are 4 possible
cases:
The circles do not touch each other. This could be because one is contained
in the other circle, or they are too far apart from each other. In these
cases your function should return value 0.
The circles intersect at a single point: either they touch internally or
externally. In these cases your function should return value 1.
The circles intersect at two points. Your function should return value 2.
The circles intersect at infinitely many points, i.e., they fully
overlap. Your function should return infinity. In Python you can represent
positive infinity as float(''inf'')
Hint: You should use the distance
function that you just
wrote in the first part to help you solve this problem. Remember to use
almostEqual
(instead of ==) when comparing floats!
-
colorBlender(rgb1, rgb2, midpoints, n) [30 pts]
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.