Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Swap Nodes by Elena_Korljukova
def swap_nodes(a):
return [(a[i + 1] if i%2 == 0 else a[i - 1]) for i in range(len(a) - len(a)%2)] + ([a[-1]] if len(a)%2 == 1 else [])
if __name__ == '__main__':
print("Example:")
print(list(swap_nodes([1, 2, 3, 4])))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(swap_nodes([1, 2, 3, 4])) == [2, 1, 4, 3]
assert list(swap_nodes([5, 5, 5, 5])) == [5, 5, 5, 5]
assert list(swap_nodes([1, 2, 3])) == [2, 1, 3]
assert list(swap_nodes([3])) == [3]
assert list(swap_nodes(["hello", "world"])) == ["world", "hello"]
print("Coding complete? Click 'Check' to earn cool rewards!")
Aug. 24, 2020