-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathik1_reverse_an_array.py
64 lines (51 loc) · 1.48 KB
/
ik1_reverse_an_array.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
62
63
64
"""
Reverse An Array
Reverse a given list of numbers.
Example One
input: [50, 35, 78, 66, 17]
Output: [17, 66, 78, 35, 50]
Example Two
input: [50, 40, 30, 20]
Output: [20, 30, 40, 50]
Notes
=====
Modify the input array in-place and return the modified array.
Constraints:
1 <= size of the input array <= 106
0 <= any element of the input array <= 106
"""
import unittest
def reverse_array(nums: list[int]) -> list[int]:
"""
Args:
nums(list_int32)
Returns:
list_int32
"""
# Option #1
# One line built in solution
# nums.reverse()
# Option #2
# Using slicing
# nums = nums[::-1]
# Option #3
# Using default brute force technique with for
# result = []
# for index in range(len(nums)-1, -1, -1):
# result.append(nums[index])
# nums = result
# Optoin #4
# Using for loop but with reversed index
# nums = [num for num in reversed(nums)]
nums = [reversed(nums)]
return nums
class TestReverseArray(unittest.TestCase):
"""Tesing Reverse array method implementation"""
def test_reverse_array(self):
"""Test reverse array method implementatio both positive and negative cases"""
self.assertEqual(reverse_array([50, 40, 30, 20]), [20, 30, 40, 50])
self.assertListEqual(reverse_array([50, 35, 78, 66, 17]), [17, 66, 78, 35, 50])
self.assertListEqual(reverse_array([11]), [11])
self.assertListEqual(reverse_array([]), [])
if __name__ == "__main__":
unittest.main()