Problem:

Partition

Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, inturn, less than or equal to each element of A[q+1..r]. It also computes the index q.

In the conquer processes, the two subarrays A[p..q-1] and A[q+1..r] are sorted by recursive calls of QuickSort(A, p, q-1) and QuickSort(A, q+1, r).

Your task is to read a sequence A and perform the Partition based on the following pseudocode:

Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4     do if A[j] n, the number of elements in the sequence A.

In the second line, Ai (i = 1,2,...,n), elements of the sequence are given separated by space characters.

Output

Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. The element which is selected as the pivot of the partition should be indicated by [  ].

Constraints

  
1 ≤ n ≤ 100,000

  
0 ≤ Ai ≤ 100,000

Sample Input 1

12
13 19 9 5 12 8 7 4 21 2 6 11

Sample Output 1

9 5 8 7 4 2 6 [11] 21 13 19 12



Sample input:
12
13 19 9 5 12 8 7 4 21 2 6 11


Sample output:
9 5 8 7 4 2 6 [11] 21 13 19 12

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
def partition(list_a, p, r):
    x = list_a[r]
    i = p-1
    for j in range(p,r):
        if list_a[j] <= x:
            i = i+1
            list_a[j], list_a[i] = list_a[i], list_a[j]
    list_a[i+1], list_a[r] = list_a[r], list_a[i+1]
    return i+1, list_a

ind,list_a = partition(list_a, 0, n-1)
for i in range(n):
    list_a[i] = str(list_a[i])
    if i == ind:
        list_a[i] = "[" + list_a[i] + "]"
print(" ".join(list_a))

Submission status: Accepted

Solution code:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  for j in range (p, r):
    if A[j]  <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  A[i+1], A[r] = A[r],A[i + 1]
  return i + 1

n = int(input())
a = list(map(int, input().split()))
i = partition(a, 0, n-1)
a[i] = "[" + str(a[i]) + "]"
print(" ".join(map(str, a)))

Submission status: Accepted

Solution code:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if (A[j] <= x):
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1
# r = (len(A)-1)
n = int(input())
A = list(map(int, input().split()))
i = partition(A, 0, n-1)
for k in range(n):
    if (k == (n-1)):
        if (k == i):
            print('[{}]'.format(A[i]), end = '')
        else:
            print('{0}'.format(A[k]), end = '')
        
    elif(k == i):
        print('[{}]'.format(A[i]), end = ' ')
    else:
        print('{}'.format(A[k]), end =' ')
print()

Submission status: Accepted

Solution code:
#! /usr/local/bin/python3
# coding: utf-8

def swap(A, i, j):
    tmp = A[i]
    A[i] = A[j]
    A[j] = tmp

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            swap(A, i, j)
    swap(A, i + 1, r)
    return i + 1

def bracketify(A, i, q):
    return "[" + str(A[i]) + "]" if i == q else str(A[i])

def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    q = partition(A, 0, n - 1)
    print(*[bracketify(A, i, q) for i in range(n)])

main()

Submission status: Accepted

Solution code:
def swap(a, b):
    return b, a

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i = i+1
            A[i], A[j] = swap(A[i], A[j])
    A[i+1], A[r] = swap(A[i+1], A[r])
    return A, i+1 

n = int(input())
A = [int(i) for i in input().split()]

B, i = partition(A, 0, n-1)
print(*B[0:i], sep=" ", end=" ")
print("[{0}]".format(B[i]), end=" ")
print(*B[i+1:], sep=" ")

Submission status: Accepted

Solution code:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    A[i+1] = "[{}]".format(A[i+1])


n = int(input())
A = list(map(int, input().split()))
partition(A, 0, n-1)
print(*A)

Submission status: Accepted

Solution code:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i += 1
            tmp = a[i]
            a[i] = a[j]
            a[j] = tmp
    tmp = a[r]
    a[r] = a[i+1]
    a[i+1] = tmp
    return i + 1


n = int(input())
a = [int(i) for i in input().split()]

q = partition(a, 0, n-1)

a = [str(i) for i in a]
a[q] = '[%s]' % a[q]
print(' '.join(a))

Submission status: Accepted

Solution code:
# Sort II - Partition
def partition(A,p,r):
    x = A[r]
    i = p - 1
    for j in range(p,r,1):
        if A[j] <= x:
            i += 1
            tmp = A[i]
            A[i] = A[j]
            A[j] = tmp
    tmp = A[i+1]
    A[i+1] = A[r]
    A[r] = tmp
    return i + 1
