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) 

2 <= N <= 100
1 <= Each integer value <= 10

 Input Format 

The first line contains N.
The second line contains N pairs of integers separated by a space.

 Output Format 

The first line contains the revised N integer pairs separated by a space.

 Example Input/Output 1 

 Input 

4
12(52) (25)50 (35)10 44(60)

 Output 

50(25) (35)44 (52)12 10(60)

 Explanation 

Here N = 4.
The 4 integers that are present inside the parentheses are 52, 25, 35 and 60.
The 4 integers that are present outside the parentheses are 12, 50, 10 and 44.
After sorting those integers in the pairs based on the given condition, the pairs become
50(25) (35)44 (52)12 10(60)

 Example Input/Output 2 

 Input 

3
626(564) (343)752 (179)99

 Output 

752(179) (343)626 (564)99

 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

Popular Posts

Highly Profitable Months

Minimum Sum of Non-Negative Elements

Largest Possible Number With Digits

Top Scoring Batsman - TEST ODI 20-20

Print OddEven

Count Embedded Integers in String

String - Vowels Position Sum