Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# VS Code
.vscode/

# Python
__pycache__/
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @KubEF @Monrealle
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions src/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Python 3.12.11
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Это описание файла
14 changes: 11 additions & 3 deletions src/fast_pow.py
Original file line number Diff line number Diff line change
@@ -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
55 changes: 51 additions & 4 deletions src/luhn.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
14 changes: 12 additions & 2 deletions test/test_fast_pow.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions test/test_luhn.py
Original file line number Diff line number Diff line change
@@ -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")
assert not luhn_check("4561 2612 1234 5463")