Write a python program to Reverse an array

 Reverse an array

Given a list of integers, reverse the list.

 

Write a function solution that accepts a list. A function should reverse the original list.

 

Input
    5 

    9 0 2 1 8

       

    Where, 

  • The first line of input represents the size of a list. 
  • Second line represents list elements separated by single space. All the elements of the list should be on a single line.

 

Output
        8 1 2 0 9


Code:-


Method 1: -


def solution(l):

    return l[::-1]


l=[]

N=int(input())

for e in range(N):

    e=int(input())

    l.append(e)

x=len(l)

x=solution(l)

for i in x:

        print(i,end=" ")


Method 2:-


def solution(l):

    l=l[::-1]

    for i in l:

        print(i,end=" ")


l=[]

N=int(input())

n=0

for e in range(N):

    e=int(input())

    l.append(e)

    n+=1

x=len(l)

solution(l)


Post a Comment

0 Comments