Sort Integers - Outside Square Brackets
Sort Integers - Outside Square Brackets
PROBLEM STATEMENT :
The program must accept a list of integers where some groups of integers are enclosed within a pair of square brackets. The program must sort(in-place) the integers that occur outside the square brackets in the given list. Then the program must print the revised list of integers as the output.
Note: The given list does not have nested square brackets.
Boundary Condition(s):
1 <= Number of integers in the given list <= 100
1 <= Each integer value <= 10^5
Input Format:
The first line contains a list of integers separated by a space, where some groups of integers are enclosed within a pair of square brackets.
Output Format:
The first line contains the revised list of integers based on the given conditions.
Example Input/Output 1:
Input: ()
40 20 [50 10 60] 30 [9 8 7 5 1] 15 65 25
Output:
15 20 [50 10 60] 25 [9 8 7 5 1] 30 40 65
Explanation:
The integers that occur outside the square brackets are given below.
40 20 30 15 65 25
After sorting those integers in their positions, the list becomes
15 20 [50 10 60] 25 [9 8 7 5 1] 30 40 65
Example Input/Output 2:
Input: ()
[9 8 7 6] 5 4 3 2 [1]
Output:
[9 8 7 6] 2 3 4 5 [1]
Example Input/Output 3:
Input: ()
123 9875 [10 50 20 30 40] 8 9
Output:
8 9 [10 50 20 30 40] 123 9875
1) LEARN THRICE
๐
2) THINK TWICE
๐
3) APPLY ONCE
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[101][51];
int t,i=0,f=0,c=0;
int a[101];
while(scanf("%s ", arr[i])>0)
{
if(arr[i][0]=='[') f=1;
if(arr[i][strlen(arr[i])-1]==']') f=0;
else if(f==0)
{
a[c++]=atoi(arr[i]);
strcpy(arr[i],"re");
}
i++;
}
for(int j=0;j<c-1;++j)
{
for(int k=j+1;k<c;++k)
{
if(a[j]>a[k])
{
t=a[j];
a[j]=a[k];
a[k]=t;
}
}
}
c=0;
for(int j=0;j<i;++j)
{
if(strcmp(arr[j],"re")==0) printf("%d ", a[c++]);
else printf("%s ", arr[j]);
}
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
string s;
getline(cin, s);
stringstream ss(s);
vector<int> v, ind;
bool flag = false;
int index = 0, pos1 = 0, pos2 = 0;
vector<string> st;
while(ss >> s)
{
if(s[0] == '[')
flag = true;
if(!flag)
{
v.push_back(stoi(s));
ind.push_back(index);
}
else
st.push_back(s);
if(s.back() == ']')
flag = false;
index ++;
}
sort(v.begin(), v.end());
for(int i = 0; i < index; i++)
{
if(find(ind.begin(), ind.end(), i) != ind.end())
cout << v[pos1 ++] << " ";
else
cout << st[pos2 ++] << " ";
}
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim();
String[] strArray = str.split(" ");
List<Integer> list = new ArrayList<>();
int i;
for(i=0;i<strArray.length;i++){
if(strArray[i].charAt(0)=='[')
{
while(i<strArray.length&&strArray[i].charAt(strArray[i].length()-1)!=']')
i++;
i++;
}
if(i<strArray.length&&strArray[i].charAt(0)!='['&&strArray[i].charAt(strArray[i].length()-1)!=']'){
list.add(Integer.parseInt(strArray[i].trim()));
strArray[i]="*";
}
}
Collections.sort(list);
for(i=0;i<strArray.length;i++)
if(strArray[i]=="*")
System.out.print(list.remove(0)+" ");
else
System.out.print(strArray[i]+" ");
}
}
PYTHON
x=list(map(str,input().split()))
a,t,f=0,[],[]
while(a<len(x)):
if x[a][0]=='[':
while x[a][-1]!=']':
a+=1
a+=1
else:
t.append(a)
f.append(int(x[a]))
a+=1
f.sort()
for i in range(len(t)):
x[t[i]]=str(f[i])
print(' '.join(x))

Comments
Post a Comment