Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The four steps to happiness solution in Clear category for Sort Except Zero by RJPython
from typing import Iterable
def except_zero(items: list) -> Iterable:
## 1. create list of indices of all zeros
null_ind = [i for i in range(len(items)) if items[i] == 0]
## 2. remove all zeros from items
while 0 in items:
items.remove(0)
## 3. sort remaining items
items.sort()
## 4. reinsert zeros by using 1.
for i in null_ind:
items.insert(i, 0)
return items
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!")
July 26, 2020