Professional Documents
Culture Documents
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.
ax2 + bx + c = 0
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!
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
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 .
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.
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
(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]
x = sin(t)
y = sin(t) cos(t)
x = sin t + 2 sin 2t
y = cos t − 2 cos 2t
4
23. Plot the figure butterfly curve:
24. Write a function called parametric plots which takes input parameters a and k and
plots the parametric equation
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.