From 2f3639c1795c0b309fdd21e4793d6b49f7e039c2 Mon Sep 17 00:00:00 2001 From: Romanovskiy Mikhail <78794445+fleron19@users.noreply.github.com> Date: Thu, 23 Oct 2025 13:51:01 +0300 Subject: [PATCH 1/8] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9645acb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Romanovskiy Mikhail + +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 5c4f099ddc5d62d2e64c43e26483eb7123385220 Mon Sep 17 00:00:00 2001 From: Romanovskiy Mikhail <78794445+fleron19@users.noreply.github.com> Date: Thu, 23 Oct 2025 13:52:25 +0300 Subject: [PATCH 2/8] Create .gitignore --- .gitignore | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7faf40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,207 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[codz] +*$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 + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.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 +#poetry.toml + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python. +# https://pdm-project.org/en/latest/usage/project/#working-with-version-control +#pdm.lock +#pdm.toml +.pdm-python +.pdm-build/ + +# pixi +# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control. +#pixi.lock +# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one +# in the .venv directory. It is recommended not to include this directory in version control. +.pixi + +# 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 +.envrc +.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/ + +# Abstra +# Abstra is an AI-powered process automation framework. +# Ignore directories containing user credentials, local state, and settings. +# Learn more at https://abstra.io/docs +.abstra/ + +# Visual Studio Code +# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore +# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore +# and can be added to the global gitignore or merged into this file. However, if you prefer, +# you could uncomment the following to ignore the entire vscode folder +# .vscode/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + +# Cursor +# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to +# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data +# refer to https://docs.cursor.com/context/ignore-files +.cursorignore +.cursorindexingignore + +# Marimo +marimo/_static/ +marimo/_lsp/ +__marimo__/ From 8eeb9b821e0c51fdb208b6eea8f7ca16d0414121 Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 13:54:30 +0300 Subject: [PATCH 3/8] Create .python-version --- .python-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .python-version diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..871f80a --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12.3 From 098e725428e592b4ae10785610534236ca717b4b Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 14:02:46 +0300 Subject: [PATCH 4/8] Fix styling --- src/fast_pow.py | 2 +- src/luhn.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 diff --git a/src/luhn.py b/src/luhn.py index 11d993c..da0f897 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -1,5 +1,5 @@ -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 total = 0 From a421bc0e2b9553b0b006e85e314c0c5abb6c412c Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 14:10:59 +0300 Subject: [PATCH 5/8] Add newline at end, change function names in tests --- src/luhn.py | 1 + test/test_fast_pow.py | 6 +++--- test/test_luhn.py | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/luhn.py b/src/luhn.py index da0f897..0ccd388 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -12,3 +12,4 @@ def luhn_check(card_number): else: total += digits[i] return (total + control) % 10 == 0 + 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 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 e99a83a5c007790d07a8aa4a88188330d2e4b006 Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 14:39:52 +0300 Subject: [PATCH 6/8] Add exceptions handling and tests for it, fix odd power bug --- src/fast_pow.py | 14 +++++++++++--- test/test_fast_pow.py | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/fast_pow.py b/src/fast_pow.py index 755e207..0a8daba 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,6 +1,14 @@ def fast_pow(number, power): - result = number - while power != 1: - result *= result + assert (isinstance(number, int) or isinstance(number, float) \ + and isinstance(power, int)), \ + "Error: number and power must be numbers" + + assert (power > 0 and isinstance(power, int)), "Error: power must be natural number" + + result = 1 + while power > 0: + if power % 2 == 1: + result *= number + number *= number power = power // 2 return result diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index aa91d54..d74b673 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -1,9 +1,26 @@ +import pytest + from src.fast_pow import fast_pow def test_two_power_two(): assert fast_pow(2, 2) == 4 +def test_five_power_five(): + assert fast_pow(5, 5) == 3125 def test_negative(): - assert fast_pow(-1, 4) == 1 + with pytest.raises(AssertionError): + fast_pow(5, -2) + +def test_not_natural(): + with pytest.raises(AssertionError): + fast_pow(6, 1.5) + +def test_zero(): + with pytest.raises(AssertionError): + fast_pow(3, 0) + +def test_wrong_types(): + with pytest.raises(AssertionError): + fast_pow("foo", "bar") From 1571d9ff5bdc62675bdd81a87900a6f137afd5d3 Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 15:00:40 +0300 Subject: [PATCH 7/8] Fix luhn alg. bug, add excpetions handling and some tests --- src/luhn.py | 10 ++++++---- test/test_luhn.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/luhn.py b/src/luhn.py index 0ccd388..88da1d6 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -1,7 +1,9 @@ def luhn_check(card_number): - digits = [int(d) for d in str(card_number) if d.isdigit()] - control = digits.pop() - parity = (len(digits))%2 + assert isinstance(card_number, str), "Error: card_number must be string" + card_number = card_number.replace(' ', '') + assert card_number.isdigit(), "Error: card_number must be string of digits" + digits = [int(d) for d in card_number] + parity = (len(digits)) % 2 total = 0 for i in range(len(digits)): if i % 2 == parity: @@ -11,5 +13,5 @@ def luhn_check(card_number): total += doubled else: total += digits[i] - return (total + control) % 10 == 0 + return total % 10 == 0 diff --git a/test/test_luhn.py b/test/test_luhn.py index d042e3d..cdd4687 100644 --- a/test/test_luhn.py +++ b/test/test_luhn.py @@ -1,9 +1,19 @@ +import pytest + from src.luhn import luhn_check 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") + +def test_wrong_types(): + with pytest.raises(AssertionError): + luhn_check(True) + +def test_wrong_string(): + with pytest.raises(AssertionError): + luhn_check("blabla") From 7bb5774695aaa6bc64d8b1dc9df40bb241abf2d3 Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Thu, 23 Oct 2025 15:07:26 +0300 Subject: [PATCH 8/8] Add card checkig utility --- src/luhn.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/luhn.py b/src/luhn.py index 88da1d6..334b8a8 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -15,3 +15,15 @@ def luhn_check(card_number): total += digits[i] return total % 10 == 0 +if __name__ == "__main__": + inp = input("Enter card number: ") + while inp != '-1': + try: + res = luhn_check(inp) + print("correct" if res else "incorrect") + except AssertionError as ex: + print(ex) + finally: + inp = input("Enter card number: ") + + print("Bye!")