Description
General instructions
• Please add your name and email in the dedicated location at the top of your code
• Do not use any external library, except when explicitly requested
• Try to follow the naming convention proposed by PEP-8 seen in class
• Use meaningful name for your variables and functions
• Leave comments in your code to explain your code and describe the difficulties you faced
INVITATION LINK
https://classroom.github.com/a/yT3cH-TF
Exercise 1: Convert Fahrenheit to Celsius (2 points)
You will write a function fahrenheit2celsius that inputs a temperature in degree Fahrenheit and convert it to Celsius. The equation to convert Celsius to Fahrenheit is the following:
𝐶 = (𝐹 − 32)
Then you will create another function called what_to_wear which inputs the temperature in Celsius and display to the user what to wear. Will this function be fruitful or void?
Temperature Under -10 °C Between -10 °C and 0 Between 0 °C and 10 °C Between 10 °C and 20 °C More than 20 °C
Clothe Puffy jacket Scarf Sweater Light jacket T-shirt
Make sure that your code is working for any case (for instance if the temperature is exactly equal to 20 it should still display something)
Exercise 2: Area and perimeter of a triangle (4 points)
In this exercise we would like to compute the area and perimeter of a triangle given only the position of its three vertices denoted respectively 𝑝1 = (𝑥1,𝑦1), 𝑝2 = (𝑥2,𝑦2), 𝑝3 = (𝑥3,𝑦3)
Figure 1. Depiction of the triangle and its 3 vertices
1. Compute the area of the triangle
To compute the area 𝐴 of this triangle from its vertex’s coordinates, we will utilize the Shoelace formula:
(𝑥1𝑦2 + 𝑥2𝑦3 + 𝑥3𝑦1) − (𝑥1𝑦3 + 𝑥2𝑦1 + 𝑥3𝑦2)
𝐴 = | |
2
You will implement it in a function called shoelace_triangle_area you have to guess what would be the inputs and output of the function (and what will be their type of data). As a reminder, the absolute value can be computed with the function abs() in Python.
2. Compute the perimeter of the triangle
Computing the perimeter 𝑃 of a triangle is trivial when knowing the size of each of its side 𝑠1, 𝑠2, 𝑠3
𝑃 = 𝑠1 + 𝑠2 + 𝑠3
To compute these lengths, you can use the Euclidean distance between the pairs of vertices. The Euclidean distance 𝑑 between two points 𝑝1 and 𝑝2
𝑑 = 𝑑𝑖𝑠𝑡(𝑝1,𝑝2) = √(𝑥1 − 𝑥2)2 + (𝑦1 − 𝑦2)2
Create a function euclidean_distance to compute the distance between two points. Then, create a function, compute_triangle_perimeter taking full advantage of your Euclidean distance function.
Exercise 3 – Compute the area of a regular polygon
In this exercise, we will calculate the area of a regular polygon with n sides. A regular polygon is an nsided polygon in which the sides are all the same length and are symmetrically placed about a common center. For instance, a regular polygon can be a triangle, a square, a pentagon, etc. More can be seen in Figure 2.
Figure 2. Different types of regular polygons
While you are very familiar with the calculation of the area of a square or of a triangle. The general formula to estimate the area 𝐴 of a regular polygon is slightly more complex:
𝑛 × 𝑠 × 𝑎
𝐴 = ,
2
Where 𝑛 is the number of sides, 𝑠 is the length of each side, and 𝑎 is the apothem. The apothem is the distance from the center of the polygon to the midpoint of a side.
Figure 3. apothem of a hexagon
The formula to calculate the apothem is the following:
𝑠
𝑎 =
180
2 tan ( 𝑛 )
You have to create a function apothem performing this calculation. You can import the Python module “math” to perform the tangent computation, but it is the only external function allowed in this assignment.
Be particularly careful when you use an external function! Should the input of the function tan() be in radian or degree? You need to verify it from online documentations or experiments. You will have to create the function deg2rad to convert degree to radian yourself (you can also use the value of pi from the math module of python).
With the support of your previous functions, create a function polygon_area to compute the area of a regular polygon from the number of side and their length.
Here are the steps you will have to resolve for this exercise:
Reviews
There are no reviews yet.