You are on page 1of 5

Workshop 2

Programming - 0509

1. Write a function that takes two numbers and returns the minimum of those.

2. Write a function that takes five numbers and returns the maximum of those.

3. Write a function that takes a list of numbers and return the product of those elements.

4. Write a function that takes a list and returns a new list with unique elements of the
first list. For example, if the function takes [1, 4, 5, 5, 2, 3, 3, 3, 3, 4, 5], then returns a
list like [1, 4, 5, 2, 3].

5. Write a function that takes 3 numbers and the function determines if they can be
the side lengths of a triangle. Notice that three numbers can be the side lengths of a
triangle when the highest value is less than the sum of the other two. Such function
should return True if those numbers can be the side lengths of a triangle and False
otherwise.

6. Write a function that takes a natural number n and returns the sum of its divisors,
except itself.

7. Write a function that takes a natural number n and returns the sum of its digits.
Hint: You can convert the number to a string to work with its digits as you work with
the elements of a list. Notice that you can convert a string to integer later on.

8. The general equation for the quadratic equation is

ax2 + bx + c = 0

where the solutions are √


b2 − 4ac
−b +
x1 =
2a

−b − b2 − 4ac
x2 =
2a
Create a function to solve the quadratic formula given a, b, c. Return x1 , x2 with your
function. Use your function to print the solutions when a = 3.3, b = 1.7, c = −9.4.

9. Create a function that takes a natural number n and returns the harmonic number
defined by
1 1 1
Hn = + + · · · +
1 2 n
10. Create a function that takes a natural number n and returns the number of prime
numbers within the interval (1, n). Notice that numbers 1 and n are excluded.

1
11. Create the function sin t(x, error = 1e-9) that takes an angle in degrees, transform it
into radians and approximate the value of sine of such angle according to the Taylor
series for sine function that is shown below. The function should approximate the
value within an error of 1e − 9 by defect. Accordingly, the iteration should stop when
the difference between the exact value and the approximation is less than or equal to
10−9 . ∞
X (−1)n x3 x5 x7
sin(x) = =x− + − +···
0
(2n + 1)! 3! 5! 7!

12. Area of a polygon


One of the most important mathematical problems through all times has been to find
the area of a polygon, especially because real estate areas often had the shape of
polygons, and it was necessary to pay for the area. We have a polygon as depicted
below.

The vertices (”corners”) of the polygon have coordinates (x1 , y1 ), (x2 , y2 ), . . . , (xn , yn ),
numbered either in a clockwise or counter clockwise fashion. The area A of the polygon
can amazingly be computed by just knowing the boundary coordinates:

1
A= |(x1 y2 + x2 y3 + · · · + xn−1 yn + xn y1 ) − (y1 x2 + y2 x3 + · · · + yn−1 xn + yn x1 )|
2

write a function polyarea(x, y) that takes two coordinate arrays with the vertices as
arguments and returns the area.. Assume that x and y are either lists or arrays. Test
the function on a triangle and a quadrilateral where you can calculate the area by
alternative methods for comparison.
Hint: Since Python lists and arrays has 0 as their first index, it is wise to rewrite the
mathematical formula in terms of vertex coordinates numbered as x0 , x1 , . . . , xn−1 and
y0 , y1 , . . . , yn−1 .

13. Write a function that accepts some quatity that represents some money and returns
its equivalent in the minimum number of coins representing that quantity. Let us use

2
colombian money. Therefore, we have coins for: $1000, $500, $200, $100 and $50.
For example, when this function accepts the number 28750 it can return a list like
[28, 1, 1, 0, 1] because

28750 = 28 × 1000 + 1 × 500 + 1 × 200 + 0 × 100 + 1 × 50

and such configuration uses the least number of coins that adds to 28750.

14. Write a programa that includes a funciton called leap(n) that indicates if the year n
is leap (by returning True) or not (by returning False). A leap year has 366 days.
After the gregorian reformation, leap years are multiples of 4 that does not end with
two zeros. Regarding the numbers ending with two zeros, they represent a leap year
if they are multiples of 4 when their last two zeros are deleted. Therefore, 1800 and
1900 are not leap years because 18 and 19 are not multiples of 4 athough the numbers
themselves are multiples of 4. On the other hand, 2000 was a leap year because 20 is
a multiple of 4.

15. Write a program that generates a random number, x, between 1 and 50, a random
number y between 2 and 5, and computes xy .

16. Rock-Paper-Scissors game


Write a program that lets the use play Rock-Paper-Scissors against the computer.
There should be five rounds, your program should print out who won and lost each
round and the whole game.

17. Game 21
Consider some game where each participant draws a series of random integers evenly
distributed from 0 and 10, with the aim of getting the sum as close as possible to 21,
but not larger than 21. You are out of the game if the sum passes 21. After each draw,
you are told the number and your total sum, and is asked whether you want another
draw or not. The one coming closest to 21 is the winner.
Implement this game in a program.

18. Frequency of random numbers


Write a program that takes a positive integer N as input and then draws N random
integers in the interval [1,6] (both ends inclusive). In the program, count how many
of the numbers, M, that equal 6 and write out the fraction M/N. Also, print all the
random numbers to the screen so that you can check for yourself that the counting is
correct. Run the program with a small value for N (e.g., N = 10) to confirm that it
works as intended.

19. (Dice Simulation)


In this problem we want to study the output when throwing a dice. You should
calculate the frequency (given in percentage) for each of the numbers: 1, 2, 3, 4, 5, 6,
when you throw a dice N times, for N = 5, 10, 50, 10000. You should plot your results
using 4 plots in the way is illustrated in the graphic below.

3
N=5 N = 10
40 40
30 30
Frequency

20 20
10 10
0 0
1 2 3 4 5 6 2 4 6
N = 50 N = 10000
20 15
15
Frequency

10
10
5 5

0 0
1 2 3 4 5 6 1 2 3 4 5 6
Dice number Dice number

20. Plot the following functions:

(a) p
f (x) = |x|, x ∈ [−9, 9]

(b)
f (x) = 2x ln |x|, x 6= 0, f (0) = 0, x ∈ [−1, 1]

(c)
2
f (x) = e−x , x ∈ [−2, 2]

21. Plot the figure eight curve:

x = sin(t)
y = sin(t) cos(t)

for t ∈ [0, 2π].

22. Plot the figure trefoil knot:

x = sin t + 2 sin 2t
y = cos t − 2 cos 2t

for t ∈ [0, 2π].

4
23. Plot the figure butterfly curve:

x = sin(t) ecos t − 2 cos(4t) − sin5 (t/12)




y = cos(t) ecos t − 2 cos(4t) − sin5 (t/12)




for t ∈ [0, 100].

24. Write a function called parametric plots which takes input parameters a and k and
plots the parametric equation

x(t) = 2k cos(t) − a cos(kt)


y(t) = 2k sin(t) − a sin(kt)

for t ∈ [0, 2π]. Include a title for each subplot to display the values for a and k, and
use plt.axis(’equal’) to display the curve properly.

You might also like