Objective
Today, we’re learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given an array, A, of N integers, print A’s elements in reverse order as a single line of space-separated numbers.

Input Format

The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers describing array ‘s elements.

Constraints

  • 1<= N<=1000
  • 1<= Ai <=10000, where Ais ith the integer in the array.

Output Format

Print the elements of array A in reverse order as a single line of space-separated numbers.

Sample Input

4

1 4 3 2

Sample Output

2 3 4 1

Solution:

Writing comment is a great habit for programmers. Not only does it help other users in understanding what the code means but also aids the programmer in organizing his/her code in logically sequential order.

Let us try to understand the solution & Understand by comments.

#reading input from user
n = int(input().strip())
 
#a single line for loop
#the loop means that split string 'n' based on the spaces between
#the string 'n' and convert each string to integer and append it
#to list 'arr'
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
 
#reverse the list 'arr' by using reversed function
a= list(reversed(arr))
 
#for loop for printing reverse of input
for x in a:
    print(x, end=' ')

Arrays HackerRank 30 Days of Code Challenge Day 7

Tags : Code Hacker Rank , rank , code , coding , python, programing , challenge , 30 day challenge , solution

Leave a Reply

Your email address will not be published.