Skip to content

Commit 79e8810

Browse files
feat(reverse-div-three): add reverse list values divisble by three
1 parent 3ada7c0 commit 79e8810

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Given an array of integers like the following:
3+
4+
[1, 6, 3, 8, 9]
5+
6+
Reverse the order of the integers in the array, but only those divisible by 3, while keeping all the other integers in the same positions as before. E.g.
7+
8+
[1, 9, 3, 8, 6]
9+
"""
10+
11+
12+
def reverseDivThree(array):
13+
left = 0
14+
right = len(array) - 1
15+
16+
while left < right:
17+
if array[left] % 3 != 0:
18+
left += 1
19+
elif array[right] % 3 != 0:
20+
right -= 1
21+
else:
22+
array[left], array[right] = array[right], array[left]
23+
left += 1
24+
right -= 1
25+
26+
return array
27+
28+
29+
print(reverseDivThree([1, 6, 3, 8, 9]))
30+
print(reverseDivThree([15, 3, 2, 0, 7, -3, 5]))
31+
print(reverseDivThree([-3, 3, 9, 12]))
32+
print(reverseDivThree([1, 2, 4, 5, 7]))

0 commit comments

Comments
 (0)