주어진 배열과 배열의 범위 [startval, endval].Array는 세 부분으로 나뉩니다.
- startval보다 작은 모든 요소가 먼저 옵니다.
- startval에서 endval까지의 모든 요소가 다음에 옵니다.
- endval보다 큰 모든 요소는 끝에 나타납니다.
예시
Input: A = [1, 14, 51, 12, 4, 2, 54, 20, 87, 98, 3, 1, 32] startval = 14, endval = 54 Output: A = [1, 12, 4, 2, 3, 1, 14, 51, 20, 32,54, 87, 98]
알고리즘
Step1: First the list is divided into three parts, first part will contain elements less than startval, second part will contain elements between startval and endval and third part will contain elements greater than endval. Step2: Concatenate all three parts together.
예시 코드
def partition_array(input, lowVal, highVal): # Separate input list in three parts my_first = [ num for num in input if num<lowVal ] my_second = [ num for num in input if (num>=lowVal and num<=highVal) ] my_third = [ num for num in input if num>highVal ] # concatenate all three parts print(my_first + my_second + my_third) # Driver program if __name__ == "__main__": my_input = [10, 140, 50, 200, 40, 20, 540, 200, 870, 980, 30, 10, 320] my_lowVal = 140 my_highVal = 200 partition_array(my_input, my_lowVal, my_highVal)
출력
[10, 50, 40, 20, 30, 10, 140, 200, 200, 540, 870, 980, 320]