Chapter 12 Reversing a list of numbers

Question: Reverse a list of numbers.

Example

Input - [12, 2, 1, 3, 4, 7, 5, 10, 111, 22]

Output - [22, 111, 10, 5, 7, 4, 3, 1, 2, 12]

12.1 STEP 1. Convert into a small problem

We can rewrite the original problem as - “reverse a list of four numbers so that the numbers in the first and last places are exchanged, and the numbers in the second and third places are exchanged.”

Here is an example.

Input - [12, 2, 4, 5]

Output - [5, 12, 2, 4]

12.2 STEP 2. Solve on Paper

  1. Copy x[0] to tmp,
  2. Copy x[3] to x[0],
  3. Copy tmp to x[3],
  4. Copy x[1] to tmp,
  5. Copy x[2] to x[1],
  6. Copy tmp to x[2],

12.3 STEP 3. Write Python Code without Loop

x = [12, 2, 4, 5]

tmp = x[0]
x[0] = x[3]
x[3] = tmp

tmp = x[1]
x[1] = x[2]
x[2] = tmp

print(x)

12.4 STEP 4. Use Loop

To use loop, we expand the code for 10 numbers and identify the repetitive blocks.

x= [12, 2, 1, 3, 4, 7, 5, 10, 111, 22]

tmp = x[0]
x[0] = x[9]
x[9] = tmp

tmp = x[1]
x[1] = x[8]
x[8] = tmp

tmp = x[2]
x[2] = x[7]
x[7] = tmp

tmp = x[3]
x[3] = x[6]
x[6] = tmp

tmp = x[4]
x[4] = x[5]
x[5] = tmp

print(x)

You now see the pattern. Three lines repeat many times, and the above code can be condensed to -

x= [12, 2, 1, 3, 4, 7, 5, 10, 111, 22]

for i in [0, 1, 2, 3, 4]:
  tmp = x[i]
  x[i] = x[9-i]
  x[9-i] = tmp
  
print(x)

12.5 STEP 5. Use range

x= [12, 2, 1, 3, 4, 7, 5, 10, 111, 22]

for i in range(5):
  tmp = x[i]
  x[i] = x[9-i]
  x[9-i] = tmp
  
print(x)

12.6 STEP 6. Generalize

x= [12, 2, 1, 3, 4, 7, 5, 10, 111, 22]

L=len(x)

for i in range(L/2):
  tmp = x[i]
  x[i] = x[L-1-i]
  x[L-1-i] = tmp
  
print(x)

12.7 Exercises

Use the above method to solve the following problems.

  1. Reverse a list of numbers.

Example 1 - Input - [12, 2, 4, 5], Output - [ 5, 4, 2, 12] Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - [22,111,10,5,7,4,2,12]

  1. Find the smallest number from a list.

Example 1 - Input - [12, 2, 4, 5], Output - 2. Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - 2.

  1. Sort a list of numbers in place.

Example 1 - Input - [12, 2, 4, 5], Output - [2, 4, 5, 12] Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - [2,4,5,7,10,12,22,111]

  1. Rotate a list of numbers so that each number shifts by two places. That means the first number goes to the third place, the second to fourth and so on. Finally, the last two numbers go to the first two spots.

Example 1 - Input - [12, 2, 4, 5], Output - [4, 5, 12, 2] Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - [111, 22, 12, 2, 4, 7, 5, 10].

Corner cases - list with zero, one, two or three numbers.

  1. Find the second smallest number from a list.

Example 1 - Input - [12, 2, 4, 5], Output - 4. Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - 4.

  1. Modify a list so that the numbers in its odd position come first and the numbers in its even position come next. The count of positions start with 0 as the first place.

Example 1 - Input - [12, 2, 4, 5], Output - [2, 5, 12, 4] Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - [2,7,10,22,12,4,5,111]

  1. Based on a list of numbers, create a new list where each element is the sum of two consecutive numbers in the original list.

Example 1 - Input - [12, 2, 4, 5], Output - [14,6,9] Example 2 - Input - [12, 2, 4, 7, 5, 10, 111, 22], Output - [14,6,11,12,15,121,133]