n = int(input())
A = list(map(int,input().split()))
q = partition(A,0,len(A)-1)
print(*['['+str(a)+']' if i==q else a for i,a in enumerate(A)])

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  
  for j in range(p, r):
    if A[j] <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  
  A[i + 1], A[r] = A[r], A[i + 1]
  
  return i + 1

n = int(input())

A = [int(num) for num in input().split()]

p = 0
r = n
q = partition(A, p, r)

A_str = [str(a) for a in A]

A_str[q] = "[" + A_str[q] + "]"

print(" ".join(A_str))

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  
  for j in range(p, r):
    if A[j] <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  
  A[i + 1], A[r] = A[r], A[i + 1]
  
  return i + 1
n = int(input())
A = [int(num) for num in input().split()]
p = 0
r = n
q = partition(A, p, r)
A_str = [str(a) for a in A]
A_str[q] = "[" + A_str[q] + "]"
print(" ".join(A_str))

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  
  for j in range(p, r):
    if A[j] <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  
  A[i + 1], A[r] = A[r], A[i + 1]
  
  return i + 1
A = [int(num) for num in input().split()][1:]
p = 0
r = len(A)-1
q = partition(A, p, r)
A_str = [str(a) for a in A]
A_str[q] = "[" + A_str[q] + "]"
print(" ".join(A_str))

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
  x = A[r]
  i = p-1
  for j in range(p, r-1):
    if A[j] <= i:
      i += 1
      A[i], A[j] = A[j], A[i]
  A[i + 1], A[r] = A[r], A[i + 1]
  return i + 1

n = int(input())
*A, = map(int, input().split())
r = A[-1]
p = min(A)

A[partition(A, p, r)] = "["+r+"]"
print(' '.join(A))

Submission status: Runtime Error

Solution code:
def Partition(A,p,r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if (A[j] <= x):
            i += 1
            A[i],A[j] = A[j],A[i]
    A[i+1],A[r] = A[r],A[i+1]
    return i+1

n = int(input())
st = input()
data = list(map(int,st.split()))

mid = Partition(data,0,len(data)-1)

for i in range(0,mid):
    print(data[i],end=" ")
print("[{0}] ".format(data[mid])))
for i in range(mid+1,len(data)-2):
    print(data[i],end=" ")
print(data[len(data)-1])

Submission status: Runtime Error

Solution code:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i = i + 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    print(*a)
    return i+1

n = int(input())
a = list(map(int, input().split()))
partition(a, 0, n)

Submission status: Runtime Error

Solution code:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i = i + 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    return i+1

n = int(input())
a = list(map(int, input().split()))
m = partition(a, 0, n-1)
a[m] = "[%s]"%(a[m])
print(" ".join(a))

Submission status: Runtime Error

Solution code:


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


n = int(input())
A = list(map(int, input().split()))

q =partition(A, 1, len(A) - 1)

print(*A[:q] + " [{0}] " + *A[q + 1:])

Submission status: Runtime Error

Solution code:
def partition(a, p, r):
    x = a[r]
    i = p-1

    for j in range(p, r):
        if a[j] <= x:
            i += 1
            tmp = a[i]
            a[i] = a[j]
            a[j] = tmp

    tmp = a[i+1]
    a[i+1] = a[r]
    a[r] = tmp
    return a, i+1


n = int(input())
A = list(map(int, input().split()))
ans, idx = partition(A, 0, n-1)
print(*ans[:idx], [ans[idx]], *ans[idx+1:])

Submission status: Runtime Error

Solution code:
def partition(p, r):
    x = a[r]
    i = p-1

    for j in range(p, r):
        if a[j] <= x:
            i += 1
            a[i], a[j] = a[j], a[i]

    a[i+1], a[r] = a[r], a[i+1]
    return i+1


n = int(input())
a = list(map(int, input().split()))
idx = partition(0, n-1)
print(*a[:idx], [a[idx]], *a[idx+1:])

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
    """
    ???????????????????????????????????????A[r]?????°?????????????????????????????????????????????A[r-1]??¨?´??????????????????§??¨???
    """
    x = A[r]p
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i+1]
    A[i+1] = A[r]
    A[r] = temp
    return i+1


