Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created September 26, 2016 21:40
Show Gist options
  • Save chermehdi/b134335e9b170253394f9687e5d6592a to your computer and use it in GitHub Desktop.
Save chermehdi/b134335e9b170253394f9687e5d6592a to your computer and use it in GitHub Desktop.
Sorting scorify challenge of the month challenge
package com.algorithms;
/**
* @Author Mehdi Maick
* Created on 25/09/2016.
*/
import java.util.*;
import java.io.*;
public class Sorting {
public static void solve(FastReader fs, PrintWriter pw) {
String line;
String[] data;
boolean[] pos;
int t = fs.nextInt();
while (t-- > 0){
data = fs.nextLine().split(" ");
int n = data.length;
ArrayList<Integer> ints = new ArrayList<>();
ArrayList<String> s = new ArrayList<>();
pos = new boolean[n];
for (int i = 0; i < n; i++) {
try{
int tmp = Integer.parseInt(data[i]);
ints.add(tmp);
pos[i] = true;
}catch(Exception e){
s.add(data[i]);
}
}
ints.sort(null);
s.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String a = o1.toLowerCase();
String b = o2.toLowerCase();
return a.compareTo(b);
}
});
StringBuilder sb = new StringBuilder();
int j =0, k = 0;
for (int i = 0; i < n; i++) {
if(pos[i]){
sb.append(ints.get(j++)+ " ");
}else{
sb.append(s.get(k++) + " ");
}
}
pw.println(sb.toString().trim());
}
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment