diff --git a/.github/workflows/lint_python.yml b/.github/workflows/lint_python.yml new file mode 100644 index 0000000..8d0ea3a --- /dev/null +++ b/.github/workflows/lint_python.yml @@ -0,0 +1,26 @@ +# TODO: Remove `|| true` from the end of tests to make them mandatory +name: lint_python +on: [pull_request, push] +jobs: + lint_python: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - run: pip install --upgrade pip wheel + - run: pip install bandit black codespell flake8 flake8-2020 flake8-bugbear + flake8-comprehensions isort mypy pytest pyupgrade safety + - run: bandit --recursive --skip B101 . || true # B101 is assert statements + - run: black --check . || true + - run: codespell || true # --ignore-words-list="" --skip="*.css,*.js,*.lock" + - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + - run: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88 + --show-source --statistics + - run: isort --check-only --profile black . || true + - run: pip install -r requirements.txt + - run: mkdir --parents --verbose .mypy_cache + - run: mypy --ignore-missing-imports --install-types --non-interactive . || true + - run: pytest . || true + - run: pytest --doctest-modules . || true + - run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true + - run: safety check diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a44d67b --- /dev/null +++ b/.gitignore @@ -0,0 +1,79 @@ +#File modified from TheAlgorithms/Python + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +.cache +.pytest_cache/ + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.DS_Store +.idea +.try +.vscode/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..344efe4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,36 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 + hooks: + - id: check-executables-have-shebangs + - id: check-yaml + - id: end-of-file-fixer + types: [python] + - id: trailing-whitespace + - id: requirements-txt-fixer + - repo: https://github.com/psf/black + rev: 21.9b0 + hooks: + - id: black + - repo: https://github.com/PyCQA/isort + rev: 5.9.3 + hooks: + - id: isort + args: + - --profile=black + - repo: https://github.com/PyCQA/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + args: + - --ignore=E203,W503 + - --max-complexity=25 + - --max-line-length=88 + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.910 + hooks: + - id: mypy + - repo: https://github.com/codespell-project/codespell + rev: v2.1.0 + hooks: + - id: codespell diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..17124eb --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: validate_manifest + name: validate pre-commit manifest + description: This validator validates a pre-commit hooks manifest file + entry: pre-commit-validate-manifest + language: python + files: ^(\.pre-commit-hooks\.yaml|hooks\.yaml)$ diff --git a/NumericalBaseConverter b/NumericalBaseConverter.py similarity index 100% rename from NumericalBaseConverter rename to NumericalBaseConverter.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e2fba5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Pillow diff --git a/sandpile.py b/sandpile.py new file mode 100644 index 0000000..9f4f328 --- /dev/null +++ b/sandpile.py @@ -0,0 +1,70 @@ +from random import randrange + +import pygame as pg + +SIZE = 100 +MAX_PILE = 4 +CASE = 5 +WIDTH, HEIGHT = SIZE * CASE, SIZE * CASE +R = 255 // MAX_PILE + 1 +COLORS = [(255 - i * R, 255 - i * R, 255 - i * R) for i in range(MAX_PILE)] + [ + (0, 0, 0) +] +MAX_ITERATIONS = 100000 +to_slide: list[tuple[int, int]] = [] + +pg.init() +SCREEN = pg.display.set_mode((WIDTH, HEIGHT), 0, 32) +pg.display.set_caption("Sandpile Simulation") +SCREEN.fill(COLORS[0]) +pg.display.update() + +grid = [] +for i in range(SIZE): + line = [] + for j in range(SIZE): + line.append(0) + grid.append(line) + + +def is_in_bound(x: int, y: int) -> bool: + if 0 > x or x >= SIZE or 0 > y or y >= SIZE: + return False + else: + return True + + +def dessiner(x: int, y: int) -> None: + pg.draw.rect(SCREEN, COLORS[grid[x][y]], (x * CASE, y * CASE, CASE, CASE)) + # pg.display.update() + + +for iteration in range(MAX_ITERATIONS): + longueur = len(to_slide) + if longueur == 0: + x = randrange(0, SIZE) + y = randrange(0, SIZE) + + grid[x][y] += 1 + if grid[x][y] == MAX_PILE: + grid[x][y] = 0 + to_slide.append((x - 1, y)) + to_slide.append((x + 1, y)) + to_slide.append((x, y + 1)) + to_slide.append((x, y - 1)) + dessiner(x, y) + else: + for i in range(longueur): + x, y = to_slide.pop(0) + if is_in_bound(x, y): + if grid[x][y] < MAX_PILE: + grid[x][y] += 1 + if grid[x][y] == MAX_PILE: + grid[x][y] = 0 + to_slide.append((x - 1, y)) + to_slide.append((x + 1, y)) + to_slide.append((x, y + 1)) + to_slide.append((x, y - 1)) + dessiner(x, y) + pg.display.update() + pg.display.set_caption(f"Itération n°{iteration}")