-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort Except Zero.py
35 lines (24 loc) · 1.04 KB
/
Sort Except Zero.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
"""
https://py.checkio.org/ru/mission/sort-except-zero/
Sort the numbers in an array. But the position of zeros should not be changed.
"""
from typing import Iterable
def except_zero(items: list) -> Iterable:
zero_indexes = []
for index, x in enumerate(items):
if x == 0:
zero_indexes.append(index)
items_copy = sorted([x for x in items if x != 0])
for x in zero_indexes:
items_copy.insert(x, 0)
return items_copy
if __name__ == '__main__':
print("Example:")
print(list(except_zero([5, 3, 0, 0, 4, 1, 4, 0, 7])))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(except_zero([5, 3, 0, 0, 4, 1, 4, 0, 7])) == [1, 3, 0, 0, 4, 4, 5, 0, 7]
assert list(except_zero([0, 2, 3, 1, 0, 4, 5])) == [0, 1, 2, 3, 0, 4, 5]
assert list(except_zero([0, 0, 0, 1, 0])) == [0, 0, 0, 1, 0]
assert list(except_zero([4, 5, 3, 1, 1])) == [1, 1, 3, 4, 5]
assert list(except_zero([0, 0])) == [0, 0]
print("Coding complete? Click 'Check' to earn cool rewards!")