diff --git a/ciphers/des_ecb.py b/ciphers/des_ecb.py index 5a8a289adbe6..559aee9b764a 100644 --- a/ciphers/des_ecb.py +++ b/ciphers/des_ecb.py @@ -544,14 +544,18 @@ def decrypt(key: str, cipher_text: str) -> str: print(f"Generated key: {Des.generate_key()}") case "e": key = input("Enter the key: ").strip() - if len(key) != 16 and not all(char in "0123456789abcdef" for char in key): + if len(key) != 16 and not all( + char in "0123456789abcdef" for char in key + ): print("Invalid key. Please try again.") continue input_string = input("Enter the string to encrypt: ").strip() print(f"Encrypted string: {Des.encrypt(key, input_string)}") case "d": key = input("Enter the key: ").strip() - if len(key) != 16 and not all(char in "0123456789abcdef" for char in key): + if len(key) != 16 and not all( + char in "0123456789abcdef" for char in key + ): print("Invalid key. Please try again.") continue cipher_text = input("Enter the cipher text to decrypt: ").strip() diff --git a/sorts/recursive_insertion_sort.py b/sorts/recursive_insertion_sort.py index 30630ce84694..15bb702790e1 100644 --- a/sorts/recursive_insertion_sort.py +++ b/sorts/recursive_insertion_sort.py @@ -5,17 +5,17 @@ from __future__ import annotations from collections.abc import MutableSequence -from typing import Any, Protocol, TypeVar +from typing import Protocol, TypeVar class Comparable(Protocol): - def __lt__(self, other: Any, /) -> bool: ... + def __lt__(self, other: object, /) -> bool: ... T = TypeVar("T", bound=Comparable) -def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None: +def rec_insertion_sort[T: Comparable](collection: MutableSequence[T], n: int) -> None: """ Given a collection of comparable elements and its length, sorts the collection in place in ascending order. @@ -42,6 +42,17 @@ def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None: >>> rec_insertion_sort(col, len(col)) >>> col ['a', 'b', 'c', 'd', 'e'] + + >>> col = [2.5, -1, 0.0, 1] + >>> rec_insertion_sort(col, len(col)) + >>> col + [-1, 0.0, 1, 2.5] + + >>> col = [1, "a"] + >>> rec_insertion_sort(col, len(col)) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ # Checks if the entire collection has been sorted if len(collection) <= 1 or n <= 1: @@ -51,7 +62,7 @@ def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None: rec_insertion_sort(collection, n - 1) -def insert_next[T](collection: MutableSequence[T], index: int) -> None: +def insert_next[T: Comparable](collection: MutableSequence[T], index: int) -> None: """ Inserts the '(index-1)th' element into place @@ -71,7 +82,8 @@ def insert_next[T](collection: MutableSequence[T], index: int) -> None: [] """ # Checks order between adjacent elements - if index >= len(collection) or collection[index - 1] <= collection[index]: + # Only relies on ``__lt__`` so any Comparable works + if index >= len(collection) or not collection[index] < collection[index - 1]: return # Swaps adjacent elements since they are not in ascending order