Arrays - DS | HackerRank Solutions
An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index, (where ), that can be referenced as or .
Reverse an array of integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Example
Return .
Function Description
Complete the function reverseArray in the editor below.
reverseArray has the following parameter(s):
- int A[n]: the array to reverse
Returns
- int[n]: the reversed array
Input Format
The first line contains an integer, , the number of integers in .
The second line contains space-separated integers that make up .
Constraints
Sample Input 1
4
1 4 3 2
Sample Output 1
2 3 4 1
## Solutions:1--
import mathimport osimport randomimport reimport sys
def reverseArray(a): return a[::-1]if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr_count = int(input().strip())
arr = list(map(int, input().rstrip().split()))
res = reverseArray(arr)
fptr.write(' '.join(map(str, res))) fptr.write('\n')
fptr.close()
## Solution-2
import mathimport osimport randomimport reimport sys
def reverseArray(a): a.reverse() return a
"""because a.reverse() just reverses the list and returns Noneand when you do return a.reverse() you are basically returning None
you should instead do this a.reverse() return a"""
if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr_count = int(input().strip())
arr = list(map(int, input().rstrip().split()))
res = reverseArray(arr)
fptr.write(' '.join(map(str, res))) fptr.write('\n')
fptr.close()
Comments
Post a Comment