[go: up one dir, main page]

0% found this document useful (0 votes)
79 views2 pages

ReverseSort Engineering

This Python code defines functions to create a list of consecutive integers from 1 to n, calculate a list of operations to rearrange the list elements based on n and p, perform those operations on the original list, and print the result. It takes input for test cases n and p, creates an initial list, calculates the operation list, performs the operations, and prints the final rearranged list or "IMPOSSIBLE".

Uploaded by

Pawan Nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views2 pages

ReverseSort Engineering

This Python code defines functions to create a list of consecutive integers from 1 to n, calculate a list of operations to rearrange the list elements based on n and p, perform those operations on the original list, and print the result. It takes input for test cases n and p, creates an initial list, calculates the operation list, performs the operations, and prints the final rearranged list or "IMPOSSIBLE".

Uploaded by

Pawan Nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

def createL(n):

l = []

for i in range(1,n+1):

l.append(i)

return l

def operationL(n, p):

if p < n-1:

return []

l = []

t=0

c=1

for i in range(n-1, 0, -1):

c += 1

if t+c+i-1 >= p:

r = p-t-i+1

l.append(r)

for k in range(i-1):

l.append(1)

t=p

break

t += c

l.append(c)

if t<p:

return []

return l

def operate(l, opeL):

length = len(opeL)

for i in range(length):

t = len(l)-(i+2)

sp = t+opeL[i]
l = l[:t]+ list(reversed(l[t:sp])) + l[sp:]

return l

def solve():

inp = input().split()

n = int(inp[0])

p = int(inp[1])

l = createL(n)

opeL = operationL(n,p)

l = operate(l, opeL)

result = " "

if opeL:

for item in l:

result += str(item)+ " "

else:

result =" IMPOSSIBLE"

print("Case #"+str(i+1)+": "+ str(result))

for i in range(int(input())):

solve()

You might also like