PPA-6
Question
Accept a sequence of comma-separated words as input. Reverse the sequence and print it as output.
Hint
Approach-1
Consider the following sequence of operations on a list L = [1, 2, 3, 4, 5]
. To reverse this list, you can pick up its last element and make it the first element of a new list.
Step-0
Step-1
Step-2
Step-3
Step-4
Step-5
You don’t necessarily have to remove the elements from the first list. But let us continue with the approach of removing an element from the first list and adding it to the second list. How will you remove an element from a list?
Approach-2
Consider the two lists:
We note the following:
x |
Index of x in P |
Index of x in Q |
---|---|---|
1 | 0 | 4 |
2 | 1 | 3 |
3 | 2 | 2 |
4 | 3 | 1 |
5 | 4 | 0 |
Do you see any pattern in the index of the element x
in P
and Q
? Is there a way you can use this pattern to reverse the list?
Solutions
Solutions galore! Do make it a point to go through every one of them.
Take a look at line-5. As the loop proceeds, we are swapping the first and last element, then the second and the penultimate element, and so on. Also take a look at the end-point of the range
function. This solution stands out from all the previous two in one detail: it reverses the list in-place, meaning, it doesn’t require a new list.
Here we are reversing the range. We start from \(n - 1\) and go all the way till \(0\). The step-size is \(-1\). Hence the range function looks like this: range(n - 1, -1, -1)
. The end-point is one less than where we want to stop, hence it is \(-1\) and not \(0\).
The best is reserved for the last. This uses some advanced slicing. We know that L[start:end]
gives the slice from L[start]
to L[end - 1]
. The third argument in the slice is the step size. So when we have L[::-1]
, it means start from the end of the list and go all the way till the beginning of the list in steps of \(-1\).
If you thought solution-5 was the best of the lot, here is something better. This reverses the list in-place.
We will not go into the details, but there is a construct called reversed
that helps us iterate over a reversed sequence.