Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Remove All After by mennadiego
from collections.abc import Iterable
def remove_all_after(items: list[int], border: int) -> Iterable[int]:
"""
remove all the elements that go after border
We have two edge cases here:
if a cutting element cannot be found, then the sequence shouldn't be changed;
if the sequence is empty, then it should remains empty.
:param items: list[int] - input list
:param border: int - border after removing
:return: Iterable[int] - output list
"""
try:
el_index = items.index(border)
print(el_index)
return items[:el_index+1]
except:
return items
if not items:
return []
print("Example:")
print(list(remove_all_after([1, 2, 3, 4, 5], 3)))
# These "asserts" are used for self-checking
assert list(remove_all_after([1, 2, 3, 4, 5], 3)) == [1, 2, 3]
assert list(remove_all_after([1, 1, 2, 2, 3, 3], 2)) == [1, 1, 2]
assert list(remove_all_after([1, 1, 2, 4, 2, 3, 4], 2)) == [1, 1, 2]
assert list(remove_all_after([1, 1, 5, 6, 7], 2)) == [1, 1, 5, 6, 7]
assert list(remove_all_after([], 0)) == []
assert list(remove_all_after([7, 7, 7, 7, 7, 7, 7, 7, 7], 7)) == [7]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 2, 2023
Comments: