diff --git a/python/README.md b/python/README.md index c0caa7ad..0f7c8778 100644 --- a/python/README.md +++ b/python/README.md @@ -43,3 +43,4 @@ - [reverse-string](./reverse-string/README.md) - [roman-numerals](./roman-numerals/README.md) - [scrabble-score](./scrabble-score/README.md) +- [difference-of-squares](./difference-of-squares/README.md) diff --git a/python/difference-of-squares/.coverage b/python/difference-of-squares/.coverage new file mode 100644 index 00000000..b68e85ad --- /dev/null +++ b/python/difference-of-squares/.coverage @@ -0,0 +1 @@ +!coverage.py: This is a private format, don't read it directly!{"arcs":{"/home/vpayno/git_vpayno/exercism-workspace/python/difference-of-squares/test/__init__.py":[[0,0],[0,-1]],"/home/vpayno/git_vpayno/exercism-workspace/python/difference-of-squares/difference_of_squares.py":[[0,1],[1,4],[4,14],[14,24],[24,-1],[24,31],[4,11],[11,-4],[14,21],[21,-14],[31,-24]]}} \ No newline at end of file diff --git a/python/difference-of-squares/.coverage.xml b/python/difference-of-squares/.coverage.xml new file mode 100644 index 00000000..a255a8da --- /dev/null +++ b/python/difference-of-squares/.coverage.xml @@ -0,0 +1,34 @@ + + + + + + /home/vpayno/git_vpayno/exercism-workspace/python/difference-of-squares + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/difference-of-squares/.coveragerc b/python/difference-of-squares/.coveragerc new file mode 100644 index 00000000..883531db --- /dev/null +++ b/python/difference-of-squares/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit = __init__.py, *_test.py diff --git a/python/difference-of-squares/.pylintrc b/python/difference-of-squares/.pylintrc new file mode 120000 index 00000000..30b33b52 --- /dev/null +++ b/python/difference-of-squares/.pylintrc @@ -0,0 +1 @@ +../.pylintrc \ No newline at end of file diff --git a/python/difference-of-squares/README.md b/python/difference-of-squares/README.md index 90c41697..74a8a08d 100644 --- a/python/difference-of-squares/README.md +++ b/python/difference-of-squares/README.md @@ -40,4 +40,9 @@ Finding the best algorithm for the problem is a key skill in software engineerin ### Based on -Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file +Problem 6 at Project Euler - https://projecteuler.net/problem=6 + +### My Solution + +- [my solution](./difference_of_squares.py) +- [run-tests](./run-tests-python.txt) diff --git a/python/difference-of-squares/difference_of_squares.py b/python/difference-of-squares/difference_of_squares.py index 2b2ec944..d0113633 100644 --- a/python/difference-of-squares/difference_of_squares.py +++ b/python/difference-of-squares/difference_of_squares.py @@ -1,10 +1,31 @@ -def square_of_sum(number): - pass +"""Python Difference of Squares Exercism""" -def sum_of_squares(number): - pass +def square_of_sum(number: int) -> int: + """Calculates the square of sum. + :param number: int + :return: int + """ -def difference_of_squares(number): - pass + return (number * (number + 1) // 2) ** 2 + + +def sum_of_squares(number: int) -> int: + """Calculates the sum of squares. + + :param number: int + :return: int + """ + + return (number * (number + 1) * (2 * number + 1)) // 6 + + +def difference_of_squares(number: int) -> int: + """Calculates the difference of squares. + + :param number: int + :return: int + """ + + return square_of_sum(number) - sum_of_squares(number) diff --git a/python/difference-of-squares/difference_of_squares.py,cover b/python/difference-of-squares/difference_of_squares.py,cover new file mode 100644 index 00000000..e9afbd75 --- /dev/null +++ b/python/difference-of-squares/difference_of_squares.py,cover @@ -0,0 +1,31 @@ +> """Python Difference of Squares Exercism""" + + +> def square_of_sum(number: int) -> int: +> """Calculates the square of sum. + +> :param number: int +> :return: int +> """ + +> return (number * (number + 1) // 2) ** 2 + + +> def sum_of_squares(number: int) -> int: +> """Calculates the sum of squares. + +> :param number: int +> :return: int +> """ + +> return (number * (number + 1) * (2 * number + 1)) // 6 + + +> def difference_of_squares(number: int) -> int: +> """Calculates the difference of squares. + +> :param number: int +> :return: int +> """ + +> return square_of_sum(number) - sum_of_squares(number) diff --git a/python/difference-of-squares/pytest.ini b/python/difference-of-squares/pytest.ini new file mode 100644 index 00000000..8dc12a49 --- /dev/null +++ b/python/difference-of-squares/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +pythonpath = . +addopts = --doctest-modules +markers = + task: exercise task/step diff --git a/python/difference-of-squares/run-tests-python.txt b/python/difference-of-squares/run-tests-python.txt new file mode 100644 index 00000000..98cac9bd --- /dev/null +++ b/python/difference-of-squares/run-tests-python.txt @@ -0,0 +1,502 @@ +Running automated test file(s): + + +=============================================================================== + +Running: ../../.github/citools/python/python-lint-pylint + +Running Python Lint - PyLint + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: pylint --version + +pylint 3.0.3 +astroid 3.0.2 +Python 3.12.1 (main, Dec 28 2023, 08:22:05) [GCC 10.2.1 20210110] + +real 0m0.311s +user 0m0.183s +sys 0m0.089s + + + ============================================================================== + +Running: pylint ./src + + +-------------------------------------------------------------------- +Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00) + + +real 0m0.437s +user 0m0.360s +sys 0m0.065s + + + ============================================================================== + +Exit code: 0 + +real 0m2.092s +user 0m1.294s +sys 0m0.532s + +real 0m2.097s +user 0m1.296s +sys 0m0.535s + +=============================================================================== + +Running: ../../.github/citools/python/python-lint-ruff + +Running Python Lint - Ruff + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: ruff --version + +ruff 0.3.5 + +real 0m0.085s +user 0m0.032s +sys 0m0.056s + + + ============================================================================== + +Running: ruff check --ignore E501 ./src + +All checks passed! + +real 0m0.134s +user 0m0.050s +sys 0m0.088s + + + ============================================================================== + +Exit code: 0 + +real 0m1.126s +user 0m0.715s +sys 0m0.433s + +real 0m1.128s +user 0m0.716s +sys 0m0.433s + +=============================================================================== + +Running: ../../.github/citools/python/python-lint-pyright + +Running Python Lint - PyRight + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: pyright --version + +pyright 1.1.358 + +real 0m1.144s +user 0m0.522s +sys 0m0.129s + + + ============================================================================== + +Running: pyright --stats ./src + +Found 2 source files +pyright 1.1.358 +0 errors, 0 warnings, 0 informations +Completed in 0.574sec + +Analysis stats +Total files parsed and bound: 20 +Total files checked: 2 + +Timing stats +Find Source Files: 0sec +Read Source Files: 0sec +Tokenize: 0.05sec +Parse: 0.04sec +Resolve Imports: 0.05sec +Bind: 0.06sec +Check: 0.05sec +Detect Cycles: 0sec + +real 0m1.400s +user 0m1.272s +sys 0m0.192s + + + ============================================================================== + +Exit code: 0 + +real 0m3.525s +user 0m2.450s +sys 0m0.660s + +real 0m3.529s +user 0m2.452s +sys 0m0.661s + +=============================================================================== + +Running: ../../.github/citools/python/python-lint-bandit + +Running Python Lint - Bandit + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: bandit --version + +bandit 1.7.7 + python version = 3.12.1 (main, Dec 28 2023, 08:22:05) [GCC 10.2.1 20210110] + +real 0m0.298s +user 0m0.224s +sys 0m0.077s + + + ============================================================================== + +Running: bandit --verbose --recursive ./src + +[main] INFO profile include tests: None +[main] INFO profile exclude tests: None +[main] INFO cli include tests: None +[main] INFO cli exclude tests: None +[main] INFO running on Python 3.12.1 +Run started:2024-04-16 04:47:59.914569 +Files in scope (2): + ./src/difference_of_squares/__init__.py (score: {SEVERITY: 0, CONFIDENCE: 0}) + ./src/difference_of_squares/difference_of_squares.py (score: {SEVERITY: 0, CONFIDENCE: 0}) +Files excluded (0): + +Test results: + No issues identified. + +Code scanned: + Total lines of code: 19 + Total lines skipped (#nosec): 0 + Total potential issues skipped due to specifically being disabled (e.g., #nosec BXXX): 0 + +Run metrics: + Total issues (by severity): + Undefined: 0 + Low: 0 + Medium: 0 + High: 0 + Total issues (by confidence): + Undefined: 0 + Low: 0 + Medium: 0 + High: 0 +Files skipped (0): + +real 0m0.237s +user 0m0.167s +sys 0m0.072s + + + ============================================================================== + +Exit code: 0 + +real 0m1.468s +user 0m1.054s +sys 0m0.432s + +real 0m1.470s +user 0m1.055s +sys 0m0.432s + +=============================================================================== + +Running: ../../.github/citools/python/python-lint-refurb + +Running Python Lint - Refurb + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: refurb --version + +Refurb: v1.26.0 +Mypy: v1.9.0 + +real 0m0.225s +user 0m0.159s +sys 0m0.066s + + + ============================================================================== + +Running: refurb --quiet --ignore 183 ./src + + +real 0m1.119s +user 0m1.020s +sys 0m0.101s + + + ============================================================================== + +Exit code: 0 + +real 0m3.438s +user 0m2.889s +sys 0m0.569s + +real 0m3.441s +user 0m2.891s +sys 0m0.569s + +=============================================================================== + +Running: ../../.github/citools/python/python-test-with-doctests + +Running Python DocTests + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +PYTHONPATH="./src" + + ============================================================================== + +Running: python -m doctest -v ./src/difference_of_squares/difference_of_squares.py ./src/difference_of_squares/__init__.py + +4 items had no tests: + difference_of_squares + difference_of_squares.difference_of_squares + difference_of_squares.square_of_sum + difference_of_squares.sum_of_squares +0 tests in 4 items. +0 passed and 0 failed. +Test passed. +1 items had no tests: + __init__ +0 tests in 1 items. +0 passed and 0 failed. +Test passed. + +real 0m0.131s +user 0m0.073s +sys 0m0.052s + + + ============================================================================== + +Exit code: 0 + +real 0m1.108s +user 0m0.733s +sys 0m0.385s + +real 0m1.111s +user 0m0.734s +sys 0m0.386s + +=============================================================================== + +Running: ../../.github/citools/python/python-test-with-coverage + +Running Python Tests With Coverage + +Python versions: + + Python 3.12.1 + pyenv 2.3.36-21-g7e550e31 + pip 24.0 from /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pip (python 3.12) + PDM, version 2.13.2 + + + ============================================================================== + +Running: rm -rf ./coverage + + +real 0m0.001s +user 0m0.001s +sys 0m0.000s + + + ============================================================================== + +Running: pytest --version + +pytest 7.4.3 + +real 0m0.976s +user 0m0.925s +sys 0m0.840s + + + ============================================================================== + +PYTHONPATH="./src" + + ============================================================================== + +Running: pytest --verbose --cov=. --cov-branch --cov-report=term-missing --cov-report=xml:.coverage.xml -p no:randomly ./test + +============================= test session starts ============================== +platform linux -- Python 3.12.1, pytest-7.4.3, pluggy-1.3.0 -- /home/vpayno/.pyenv/versions/3.12.1/bin/python +cachedir: .pytest_cache +hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase(PosixPath('/home/vpayno/git_vpayno/exercism-workspace/python/difference-of-squares/.hypothesis/examples')) +rootdir: /home/vpayno/git_vpayno/exercism-workspace/python/difference-of-squares +configfile: pytest.ini +plugins: anyio-4.2.0, libtmux-0.25.0, pylama-8.4.1, cov-4.1.0, datafiles-3.0.0, docker-2.0.1, subprocess-1.5.0, typeguard-4.1.5, hypothesis-6.98.17, snapshot-0.9.0 +collecting ... collected 9 items + +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_difference_of_squares_1 PASSED [ 11%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_difference_of_squares_100 PASSED [ 22%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_difference_of_squares_5 PASSED [ 33%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_square_of_sum_1 PASSED [ 44%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_square_of_sum_100 PASSED [ 55%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_square_of_sum_5 PASSED [ 66%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_sum_of_squares_1 PASSED [ 77%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_sum_of_squares_100 PASSED [ 88%] +test/difference_of_squares_test.py::DifferenceOfSquaresTest::test_sum_of_squares_5 PASSED [100%] + +=============================== warnings summary =============================== +../../../../.pyenv/versions/3.12.1/lib/python3.12/site-packages/coverage/pytracer.py:164 + /home/vpayno/.pyenv/versions/3.12.1/lib/python3.12/site-packages/coverage/pytracer.py:164: DeprecationWarning: currentThread() is deprecated, use current_thread() instead + self.thread = self.threading.currentThread() + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html + +---------- coverage: platform linux, python 3.12.1-final-0 ----------- +Name Stmts Miss Branch BrPart Cover Missing +---------------------------------------------------------------------- +difference_of_squares.py 7 1 0 0 86% +test/__init__.py 0 0 0 0 100% +---------------------------------------------------------------------- +TOTAL 7 1 0 0 86% +Coverage XML written to file .coverage.xml + +========================= 9 passed, 1 warning in 1.10s ========================= + +real 0m2.020s +user 0m1.835s +sys 0m0.141s + + + ============================================================================== + +Running: coverage report --show-missing + +Name Stmts Miss Branch BrPart Cover Missing +---------------------------------------------------------------------- +difference_of_squares.py 7 1 0 0 86% +test/__init__.py 0 0 0 0 100% +---------------------------------------------------------------------- +TOTAL 7 1 0 0 86% + +real 0m0.155s +user 0m0.089s +sys 0m0.067s + + + ============================================================================== + +Running: coverage annotate + + +real 0m0.147s +user 0m0.078s +sys 0m0.072s + + + ============================================================================== + +Line Coverage: 85.7% +Branch Coverage: 100.0% + + ============================================================================== + +Exit code: 0 + +real 0m4.272s +user 0m3.593s +sys 0m1.434s + +real 0m4.275s +user 0m3.594s +sys 0m1.435s + +=============================================================================== + +tail -n 10000 ./*,cover | grep -E -C 3 '^> def |^! ' + +> def square_of_sum(number: int) -> int: +> """Calculates the square of sum. +-- + +> def sum_of_squares(number: int) -> int: +> """Calculates the sum of squares. +-- + +> def difference_of_squares(number: int) -> int: +> """Calculates the difference of squares. + +=============================================================================== + +Running: misspell ./src/difference_of_squares/difference_of_squares.py ./src/difference_of_squares/__init__.py + +real 0m0.024s +user 0m0.028s +sys 0m0.014s + +=============================================================================== + diff --git a/python/difference-of-squares/src/difference_of_squares/__init__.py b/python/difference-of-squares/src/difference_of_squares/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/difference-of-squares/src/difference_of_squares/difference_of_squares.py b/python/difference-of-squares/src/difference_of_squares/difference_of_squares.py new file mode 120000 index 00000000..99087449 --- /dev/null +++ b/python/difference-of-squares/src/difference_of_squares/difference_of_squares.py @@ -0,0 +1 @@ +../../difference_of_squares.py \ No newline at end of file diff --git a/python/difference-of-squares/test/__init__.py b/python/difference-of-squares/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/difference-of-squares/test/__init__.py,cover b/python/difference-of-squares/test/__init__.py,cover new file mode 100644 index 00000000..e69de29b diff --git a/python/difference-of-squares/test/difference_of_squares_test.py b/python/difference-of-squares/test/difference_of_squares_test.py new file mode 120000 index 00000000..31321bc7 --- /dev/null +++ b/python/difference-of-squares/test/difference_of_squares_test.py @@ -0,0 +1 @@ +../difference_of_squares_test.py \ No newline at end of file