From 85821c7ccb908f1002374c86db7d89577c8739a3 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 13:54:39 +0300 Subject: [PATCH 01/14] Add LICENSE file --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..700ef42 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Nicholay Shestakov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 419248521afc488b018ff23a38487f5061030770 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 13:55:55 +0300 Subject: [PATCH 02/14] Add .gitignore --- .gitignore | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad4a1f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,176 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python +# Edit at https://www.toptal.com/developers/gitignore?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +# End of https://www.toptal.com/developers/gitignore/api/python From f7849024c7da9601b6e2623cda4a3d1a4615c1cb Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:03:31 +0300 Subject: [PATCH 03/14] Fixed CamelCase in name of function --- src/fast_pow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fast_pow.py b/src/fast_pow.py index 1ba1dc1..755e207 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,4 +1,4 @@ -def fastPow(number, power): +def fast_pow(number, power): result = number while power != 1: result *= result From 0e8fe78d19515731e2c731d6e8bacf24508ef3f4 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:03:56 +0300 Subject: [PATCH 04/14] Fixed CamelCase in names of function and variable --- src/luhn.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/luhn.py b/src/luhn.py index 11d993c..c5e85ca 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -1,7 +1,7 @@ -def luhnСheck(cardNumber): - digits = [int(d) for d in str(cardNumber) if d.isdigit()] +def luhn_check(card_number): + digits = [int(d) for d in str(card_number) if d.isdigit()] control = digits.pop() - parity = (len(digits))%2 + parity = (len(digits)) % 2 total = 0 for i in range(len(digits)): if i % 2 == parity: From cf9e944941b14499e68882673b9aa640580d1457 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:24:08 +0300 Subject: [PATCH 05/14] Add blank line in the end of file --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e8c1a14..9dec7a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,4 +17,4 @@ [tool.pytest.ini_options] testpaths = "test" -pythonpath = '.' \ No newline at end of file +pythonpath = '.' From 16c016929badf5c19ce17cbb14fb0f1591de3f51 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:24:35 +0300 Subject: [PATCH 06/14] Fixed CamelCase in name of function --- test/test_fast_pow.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index 5e4aebd..aa91d54 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -1,9 +1,9 @@ -from src.fast_pow import fastPow +from src.fast_pow import fast_pow def test_two_power_two(): - assert fastPow(2, 2) == 4 + assert fast_pow(2, 2) == 4 def test_negative(): - assert fastPow(-1, 4) == 1 + assert fast_pow(-1, 4) == 1 From 9ce0ccf77c413585ba9b3d8636db7aabc5b472fd Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:24:42 +0300 Subject: [PATCH 07/14] Fixed CamelCase in name of function --- test/test_luhn.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_luhn.py b/test/test_luhn.py index 1c021a2..d042e3d 100644 --- a/test/test_luhn.py +++ b/test/test_luhn.py @@ -1,9 +1,9 @@ -from src.luhn import luhnСheck +from src.luhn import luhn_check def test_good(): - assert luhnСheck("8571 2612 1234 5467") + assert luhn_check("8571 2612 1234 5467") def test_bad(): - assert not luhnСheck("4561 2612 1234 5463") \ No newline at end of file + assert not luhn_check("4561 2612 1234 5463") From 9c98b1eee3934a94a74bfb4045a158f612dc1154 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:42:24 +0300 Subject: [PATCH 08/14] Add exception for fast_pow If power not natural number raises exception. --- src/fast_pow.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fast_pow.py b/src/fast_pow.py index 755e207..ce1daea 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,4 +1,6 @@ def fast_pow(number, power): + if power != abs(round(power)) or power <= 0: + raise Exception("Not natural power.") result = number while power != 1: result *= result From 4a36f823a9740aa11faf2c27d8946dccba7bef70 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 14:45:01 +0300 Subject: [PATCH 09/14] Add test for fast_pow Add test, who tests not natural power. --- test/test_fast_pow.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index aa91d54..f9e6a39 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -7,3 +7,10 @@ def test_two_power_two(): def test_negative(): assert fast_pow(-1, 4) == 1 + + +def test_incorrect_power(): + try: + fast_pow(1, -1) + except Exception: + assert True From a410ff543d93e73f155594b019cb6960b8f722eb Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 15:01:29 +0300 Subject: [PATCH 10/14] Fixed bug in luhn check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Индекс начинается с нуля, поэтому его остаток от деления на два должен быть НЕ равен остатку от деления длины номера минус один на два. --- src/luhn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/luhn.py b/src/luhn.py index c5e85ca..c4419c9 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -4,7 +4,7 @@ def luhn_check(card_number): parity = (len(digits)) % 2 total = 0 for i in range(len(digits)): - if i % 2 == parity: + if i % 2 != parity: doubled = digits[i] * 2 if doubled > 9: doubled -= 9 From c77aa874ddd83c0708ce4169803965b11a108197 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 15:06:33 +0300 Subject: [PATCH 11/14] Update test for luhn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Сделаны новые рабочие тесты в связи с исправлением бага в самом алгоритме. --- test/test_luhn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_luhn.py b/test/test_luhn.py index d042e3d..5de3f5c 100644 --- a/test/test_luhn.py +++ b/test/test_luhn.py @@ -2,8 +2,8 @@ def test_good(): - assert luhn_check("8571 2612 1234 5467") + assert luhn_check("4561 2612 1234 5467") def test_bad(): - assert not luhn_check("4561 2612 1234 5463") + assert not luhn_check("4561 2612 1234 5464") From 90b5011d58822ce230fc3e8b1db6c2677f06423b Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 15:07:43 +0300 Subject: [PATCH 12/14] Add luhn utilite --- src/luhn_utilite.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/luhn_utilite.py diff --git a/src/luhn_utilite.py b/src/luhn_utilite.py new file mode 100644 index 0000000..62bd050 --- /dev/null +++ b/src/luhn_utilite.py @@ -0,0 +1,8 @@ +from luhn import luhn_check + +if __name__ == "__main__": + while (card := input("Input card number (-1 for exit): ")) != "-1": + if luhn_check(card): + print("correct") + else: + print("incorrect") From 0c7e51a1505fa818ff1f1dd858bc766d13ac4e2e Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 15:09:44 +0300 Subject: [PATCH 13/14] Delete luhn utilite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Удалена утилита отдельным файлом, потому что прочитал задание. --- src/luhn_utilite.py | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/luhn_utilite.py diff --git a/src/luhn_utilite.py b/src/luhn_utilite.py deleted file mode 100644 index 62bd050..0000000 --- a/src/luhn_utilite.py +++ /dev/null @@ -1,8 +0,0 @@ -from luhn import luhn_check - -if __name__ == "__main__": - while (card := input("Input card number (-1 for exit): ")) != "-1": - if luhn_check(card): - print("correct") - else: - print("incorrect") From 151a5a6b881240515992712e9e6e6bdae6949d86 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 23 Oct 2025 15:10:31 +0300 Subject: [PATCH 14/14] Add luhn utilite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit В файл luhn добавлена утилита для взаимодействия с функцией из консоли. --- src/luhn.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/luhn.py b/src/luhn.py index c4419c9..f0c2aff 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -12,3 +12,11 @@ def luhn_check(card_number): else: total += digits[i] return (total + control) % 10 == 0 + + +if __name__ == "__main__": + while (card := input("Input card number (-1 for exit): ")) != "-1": + if luhn_check(card): + print("correct") + else: + print("incorrect")