Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Explained solution in Clear category for Replace Last by Selindian
def replace_last(line: list) -> list:
# your code here
if len(line) > 1: # If we have more than one element
array = [line[-1]] # Create new array with only the last element of the old array.
array.extend(line[0:len(line)-1]) # Extend (not append) tzhe new array with the element 0 - (last-1) of the old array.
return array # Return new array.
else:
return line # Else return original array.
if __name__ == '__main__':
print("Example:")
print(replace_last([2, 3, 4, 1]))
# These "asserts" are used for self-checking and not for an auto-testing
assert replace_last([2, 3, 4, 1]) == [1, 2, 3, 4]
assert replace_last([1, 2, 3, 4]) == [4, 1, 2, 3]
assert replace_last([1]) == [1]
assert replace_last([]) == []
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 10, 2022
Comments: