Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for When "k" is Enough! by wo.tomasz
from typing import Iterable
def remove_after_kth(items: list, k: int) -> Iterable:
ni = []
di = {}
for i in items:
if i in di.keys():
di[i] += 1
else:
di[i] = 0
if di[i] < k:
ni.append(i)
return ni
print("Example:")
print(list(remove_after_kth([42, 42, 42, 42, 42, 42, 42], 3)))
print(list(remove_after_kth([42, 42, 42, 99, 99, 17], 0)))
print(list(remove_after_kth([1, 1, 1, 2, 2, 2], 5)))
assert list(remove_after_kth([42, 42, 42, 42, 42, 42, 42], 3)) == [42, 42, 42]
assert list(remove_after_kth([42, 42, 42, 99, 99, 17], 0)) == []
assert list(remove_after_kth([1, 1, 1, 2, 2, 2], 5)) == [1, 1, 1, 2, 2, 2]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 25, 2023
Comments: