TRAINING 1
1/ Let’s consider the following function :
def maxi(a,b) :
if a>=b :
return a
else :
return b
What is this function named maxi for ?
2/ Program a function named maxi4 which allows to determine the largest number among four given numbers, by using the maxi function defined above.
TRAINING 2
Let’s consider the following function :
def echo(n) :
d=" "
for i range(n) :
d=d+"Clap "
return d
What does echo(7) returns ?
TRAINING 3
1/ The fac function which is defined for natural integers except 0 is programmed as follows in Python :
def fac(a) :
b=1
for i range(1,a+1) :
b=b*i
return b
a/ What does fac(3) returns ?
b/ What does fac(6) returns ?
2/ For an integer a different from 0, what does this function work out ?
TRAINING 4
The div function, programmed opposite, is defined for two numbers a and b such as a>=1 and b>=1.
def div(a,b) :
if a%b==0 :
m=1
else :
m=0
return m
What result does this function return ?
Training 1 - Solution
1/ This function returns the maximum between the numbers a and b.
2/ def maxi4(a,b,c,d):
return maxi(maxi(a,b),maxi(c,d))
Training 2 - Solution
echo(7) returns : Clap Clap Clap Clap Clap Clap Clap
Training 3 - Solution
1/ a/ fac(3) returns 6
b/ fac(6) returns 720
2/ fac(n) returns factorial of n.
Training 4 - Solution
div(a,b) returns 1 if b is a divisor of a and 0 il b isn't a divisor of a.