if __name__ == '__main__':
    # ??????????????\???
    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]
    num_of_data = int(input())
    A = [int(x) for x in input().split(' ')]

    # ???????????????
    p = partition(A, 0, len(A)-1)  # ????????????????????????????????????len(A)??§?????????-1???????????????????????????

    # ???????????¨???
    left = A[:p]
    partition = A[p]
    right = A[p+1:]
    print('{0} [{1}] {2}'.format(' '.join(map(str, left)), partition, ' '.join(map(str, right))))

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] < x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


fin = open('sample.txt')
input = fin.readline

if __name__ == '__main__':
    N = int(input().strip())
    AA = [int(a) for a in input().strip().split()]
    i = partition(AA, 0, N - 1)
    _str = ' '.join([str(a) for a in AA[:i]]) + \
           ' [{}] '.format(AA[i]) + \
            ' '.join([str(a) for a in AA[i+1:]])
    print(_str)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-


def partition(A, p, r):
    x = A[r]
    i = p 
    for j in range(1, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i], A[r] = A[r], A[i]
    return i 


def main():
    n = int(input())
    A = [int(e) for e in input().split()]
    p = 0
    r = len(A) - 1
    idx = partition(A, p, r)
    A[idx] = "[%s]" % A[idx]
    return " ".join(A)

main()

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

n = int(input())
A = list(map(int, input().split()))
i = partition(A, 0, n)
A[i] = "["+str(A[i])+"]"
print(*A)

Submission status: Runtime Error

