Skip to content

Problem Solving with Sequence

Problem Set

The following problem sets are intended for practice. Attempt these on your own.

Donut

Problem

P01

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.

\[ A = \pi r^2 \]

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, and r2 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)?

a1 = 3.1415 * r1 * r1
Hint #2

Can you compute the area of the hole?

a2 = 3.1415 * r2 * r2
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.

donut_area = a1 - a2

Hint #4

Given the area of donut, can you compute the volume?

Assuming donut_area is the area of the donut.

volume = donut_area * h

Possible Solution
1
2
3
4
5
6
# Assume h, r1, and r2 are initialized
a1 = 3.1415 * r1 * r1
a2 = 3.1415 * r2 * r2
donut_area = a1 - a2
volume = donut_area * h
print(volume)

Cartesian Distance

Problem

P02

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.

\[ d = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2} \]

Task

Write Python code to compute and print the distance between two points represented by (x1, y1) and (x2, y2).

Assumptions

  • x1, x2, y1, and y2 are integers.
  • x1, x2, y1, and y2 are already initialized.
Hints
Hint #1

First step is to compute \(x_1 - x_2\). Also compute \(y_1 - y_2\). What is the expression?

dx = x1 - x2
dy = y1 - y2
Hint #2

How do you compute the square of a value?

Two possibilities:

dx * dx
dy ** 2

Hint #3

How do you compute the square root of a value?

v ** (0.5)
Possible Solution
1
2
3
4
dx = x1 - x2
dy = y1 - y2
dist = (dx * dx + dy * dy) ** 0.5
print(dist)

Matchstick

Problem

P03

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\)?

P03A

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
1
2
3
4
5
# Assume n is initialized
m3 = n - 1
m2 = m3 * 4
m1 = 3 + m2
print(m1)

Can you convert this into a single line?

1
2
3
4
5
# Assume n is initialized
# m3 is copied
m2 = (n - 1) * 4
m1 = 3 + m2
print(m1)
1
2
3
4
5
# Assume n is initialized
# m3 is copied
# m2 is copied
m1 = 3 + ((n - 1) * 4)
print(m1)
1
2
3
4
5
# Assume n is initialized
# m3 is copied
# m2 is copied
# m1 is copied
print(3 + ((n - 1) * 4))

This process can also be done in reverse.