diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b36579 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# VS Code +.vscode/ + +# Python +__pycache__/ diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..707e4bb --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +* @KubEF @Monrealle diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0ba69bf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Stanislav Klimovich + +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. diff --git a/src/.python-version b/src/.python-version new file mode 100644 index 0000000..d2e8921 --- /dev/null +++ b/src/.python-version @@ -0,0 +1 @@ +Python 3.12.11 diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..46688f1 --- /dev/null +++ b/src/README.md @@ -0,0 +1 @@ +# Это описание файла diff --git a/src/fast_pow.py b/src/fast_pow.py index 1ba1dc1..ba6d0c9 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,6 +1,14 @@ def fastPow(number, power): - result = number - while power != 1: - result *= result + if power < 0: + print("Степень не может быть отритцательной") + if power == 0: + return 1 + + result = 1 + base = number + while power > 0: + if power % 2 == 1: + result *= base + base *= base power = power // 2 return result diff --git a/src/luhn.py b/src/luhn.py index 11d993c..0c1d947 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -1,8 +1,21 @@ -def luhnСheck(cardNumber): - digits = [int(d) for d in str(cardNumber) if d.isdigit()] - control = digits.pop() - parity = (len(digits))%2 +def luhn_check(number_card): + """ + Проверяет ввод по алгоритму Луна + + Args: + numbee_card: Номер карты в виде строки или числа + Может содержать пробелы и другие символы + Return: + True: Если номер карты корректен + False: Если номер карты неправильный + """ + # Убираем все нецифровые символы + digits = [int(d) for d in str(number_card) if d.isdigit()] + + control = digits.pop() # Контрольная цифра + parity = (len(digits)) % 2 total = 0 + for i in range(len(digits)): if i % 2 == parity: doubled = digits[i] * 2 @@ -11,4 +24,38 @@ def luhnСheck(cardNumber): total += doubled else: total += digits[i] + return (total + control) % 10 == 0 + + +def main(): + """Консольная утилита на основе алгоритма Луна для проверки номеров карт""" + print("Введите номер карты или -1 для выхода:") + + while True: + try: + user_input = input("> ").strip() + + # Проверяем ввод на наличие -1 + if user_input == '-1': + print("Выход из программы") + break + + # Проверяем, что ввод не пустой + if not user_input: + print("Ошибка, введите номер карты") + continue + # Проверяем номер карты + if luhn_check(user_input): + print("Correct") + + else: + print("Incorrect") + + except KeyboardInterrupt: + print("\nВыход из программы") + break + + +if __name__ == "__main__": + main() diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index 5e4aebd..0089f85 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -1,9 +1,19 @@ from src.fast_pow import fastPow -def test_two_power_two(): +def test_unit_tests(): # unit тесты assert fastPow(2, 2) == 4 + assert fastPow(1, 10) == 1 + assert fastPow(3, 4) == 81 + assert fastPow(5, 3) == 125 -def test_negative(): +def test_extreme_cases(): # Крайние случаи assert fastPow(-1, 4) == 1 + assert fastPow(0, 10) == 0 + assert fastPow(67, 0) == 1 + assert fastPow(-2, 3) == -8 + +def test_property_based_tests(): # Property based тесты + assert fastPow(7, 4) == 7 ** 4 + assert fastPow(23, 0) == 23 ** 0 diff --git a/test/test_luhn.py b/test/test_luhn.py index 1c021a2..c605c10 100644 --- a/test/test_luhn.py +++ b/test/test_luhn.py @@ -1,9 +1,8 @@ -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")