Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
lambda solution in Clear category for Replace Last by Max0526
from typing import Iterable
replace_last = lambda line: [line[-1]] + line[:-1] if len(line) > 0 else []
'''
A rewrite of
def replace_last(line: list) -> Iterable:
if len(line) == 0:
return []
lst = []
lst.append(line[-1])
for i in range(0,len(line)-1):
lst.append(line[i])
return lst
'''
print("Example:")
print(list(replace_last([2, 3, 4, 1])))
assert list(replace_last([2, 3, 4, 1])) == [1, 2, 3, 4]
assert list(replace_last([1, 2, 3, 4])) == [4, 1, 2, 3]
assert list(replace_last([1])) == [1]
assert list(replace_last([])) == []
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 5, 2023