-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathreverse_array.py
More file actions
36 lines (29 loc) · 604 Bytes
/
reverse_array.py
File metadata and controls
36 lines (29 loc) · 604 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Given an array of 'A' of 'N' integers, print each element in reverse order
as a single line of space-separated integers.
Example:
Input = 1 4 3 2
Output = 2 3 4 1
"""
# Reverse the array
# Method 1
# For loop
def reverseArray_1(a):
b = []
for i in a:
b.insert(0, i)
return b
# Method 2
# Copies the list prior to reversing it
def reverseArray_2(a):
return a[::-1]
# Method 3
# The fastest way to reverse a long list
def reverseArray_3(a):
a = a.reverse()
return a
a = [1, 4, 3, 2]
print (reverseArray_1(a))
print (reverseArray_2(a))
reverseArray_3(a)
print(a)