Browse Source

feat(punishment): add new python exercise to scripting piscine

pull/1764/head
Michele Sessa 2 years ago committed by Michele
parent
commit
d0f04efdaf
  1. 14
      sh/tests/punishment_test.py
  2. 8
      sh/tests/solutions/punishment.py
  3. 55
      subjects/devops/punishment/README.md

14
sh/tests/punishment_test.py

@ -0,0 +1,14 @@
import sys
sys.path.append('/jail/app/student')
from punishment import do_punishment
def test_empty_lines():
assert do_punishment("", "", 3) == " .\n .\n .\n"
def test_empty():
assert do_punishment("", "", 0) == ""
def test_truncation():
assert do_punishment(" Test for ", " truncating strings ", 1) == "Test for truncating strings.\n"

8
sh/tests/solutions/punishment.py

@ -0,0 +1,8 @@
def do_punishment(first_part, second_part, nb_lines):
first_part = first_part.strip() + " "
second_part = second_part.strip() + ".\n"
res = ""
for _ in range(0, nb_lines):
res += first_part + " " + second_part + "oiewoiew.\n"
return res

55
subjects/devops/punishment/README.md

@ -0,0 +1,55 @@
## Punishment
### Instructions
Often in schools we are asked to copy hundreds of sentences in order to better remember not to do something, this punishment is very old and quite boring!
Hopefully we now have Python that can do the job for us.
In order to do so create a file `punishment.py` which will contain a function `do_punishment` having 3 arguments:
- `first_part`: which will be a string.
- `second_part`: which will be also a string.
- `nb_lines`: which will be a number.
Here is the prototype of the function:
```python
def do_punishment(first_part, second_part, nb_lines):
```
The function will concatenate `first_part` and `second_part`, adding a space in between them and a `.` at the end of `second_part`. It will repeat this process for `nb_lines` times.
The function will return a single string containing all the repeated sentences.
> In case `first_part` and `second_part` have empty spaces at the start or at the end those spaces should be trimmed (removed from the strings).
### Usage
Here is a possible `test.py` to test your functions:
```python
import punishment
print(punishment.do_punishment(' The first half ', ' and the second ', 4), end='')
print(punishment.do_punishment('Will not', 'show', 0), end='')
print(punishment.do_punishment('', '', 3), end='')
```
```bash
$ python test.py
The first half and the second.
The first half and the second.
The first half and the second.
The first half and the second.
.
.
.
```
### Hints
- Removing spaces at the start and end of a string is so common that almost all languages implement this feature. Here you can check for the `strip()` method.
- Instead of using loops you can try the `string multiplication` operator, which is a very nice feature of Python and will make your code more readable.
### References
* [strip](https://www.w3schools.com/python/ref_string_strip.asp)
* [string multiplication](https://www.geeksforgeeks.org/create-multiple-copies-of-a-string-in-python-by-using-multiplication-operator/)
Loading…
Cancel
Save