diff --git a/src/fast_pow.py b/src/fast_pow.py index 1ba1dc1..fbca128 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,6 +1,25 @@ -def fastPow(number, power): - result = number - while power != 1: - result *= result - power = power // 2 +def fast_pow(number, power): + #проверка степени + if not isinstance(power, int): + raise TypeError("power must be an integer") + + # нули + if power == 0: + return 1 + if number == 0: + if power < 0: + raise ValueError("0 cannot be raised to a negative power") + return 0 + #отрицательная степень + if power < 0: + number = 1 / number + power = -power + + result = 1 + base = number + while power > 0: + if power % 2 == 1: + result*=base + base *= base + power >>= 1 return result diff --git a/src/luhn.py b/src/luhn.py index 11d993c..4e25264 100644 --- a/src/luhn.py +++ b/src/luhn.py @@ -1,5 +1,24 @@ -def luhnСheck(cardNumber): - digits = [int(d) for d in str(cardNumber) if d.isdigit()] +def luhn_check(card_number): + #проверка строки + if not isinstance(card_number, str): + raise TypeError("card_number должен быть строкой") + #проверка на корректный ввод + + digits = [] + #проверка на корректность ввода + for ch in card_number: + if ch.isdecimal(): + digits.append(int(ch)) + elif ch in " \t\r\n": + continue + else: + raise ValueError("Кривой ввод") + + digits = [int(d) for d in str(card_number) if d.isdigit()] + + if len(digits) < 2: + raise ValueError("Как минимум 2 цифры.") + control = digits.pop() parity = (len(digits))%2 total = 0 diff --git a/src/main1.py b/src/main1.py new file mode 100644 index 0000000..f8082d2 --- /dev/null +++ b/src/main1.py @@ -0,0 +1,51 @@ +def luhn_check(card_number): + #проверка строки + if not isinstance(card_number, str): + raise TypeError("card_number должен быть строкой") + #проверка на корректный ввод + + digits = [] + #проверка на корректность ввода + for ch in card_number: + if ch.isdecimal(): + digits.append(int(ch)) + elif ch in " \t\r\n": + continue + else: + raise ValueError("Кривой ввод") + + digits = [int(d) for d in str(card_number) if d.isdigit()] + + if len(digits) < 2: + raise ValueError("Как минимум 2 цифры.") + + control = digits.pop() + parity = (len(digits))%2 + total = 0 + for i in range(len(digits)): + if i % 2 == parity: + doubled = digits[i] * 2 + if doubled > 9: + doubled -= 9 + total += doubled + else: + total += digits[i] + return (total + control) % 10 == 0 + + + +def main() + print("Введите номер карты (или -1 для выхода):") + while True: + try: + line = input().strip() + except EOFError: + return 0 + except KeyboardInterrupt: + print("\nЗавершение по Ctrl-C.") + return 0 + if line == "-1": + return 0 + print("correct" if ok else "incorrect") +if __name__ == "__main__": + raise SystemExit(main()) \ No newline at end of file diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index 5e4aebd..5e00bfc 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -1,9 +1,22 @@ -from src.fast_pow import fastPow +from src.fast_pow import fast_pow +def test_base(): + assert fast_pow(2, 10) == 1024 + +def test_negative_base(): + assert fast_pow(-3, 3) == -27 + assert fast_pow(-3, 4) == 81 + +def test_zero_base(): + assert fast_pow(2, 0) == 1 + def test_two_power_two(): - assert fastPow(2, 2) == 4 + assert fast_pow(2, 2) == 4 +def test_negative_pow(): + assert abs(fast_pow(2, -3) - 0.125) < 1e-15 + assert abs(fast_pow(-2, -3) + 0.125) < 1e-15 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..a7397fa 100644 --- a/test/test_luhn.py +++ b/test/test_luhn.py @@ -1,9 +1,24 @@ -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_incorrect_input(): + try: + luhn_check("7") # слишком коротко + except ValueError: + pass + else: + raise AssertionError("Ошибка недостатка цифр.") + +def test_incorrect_input_2(): + try: + luhn_check("4111 11X") + except ValueError: + pass + else: + raise AssertionError("Value error.") 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") \ No newline at end of file