-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayrev.py
61 lines (48 loc) · 1.36 KB
/
arrayrev.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from array import array
def reverseArray(arr):
# Guard:
if len(arr) < 2:
return arr
if isinstance(arr, array):
revarr = array(arr.typecode)
revitr = False
chgtype = False
elif isinstance(arr, list):
revarr = []
revitr = True
chgtype = False
elif isinstance(arr, tuple):
revarr = []
revitr = False
chgtype = True
else:
raise TypeError('Expected array.array, list, or tuple')
if revitr:
for i in arr.__reversed__():
revarr.append(i)
else:
for i in range(len(arr) - 1, -1, -1):
revarr.append(arr[i])
if chgtype:
revarr = tuple(revarr)
return revarr
def main():
# Either array.array, list, or tuple:
# container_type = 'array'
# container_type = 'list'
container_type = 'tuple'
arr_size = int(input())
arr_input = map(int, input().split())
if container_type == 'array':
arr_type = 'i' # signed int
arr = array(arr_type, arr_input)
elif container_type == 'list':
arr = list(arr_input)
elif container_type == 'tuple':
arr = tuple(arr_input)
else:
raise TypeError('Expected container_type of array, list, or tuple')
revarr = reverseArray(arr)
print(' '.join(str(i) for i in revarr))
if __name__ == '__main__':
main()