5.1 The Dutch National Flag Problem Brute Solution

#1

I implemented a brute force solution that follows the one mentioned in the text, but during the EPI Judge, I get a failed test:

“Test FAILED ( 2/204) Not partitioned after 2th element
Arguments
A: [1, 1, 0, 2]
pivot_idx: 1
Failure info explanation: Basic test, one swap”

However, my final result is a correctly rearranged array. Would anyone know why the test fail despite the final array being correct? Code is followed below:

def dutch_flag_partition(pivot_index, A):
# TODO - you fill in here.
L = []
E = []
G = []

pivot = A[pivot_index]

for i in range(len(A)):
    if A[i] < pivot:
        L.append(A[i])
    elif A[i] == pivot:
        E.append(A[i])
    elif A[i] > pivot:
        G.append(A[i])
    else: return -1

A = (L + E + G)

return A
0 Likes

#2

Please post your result.

0 Likes