Member-only story
One common interview question that the prospective Python developer will be asked to answer is how to reverse an array. There are several ways to reverse an array and in this post I intend to set out one solution that I have developed myself along with a few other solutions that I have researched.
Python does not have an array, but instead uses a list as an array. Lists are built into the Python programming language, whereas arrays aren’t. Arrays are not a built-in data structure, and therefore need to be imported via the array module in order to be used.
The first solution to reversing an array is one that I have developed myself to answer the question. The psuedocode for this solution is:-
- Define a list of numbers, lst.
- Define a function, reverse_array, which will take a list as its input.
- Within the function, define the blank list, reverse_lst.
- Within the function, create a for loop that will iterate through the length of the list minus 1, to -1, at steps of -1.
- Within the for loop, define the local variable, reverse, which is the iteration of the list.
- Within the for loop, append reverse to the list, reverse_lst.
- Once the for loop has completed its…