Problem Solving with Sequence
Problem Set
The following problem sets are intended for practice. Attempt these on your own.
Donut
Problem
Problem Description
Consider a donut of height h
such that the radius is r1
and the center hole has a radius of r2
.
We let r1 > r2
because the hole cannot be bigger than the donut!
Recap that the are of a circle of radius \(r\) is given by the formula below.
We let \(\pi\) to be 3.1415 for simplicity. Afterall, we do not need high precision for our donuts, we enjoy it anyway. Now, the volume of the donut should be the area times height.
Task
Write Python code to compute and print the volume of the donut. We assume that the donut is more like a cylinder so that the formula for the volume above is valid.
Assumptions
0 < r2 < r1
0 < h
h
,r1
, andr2
are already initialized.
Hints
Hint #1
Can you compute the area of the donut if there is no hole (i.e., assume the hole is filled)?
Hint #2
Can you compute the area of the hole?
Hint #3
Given area of donut without hole (i.e., assume the hole is filled) and area of hole, can you compute the area of donut?
Let a1
be area of donut without hole (i.e., assume the hole is filled) and a2
be the area of the hole.
Hint #4
Given the area of donut, can you compute the volume?
Assuming donut_area
is the area of the donut.
Cartesian Distance
Problem
Problem Description
We can represent a point in a Cartesian coordinate with two values. One value represents the position in the x-coordinate and another value represents the position in the y-coordinate. This is written as \((x, y)\). For instance, the red point is at (-3, 1) and the green point is at (2, 3).
Given two points \(p_1 = (x_1, y_1)\) and \(p_2 = (x_2, y_2)\), the distance between them can be computed with the following formula.
Task
Write Python code to compute and print the distance between two points represented by (x1
, y1
) and (x2
, y2
).
Assumptions
x1
,x2
,y1
, andy2
are integers.x1
,x2
,y1
, andy2
are already initialized.
Hints
Hint #1
Hint #3
How do you compute the square root of a value?
Matchstick
Problem
Problem Description
We want to create a pattern using matchstick as shown above. The pattern for a given \(N\) is created by first creating a pattern for \(N - 1\) and then we add 2 additional triangles on the sides.
Task
Write Python code to compute and print the number of matchstick needed for a given \(N\).
The initial value for \(N\) is given in the variable n
.
Assumptions
0 < n
Hints
Hint #1
Can you create the pattern for \(N = 5\)?
Hint #2
n | match | |||
---|---|---|---|---|
1 | 3 | |||
2 | 7 | |||
3 | 11 | |||
4 | 15 | |||
: | : | |||
n | m1 |
Can you find the formula? Formulate \(m1\) in terms of \(n\). You can use the table above.
n | match | m1 | ||
---|---|---|---|---|
1 | 3 | 3 = 3 + 0 | ||
2 | 7 | 7 = 3 + 4 | ||
3 | 11 | 11 = 3 + 8 | ||
4 | 15 | 15 = 3 + 12 | ||
: | : | : | ||
n | m1 | m1 = 3 + m2 |
Formulate \(m2\) in terms of \(n\).
n | match | m1 | m2 | |
---|---|---|---|---|
1 | 3 | 3 = 3 + 0 | 0 = 0 * 4 | |
2 | 7 | 7 = 3 + 4 | 4 = 1 * 4 | |
3 | 11 | 11 = 3 + 8 | 8 = 2 * 4 | |
4 | 15 | 15 = 3 + 12 | 12 = 3 * 4 | |
: | : | : | : | |
n | m1 | m1 = 3 + m2 | m2 = m3 * 4 |
Formulate \(m3\) in terms of \(n\).
n | match | m1 | m2 | m3 |
---|---|---|---|---|
1 | 3 | 3 = 3 + 0 | 0 = 0 * 4 | 0 = 1 - 1 |
2 | 7 | 7 = 3 + 4 | 4 = 1 * 4 | 1 = 2 - 1 |
3 | 11 | 11 = 3 + 8 | 8 = 2 * 4 | 2 = 3 - 1 |
4 | 15 | 15 = 3 + 12 | 12 = 3 * 4 | 3 = 4 - 1 |
: | : | : | : | : |
n | m1 | m1 = 3 + m2 | m2 = m3 * 4 | m3 = n - 1 |
Does it work for \(n = 5\)?
Possible Solution
Can you convert this into a single line?
This process can also be done in reverse.