Sort Pairs - Inside & Outside Parenthesis
Coding Problem Keys
Sort Pairs - Inside & Outside Parenthesis
Problem Statement
The program must accept N pairs of integers as the input. One of the two integers in each pair is enclosed within a pair of parenthesis. The program must sort the integers that are present inside the parentheses in ascending order and sort the integers that are present outside the parentheses in descending order. Finally, the program must print N revised pairs as the output.
Boundary Condition(s)
Input Format
Output Format
Example Input/Output 1
Input
Output
Explanation
Example Input/Output 2
Input
Output
Note: Max Execution Time Limit: 50 millisecs
Solution
Programming Language: Python 3 Language
n=int(input()) l=input().split() inp=[];out=[] for i in l: s="" for j in i: if j.isnumeric(): s+=j elif j=='(' and s: out.append(int(s)) s="" elif j==')' and s: inp.append(int(s)) s="" if s: out.append(int(s)) inp=sorted(inp) out=sorted(out)[::-1] for i in range(n): if l[i][-1]==')': print(out[i],'(',inp[i],')',sep="",end=" ") else: print('(',inp[i],')',out[i],sep="",end=" ")
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments