BASHA TECH

[HackerRank][Python] Arrays 본문

Activity/Coding Test

[HackerRank][Python] Arrays

Basha 2023. 4. 17. 14:28
728x90

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

Example

A = [1, 2, 3, 4]

Print 4 3 2 1. Each integer is separated by one space.

Input Format

The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers that describe array A's elements.

Constraints

Constraints

  • 1 <= N <= 1000
  • 1 <= A[i] <= 10000, where A[i] is the th square of i integer in the array.

Output Format

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

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input().strip())

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

    rev_arr = arr[::-1]
    print(*rev_arr)
 
  1. First, we take the input for the size of the array, n.
  2. Then, we take the input for the elements of the array as a space-separated list of integers, arr.
  3. We use slicing to reverse the list arr and assign it to a new list rev_arr.
  4. Finally, we print the elements of rev_arr as a single line of space-separated numbers using the print function and the unpacking operator *.
728x90
반응형

'Activity > Coding Test' 카테고리의 다른 글

[HackerRank][Python] Recursion 3  (0) 2023.04.17
[HackerRank][Python] Dictionaries and Maps  (0) 2023.04.17
[HackerRank][Python] Let's Review  (1) 2023.04.17
[HackerRank][Python] Loop  (0) 2023.04.13
[HackerRank][Python] Class vs Instance  (0) 2023.04.12
Comments