epicours/Algo/B2/Exercises/Imperative exercise.md

79 lines
1.1 KiB
Markdown

## Exercise 1:
```python
def test (n: int) -> bool:
return n >= 100 and n < 100
def sum_digits (n: int) -> int:
a = n//100
b = (n//10)%10
c = n%10
return (a+b+c)
def product_digits (n):
a = n//10
b = (n//10) % 10
c = n % 10
return (a*b*c)
def abs (n):
if n > 0:
return n
else:
return -n
def loop (n):
n = abs(n)
if (sum_digits(n) == product_digits(n)):
return n
else
return loop(n+1)
```
## Exercise 2:
```python
def max3 (x: int, y: int, z: int) -> int:
if ((x > y) and (x > z)):
m = x
elif ((y > x) and (y > z)):
m = y
elif ((z > x) and (z > y)):
m = z
return m
def max3(x: int, y: int, z: int) -> int:
"""
Function that return the max of 3 numbers
::params:
x: int -> The first value
y: int -> The second value
z: int -> The third value
::returns:
The value of the biggest value
"""
if x > y:
m = x
else:
m = y
if m > z:
return m
else:
return z
def main():
print("Give a 3-digit number")
n = int(input())
if test(n):
res = loop(n)
if res != 1:
print(res, "is the mystery number")
else:
print("Try again")
main()
else:
print("set a 3 digits number")
main()
```