본문 바로가기
카테고리 없음

[Check-IO] sort-except-zero

by UltraLowTemp-Physics 2021. 4. 9.

1. Problem: py.checkio.org/en/mission/sort-except-zero/
2. My Solution 

def except_zero(items: list) -> Iterable:
    
    # duplication of the list of "items"
    copy_list = items
    # The list consisted of the index of zeros in items 
    index_of_zeros = []        
    # Find the index of zeros in "items"
    for n in range(len(items)):
        if items[n] == 0: index_of_zeros.append(n)
    # delete zeros in "items" based on the index list
    for n in index_of_zeros[::-1]:
        del copy_list[n]
    # sorting list in which there are no zeros 
    copy_list = sorted(copy_list)
    # Appending zeros to "copy_list" based on their original position
    for n in range(len(index_of_zeros)):
        copy_list.insert(index_of_zeros[n],0)
    
    return copy_list

댓글