From 0ff5b55ec5602e8d1b857f61f4c29611ed6e497d Mon Sep 17 00:00:00 2001 From: Michele Sessa Date: Wed, 18 Jan 2023 19:29:10 +0000 Subject: [PATCH] feat(numerical-operations-the-return): add new python exercise to scripting piscine --- .../numerical_operations_the_return_test.py | 34 ++++++++++++++ .../numerical_operations_the_return.py | 19 ++++++++ .../numerical-operation-the-return/README.md | 44 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 sh/tests/numerical_operations_the_return_test.py create mode 100644 sh/tests/solutions/numerical_operations_the_return.py create mode 100644 subjects/devops/numerical-operation-the-return/README.md diff --git a/sh/tests/numerical_operations_the_return_test.py b/sh/tests/numerical_operations_the_return_test.py new file mode 100644 index 000000000..86bcc49e3 --- /dev/null +++ b/sh/tests/numerical_operations_the_return_test.py @@ -0,0 +1,34 @@ +from numerical_operations_the_return import * +import sys + +sys.path.append('/jail/app/student') + + +def test_modulo(): + assert modulo(100, 50) == 0 + assert modulo(11, 5) == 1 + assert modulo(27, 20) == 7 + assert modulo(3.5, 0.5) == 0 + assert modulo(3.5, 1.5) == 0.5 + assert modulo(0, 2) == 0 + assert modulo(20, 0) == 0 + + +def test_divide(): + assert divide(100, 50) == 100 / 50 + assert divide(11, 5) == 11 / 5 + assert divide(27, 20) == 27 / 20 + assert divide(3.5, 0.5) == 7 + assert divide(3.5, 1.5) == 3.5 / 1.5 + assert divide(0, 2) == 0 + assert divide(20, 0) == 0 + + +def test_integer_division(): + assert integer_division(100, 51) == 1 + assert integer_division(11.0, 5.0) == 2 + assert integer_division(27.0, 20.0) == 1 + assert integer_division(3.5, 0.5) == 7 + assert integer_division(3.5, 1.5) == 2 + assert integer_division(0, 2) == 0 + assert integer_division(20, 0) == 0 diff --git a/sh/tests/solutions/numerical_operations_the_return.py b/sh/tests/solutions/numerical_operations_the_return.py new file mode 100644 index 000000000..bfed09037 --- /dev/null +++ b/sh/tests/solutions/numerical_operations_the_return.py @@ -0,0 +1,19 @@ +def modulo(a, b): + if b == 0: + return 0 + else: + return a % b + + +def divide(a, b): + if b == 0: + return 0 + else: + return a / b + + +def integer_division(a, b): + if b == 0: + return 0 + else: + return a // b diff --git a/subjects/devops/numerical-operation-the-return/README.md b/subjects/devops/numerical-operation-the-return/README.md new file mode 100644 index 000000000..9796a08bd --- /dev/null +++ b/subjects/devops/numerical-operation-the-return/README.md @@ -0,0 +1,44 @@ +## Numerical operation: the return! + +### Instructions + +Create a file `numerical_operations_the_return.py` containing the following functions: + +- `modulo(a, b)` +- `divide(a, b)` +- `integer_division(a, b)` + +We assume that `a` and `b` are numbers (`int` or `float`). + +> In case of a division by zero or modulo zero your functions should return `0`. + +### Usage + +Here is a possible `test.py` to test your functions: + +```python +import numerical_operations_the_return + +print(numerical_operations_the_return.modulo(10, 3)) +print(numerical_operations_the_return.divide(10, 3)) +print(numerical_operations_the_return.divide(10, 0)) +print(numerical_operations_the_return.integer_division(10, 3)) +``` + +```bash +$ python test.py +1 +3.3333333333333335 +0 +3 +$ +``` + +### Hints + +- Some operations will panic in special cases (like division by zero), it is very important to always account for those cases and handle them properly in order to avoid bugs. +- In `Python 2` a division with two integers will return an integer, in `Python 3` it will return a float. We assume you are using `Python 3`, in case you want to force the `Python 3` behavior you can cast one of the operands to float like so: `float(a)`. + +### References + +- [conditions](https://www.w3schools.com/python/python_conditions.asp)