I had a job interview today. During it I was asked to write down an algorithm that will reverse a list. First I offered the answer using the reversed() method:
I like this way:
def reverse(arr): for i in range(len(arr) / 2): arr[-i-1], arr[i] = arr[i], arr[-i-1]
Basically iterating through the first half of the array and swapping i and len(i)-i-1.
i
len(i)-i-1