Posted by : at

Category : 30_Days_of_Code


In this challenge, you are given an array A of N integers, and asked to print A’s elements reverse order as a single line of space-separated numbers.

Sample Input:

4
1 4 3 2

Sample Output:

2 3 4 1

The first thing to do is the read an array of numbers from the user.

arr = list(map(int, input().strip().split())) 

In the code above:

  • input() take a user input with as many numbers as they want in the array.
  • input().strip() removes any blank spaces so 3 4 2 1 is made to be “3421”
  • input().strip().split() splits “3421” into “3” “4” “2” “1”.
  • map(int, __) in list(map(int, input().strip().split())) converts the strings into integers, but alone it returns a map object. It needs list() to convert it into an array of [3, 4, 2, 1].

The next step is to reverse the elements in the array. One way to go about this would be to use for loop. The range that a for loop loops over is range(start, stop, step). To loop backwards, you could start at the end, stop at the beginning and make the step -1 (decreasing). However, you must be careful with looping and indecing, as the index might be out of bounds.

In the example below, there is an array of 4 elements. The for loop loops from 0 up to the length of the array (4), by an increment of 1. This is perfect for indecing, as rev_num[0] = 1, rev_num[1] = 4, rev_num[2] = 3 and rev_num[3] = 2.

rev_num = [1, 4, 3, 2]
for i in range(len(rev_num)): #range(start, stop, step) from start UP TO stop
    print(i, end = ' ')
0 1 2 3

But what if you switched it to loop backwards? This loops FROM the length of the array (4) down to 0, by an increment of -1. Now if you use indecing, rev_num[4] is out of bounds, giving an error, and rev_num[3], rev_num[2], and rev_num[1] would only print 1 4 3.

rev_num = [1, 4, 3, 2]
for i in range(len(rev_num),0,-1): #range(start, stop, step) from start UP TO stop
    print(i, end = ' ')
4 3 2 1

Therefore, you actually want to use i-1

rev_num = [1, 4, 3, 2]
for i in range(len(rev_num),0,-1): #range(start, stop, step) from start UP TO stop
    print(i-1, end = ' ')
3 2 1 0

An easier way to accomplish this is to utilize the my_string[start:stop:by] notation from the last challenge. When start is left blank it defaults to 0, and when end is left blank it defaults to the length of the string/array. You do not have to worry about indecing here. In order to extract these numbers from the list, use the * before the array.

"hello"[::-1] 

rev_num = [1, 4, 3, 2]
rev_num[::-1]
*rev_num[::-1]
olleh

[2, 3, 4, 1]
2 3 4 1

Sample Solution 1:

#!/bin/python3

import math
import os
import random
import re
import sys
 
if __name__ == '__main__':
    n = int(input()) #number of integers in the array
    arr = list(map(int, input().rstrip().split())) #the array

    for i in range(n,0,-1):
        print(arr[i-1], end = " ")

Sample Solution 2:

#!/bin/python3

import math
import os
import random
import re
import sys
 
if __name__ == '__main__':
    n = int(input()) #not needed for this code
    arr = list(map(int, input().rstrip().split())) #the array

    print(*arr[::-1])
About

Data Scientist with B.S. Statistics (3.8 GPA, Cum Laude) from UCLA. Programming knowledge includes R, Python, SQL, Tableau, SAS and other data analysis tools.

Star