Solution code:
def partition(p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1
n = int(input())
A = [int(i) for i in input().split()]
q = partition(0, n - 1);
print(*A[:q], "[" + str(A[q]) + "]", end="")
if len(A[q + 1:]) > 0:
    print("", *A[q + 1:])
else:
    print("")

Submission status: Runtime Error

Solution code:
def partition(p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1
n = int(input())
A = [int(i) for i in input().split()]
q = partition(0, n - 1);
print(*A[:q], "[" + str(A[q]) + "]", end="")
if len(A[q + 1:]) > 0:
    print("", *A[q + 1:])

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
def partition(p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1

q = partition(0, n - 1);
print(*A[:q], "[" + str(A[q]) + "]", end="")
if len(A[q + 1:]) > 0:
    print("", *A[q + 1:])

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
def partition(p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1

q = partition(0, n - 1)
print(*A[:q], "[" + str(A[q]) + "]", end="")
if len(A[q + 1:]) > 0:
    print("", *A[q + 1:])

Submission status: Runtime Error

Solution code:
import sys

def swap(A, i, j):
    x = A[i]
    A[i] = A[j]
    A[j] = x

def partition(A, p, r, idx):
    x = A[r][idx]                # 1 x = A[r]
    i = p - 1                    # 2 i = p-1
    for j in range(p, r):        # 3 for j = p to r-1
        if A[j][idx] <= x:       # 4     if A[j] <= x
            i += 1               # 5        then i = i+1
            if(i != j):
                swap(A, i, j)    # 6        exchange A[i] and A[j] 
    i += 1 
    swap(A, i, r)                # 7 exchange A[i+1] and A[r]
    return i                     # 8 return i+1

def quickSort(A, p, r, idx):
    if p < r:
        q = partition(A, p, r, idx)
        quickSort(A, p, q - 1, idx)
        quickSort(A, q + 1, r, idx)

def main():
    """ ????????? """
    num = int(input().strip())
    istr = sys.stdin.read()
    cards = list(istr.splitlines())
    
    A = [[0,0] for i in range(num)]
    B = [[0,0] for i in range(num)]
    
    for i in range(num):
        A[i][0] = cards[i][0]
        A[i][1] = int(cards[i][2:])
        B[i][0] = cards[i][0]
        B[i][1] = int(cards[i][2:])

    quickSort(A, 0, num-1,1)
    B.sort(key = lambda d:d[1])
    res = "Stable"
    for i in range(num):
        if A[i][0] != B[i][0] or A[i][1] != B[i][1]:
            res = "Not stable"
            break

    print(res)
    for x in A:
        print(" ".join(map(str, x)))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        while A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

k = partition(A)
B = list(map(str, A))
B[k] = "[{}]".format(B[k])
print(" ".join(B))

Submission status: Runtime Error

Solution code:
def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        while A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

if __name__ == "__main__:
    input()
    A = list(map(int, input().strip().split()))
    k = partition(A)
    B = list(map(str, A))
    B[k] = "[{}]".format(B[k])
    print(" ".join(B))

Submission status: Runtime Error

Solution code:
def swap(A,i,j):
    A[i],A[j] = A[j],A[i]
    return A

def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        while A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

if __name__ == "__main__":
    input()
    A = list(map(int, input().strip().split()))
    k = partition(A)
    B = list(map(str, A))
    B[k] = "[{}]".format(B[k])
    print(" ".join(B))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))

x = a[-1]
i = -1

for j in range(len(a)-1):
    if a[j] <= x:
        i += 1
        a[i], a[j] = a[j], a[i]

a[i + 1], a[n] = "[" + str(a[n]) + "]", a[i + 1]

print(*a)

Submission status: Runtime Error

Solution code:
n=int(input())-1
A=list(map(int,input().split()))
i=0
for j in range(n):
 if A[j]<=A[-1]:A[i],A[j]=A[j],A[i];i+=1
A[i],A[n]=A[n],A[i]
print(" ".join(map(str,A[:k]))+" ["+str(A[i])+"] "+" ".join(map(str,A[k+1:])))

Submission status: Runtime Error

Solution code:
def s():
 n=int(input())-1
 A=list(map(int,input().split()))
 i=0
 for j in range(n):A[j]<=A[-1]and A[i],A[j]=A[j],A[i];i+=1
 A[i],A[n]=A[n],A[i]
 print(" ".join(map(str,A[:i]))+" ["+str(A[i])+"] "+" ".join(map(str,A[i+1:])))
if'__main__'==__name__:s()

Submission status: Runtime Error

Solution code:
MAX=100000
list=[]

def partition(p,r):
	t=0
	x=list[r]
	i=p-1
	j=p
	while(j<r):
		if(int(list[j])<=x):
			i+=1
			t=int(list[i])
			list[i]=list[j]
			list[j]=t
		j+=1

	t=int(list[i+1])
	list[i+1]=list[r]
	list[r]=t

	return i+1

n=int(input())
i=0
while(i<n):
	list.append(int(input()))
	i+=1

q=partition(0,n-1)

i=0
while(i<n):
	if(i==q):
		print("[",end="")
		print(str(list[i]),end="")
		print("]",end="")
	else:
		print(str(list[i])+" ",end="")
	i+=1

print()

Submission status: Runtime Error

Solution code:
list=[]

def partition(p,r):
	t=0
	x=list[r]
	i=p-1
	j=p
	while(j<r):
		if(int(list[j])<=x):
			i+=1
			t=int(list[i])
			list[i]=list[j]
			list[j]=t
		j+=1

	t=int(list[i+1])
	list[i+1]=list[r]
	list[r]=t

	return i+1

def main():
	n=int(input())
	i=0
	while(i<n):
		list.append(int(input()))
		i+=1

	q=partition(0,n-1)
	i=0
	while(i<n):
		if(i==q):
			print("[",end="")
			print(str(list[i]),end="")
			print("] ",end="")
		else:
			print(str(list[i])+" ",end="")
		i+=1
	print()
	return 0

if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
list=[]

def partition(p,r):
	t=0
	x=list[r]
	i=p-1
	j=p
	while(j<r):
		if(int(list[j])<=x):
			i+=1
			t=int(list[i])
			list[i]=list[j]
			list[j]=t
		j+=1

	t=int(list[i+1])
	list[i+1]=list[r]
	list[r]=t

	return i+1

def main():
	n=int(input())
	i=0
	while(i<n):
		list.append(int(input().split()))
		i+=1

	q=partition(0,n-1)
	i=0
	while(i<n):
		if(i==q):
			print("[",end="")
			print(str(list[i]),end="")
			print("] ",end="")
		else:
			print(str(list[i])+" ",end="")
		i+=1
	print()
	return 0

if __name__ == "__main__":
	main()

Submission status: WA: Presentation Error

Solution code:
def partition(p, r):
    x = A[r]
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


n = int(input())
A = [int(_) for _ in input().split()]
q = partition(0, n-1)
for i in range(n):
    if i:
        print(" ", end="")
    if i == q:
        print("[", end="")
    print(A[i], end="")
    if i == q:
        print("]", end="")
print("\n")

Submission status: WA: Presentation Error

Solution code:
def partition(p, r):
    x = A[r]
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


n = int(input())
A = [int(_) for _ in input().split()]
q = partition(0, n-1)
for i in range(n):
    if i:
        print(" ", end="")
    if i == q:
        print("[", end="")
    print(A[i], end="")
    if i == q:
        print("]", end="")

Submission status: WA: Presentation Error

Solution code:
def partition(n,a,p,r):
  x = a[r]
  i = p - 1
  
  for j in range(p,r):
    if x >= a[j]:
      i += 1
      a[i],a[j] = a[j],a[i]
      
  a[i+1],a[r] = a[r],a[i+1]
  
  for k in range(i+1):
    print(a[k],end=" ")
    
  print("[{}]".format(x),end=" ")
  
  for k in range(i+2,r+1):
    print(a[k],end=" ")
    
  print()

n = int(input())
a = list(map(int,input().split()))
partition(n,a,0,len(a)-1)

Submission status: WA: Presentation Error

Solution code:
n = int(input())
A = list(map(int, input().split()))

p, r = 0, len(A)-1
x = A[r]
i = p-1
for j in range(p, r):
    if A[j] <= x:
        i += 1
        A[i], A[j] = A[j], A[i]
A[i+1], A[r] = A[r], A[i+1]
k = i+1
for i in range(len(A)):
    if i == k:
        print("[{0}] ".format(A[i]), end='')
    else:
        print(A[i], '', end='')
print()

Submission status: WA: Presentation Error

Solution code:
n = int(input())
A = list(map(int, input().split()))

p, r = 0, len(A)-1
x = A[r]
i = p-1
for j in range(p, r):
    if A[j] <= x:
        i += 1
        A[i], A[j] = A[j], A[i]
A[i+1], A[r] = A[r], A[i+1]
k = i+1
for i in range(len(A)):
    if i == k:
        print("[{0}] ".format(A[i]), end='')
    else:
        print(A[i], '', end='')

Submission status: WA: Presentation Error

Solution code:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i = i+1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]

    return i+1

n = int(input())
A = list(map(int, input().split()))
#print(partition(A, 0, len(A)-1), A)
k = partition(A, 0, len(A)-1)
#print(k)
#print(A)
for i in range(len(A)):
    if i == k:
        print('['+str(A[i])+']', end=' ')
    else:
        print(A[i], end=' ')

Submission status: WA: Presentation Error

Solution code:
def partition(A,p,n):
    x=A[n-1]
    i=p-1
    for j in range(n-1):
        if A[j]<=x:
            i+=1
            A[i],A[j]=A[j],A[i]
    A[i+1],A[n-1]=A[n-1],A[i+1]
    
    for k in range(n):
        if k==i+1:
            print("[{}]".format(A[k]), end=" ")
        else:
            print(A[k],end=" ")

n=int(input())
A=list(map(int,input().split()))

partition(A,0,n)

Submission status: WA: Presentation Error

Solution code:
def partiton(A, p, r):
    x = A[-1]
    i = p - 1

    for j in range(p, r):
        if A[j] <= x:
            i = i + 1
            A[i], A[j] = A[j], A[i]

    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


_ = int(input())
li = [int(x) for x in input().split(' ')]
q = partiton(li, 0, len(li) - 1)

for i in range(0, len(li)):
    if i == q:
        print('[{0}]'.format(li[i]), end=' ')
    else:
        print(li[i], end=' ')

Submission status: WA: Presentation Error

Solution code:
def partiton(A, p, r):
    x = A[-1]
    i = p - 1

    for j in range(p, r):
        if A[j] <= x:
            i = i + 1
            A[i], A[j] = A[j], A[i]

    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


_ = int(input())
li = [int(x) for x in input().split(' ')]
q = partiton(li, 0, len(li) - 1)

for i in range(0, len(li)):
    if i == q:
        print('[{0}]'.format(li[i]), end=' ')
    else:
        print(li[i], end=' ')

print()

Submission status: WA: Presentation Error

Solution code:
def partiton(A, p, r):
    x = A[-1]
    i = p - 1

    for j in range(p, r):
        if A[j] <= x:
            i = i + 1
            A[i], A[j] = A[j], A[i]

    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


_ = int(input())
li = [int(x) for x in input().split(' ')]
q = partiton(li, 0, len(li) - 1)

for i in range(0, len(li)):
    if i == q:
        print('[{0}]'.format(li[i]), end='')
    else:
        print(li[i], end='')

print()

Submission status: WA: Presentation Error

Solution code:
n = int(input())
numbers = list(map(int, input().split()))

def main() :
    x = numbers[n - 1]
    i = 0
    for j in range(n) :
        if numbers[j] <= x :
            numbers[i], numbers[j] = numbers[j], numbers[i]
            i += 1

    [print(numbers[j] if j != i - 1 else '['+str(numbers[j])+']', end = ' ' if j != n - 1 else '') for j in range(n)]

if __name__ == '__main__':
    main()