8000 add move zeros algorithm · dori-dev/python-algorithms@b538596 · GitHub
[go: up one dir, main page]

Skip to content

Commit b538596

Browse files
committed
add move zeros algorithm
1 parent cdb8697 commit b538596

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

arrays/move_zeros.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
move_zeros([True, 3, 5, 0, 3, False, None, 0, 'dori', 0]) =>
3+
[True, 3, 5, 3, False, None, 'dori', 0, 0, 0]
4+
"""
5+
6+
7+
def move_zeros(sequence: list) -> list:
8+
result = []
9+
zeros_count = 0
10+
for item in sequence:
11+
if item == 0 and not isinstance(item, bool):
12+
zeros_count += 1
13+
else:
14+
result.append(item)
15+
result.extend([0] * zeros_count)
16+
return result
17+
18+
19+
if __name__ == "__main__":
20+
print(move_zeros([True, 3, 5, 0, 3, False, None, 0, 'dori', 0]))

0 commit comments

Comments
 (0)
0