Skip to content

Commit 81daa51

Browse files
author
abregman
committed
Add an exercise
And also .github folder and tox.ini for setting up the CI
1 parent 05f672d commit 81daa51

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

.github/workflows/pipeline.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: "Cibyl CI"
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: "Setup Python"
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: 3.9
18+
- name: "Install dependencies"
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install tox
22+
- name: "Run tox"
23+
run: tox

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | [Solution](solutions/hello_world/hello_world.md) | |
88
| 22 times | [Exercise](exercises/hello_world/22_times.md) | [Solution](solutions/hello_world/22_times.md) | |
99

10+
## Loops
11+
12+
|Name|Objective & Instructions|Solution|Comments|
13+
|--------|--------|------|----|
14+
| Refactor-1 | [Exercise](exercises/loops/refactor_1.md) | [Solution](solutions/loops/refactor_1.py) | |
15+
1016
## Functions
1117

1218
|Name|Objective & Instructions|Solution|Comments|

exercises/loops/refactor_1.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Refactor 01
2+
3+
Refactor the following code
4+
5+
```python
6+
from collections import namedtuple
7+
8+
Mushroom = collections.nametuple('Mushroom', ['name', 'poisonous'])
9+
10+
mushrooms = [Mushroom('Portabello', false), Mushroom('Oyster', false),
11+
Mushroom('Death Cap', true)]
12+
i = 0
13+
14+
for mushroom in mushrooms:
15+
i += 1
16+
name = mushroom.name
17+
print('%d:"%s"'%(i, name))
18+
```

solutions/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2022 Arie Bregman
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.

solutions/loops/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2022 Arie Bregman
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.

solutions/loops/refactor_1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
from collections import namedtuple
4+
import timeit
5+
6+
def before_refactor():
7+
8+
Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
9+
10+
mushrooms = [Mushroom('Portabello', False), Mushroom('Oyster', False),
11+
Mushroom('Death Cap', True)]
12+
i = 0
13+
14+
for mushroom in mushrooms:
15+
i += 1
16+
name = mushroom.name
17+
print('%d:"%s"'%(i, name))
18+
19+
def after_refactor(): # <- Solution is here! :)
20+
21+
Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
22+
23+
mushrooms = [Mushroom('Portabello', False), Mushroom('Oyster', False),
24+
Mushroom('Death Cap', True)]
25+
26+
for i, mushroom in enumerate(mushrooms):
27+
i += 1
28+
name = mushroom.name
29+
print('%d:"%s"'%(i, name))
30+
31+
print(timeit.timeit("before_refactor()", setup="from __main__ import before_refactor", number=100))
32+
print(timeit.timeit("after_refactor()", setup="from __main__ import after_refactor", number=100))

tox.ini

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[tox]
2+
minversion = 3.8.0
3+
envlist = linters,unit,coverage
4+
skipsdist = True
5+
ignore_basepython_conflict = True
6+
skip_missing_interpreters = False
7+
requires =
8+
tox-extra; python_version >= '3.8'
9+
10+
[testenv]
11+
usedevelop = True
12+
install_command = pip install -U {opts} {packages}
13+
14+
[testenv:linters]
15+
deps =
16+
pre-commit>=1.21.0
17+
pylint>=2.12.0
18+
-r {toxinidir}/requirements.txt
19+
-r {toxinidir}/test-requirements.txt
20+
commands =
21+
python -m pre_commit run -a
22+
23+
[testenv:unit]
24+
deps =
25+
-r {toxinidir}/requirements.txt
26+
-r {toxinidir}/test-requirements.txt
27+
commands =
28+
python -m unittest
29+
30+
[gh-actions]
31+
python =
32+
3.9: py39
33+
34+
[testenv:coverage]
35+
deps =
36+
-r {toxinidir}/requirements.txt
37+
-r {toxinidir}/test-requirements.txt
38+
commands =
39+
coverage run -m unittest discover
40+
coverage report --fail-under=90

0 commit comments

Comments
 (0)