물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Sort the odd

BEstyle 2022. 9. 27. 12:30

DESCRIPTION:

Task

You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.

Examples

[7, 1]  =>  [1, 7]
[5, 8, 6, 3, 4]  =>  [3, 8, 6, 5, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]  =>  [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]

def sort_array(source_array):
    print(source_array)
    placeList=[]
    alist=[]
    for i in range(len( source_array)):
        if source_array[i]%2==1:
            alist.append(source_array[i])
            placeList.append(i)
    print (alist)
    print(placeList)

    for j in range(len(alist)):
        for i in range(len(alist)-1):
            if alist[i]>alist[i+1]:
                temp=alist[i]
                alist[i]=alist[i+1]
                alist[i+1]=temp
                
    print(alist)
    print(placeList)

    for i in range(len(alist)):
        source_array[placeList[i]]=alist[i]
    return source_array

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Will you make it?  (0) 2022.09.27
You're a square!  (0) 2022.09.27
Multiples of 3 or 5  (0) 2022.09.27
Area or Perimeter  (1) 2022.09.26
Break camelCase  (1) 2022.09.26