Skip to content
Merged
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
8 changes: 6 additions & 2 deletions ciphers/des_ecb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 17 additions & 5 deletions sorts/recursive_insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading