This commit is contained in:
MaximSmolskiy 2024-04-02 22:40:42 +03:00
parent 487fa33f32
commit 331c027fad

View File

@ -3,8 +3,6 @@ Bead sort only works for sequences of non-negative integers.
https://en.wikipedia.org/wiki/Bead_sort https://en.wikipedia.org/wiki/Bead_sort
""" """
from itertools import pairwise
def bead_sort(sequence: list) -> list: def bead_sort(sequence: list) -> list:
""" """
@ -33,7 +31,7 @@ def bead_sort(sequence: list) -> list:
if any(not isinstance(x, int) or x < 0 for x in sequence): if any(not isinstance(x, int) or x < 0 for x in sequence):
raise TypeError("Sequence must be list of non-negative integers") raise TypeError("Sequence must be list of non-negative integers")
for _ in range(len(sequence)): for _ in range(len(sequence)):
for i, (rod_upper, rod_lower) in enumerate(pairwise(sequence)): for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): # noqa: RUF007
if rod_upper > rod_lower: if rod_upper > rod_lower:
sequence[i] -= rod_upper - rod_lower sequence[i] -= rod_upper - rod_lower
sequence[i + 1] += rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower