Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.tools.javac.util.Assert;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.concurrent.atomic.AtomicInteger;
- @SuppressWarnings({"UnnecessaryUnboxing", "WeakerAccess", "BooleanConstructorCall",
- "UnnecessaryBoxing", "UnnecessaryContinue", "StringConcatenationInLoop"})
- public class Main {
- public static void main(String[] args) throws Exception {
- String t1 = "1,6,2,8,3";
- String t2 = "1,8,4,9,-4,2,2";
- String t3 = "4,7,1,8,-2";
- Assert.check(high_and_low(t1).equals("1, 8"));
- System.out.println(t1 + " \nresult: "+high_and_low(t1));
- System.out.println();
- Assert.check(high_and_low(t2).equals("-4, 9"));
- System.out.println(t2 + " \nresult: "+high_and_low(t2));
- System.out.println();
- Assert.check(high_and_low(t3).equals("-2, 8"));
- System.out.println(t3 + " \nresult: "+high_and_low(t3));
- }
- public static String high_and_low(String stringToTest){
- String low = sort(splitBy(stringToTest, ',')).get(0);
- int size = sort(splitBy(stringToTest, ',')).size();
- String high= sort(splitBy(stringToTest, ',')).get(size-1);
- String ret = "";
- for (Character c : low.toCharArray()){
- ret+=c;
- }
- ret +=',';
- ret +=' ';
- for (Character c : high.toCharArray()){
- ret+=c;
- }
- return ret;
- }
- public static ArrayList<String> splitBy(String s,Character charToFind){
- boolean[] whereTheCommasAre = charBoolArray(s,charToFind);
- ArrayList<String> numbers = new ArrayList<>(1);
- int idx = -1;
- for (int i = 0; i < whereTheCommasAre.length; i++) {
- if(new Boolean(whereTheCommasAre[i]).booleanValue()){
- String number = s.substring(idx+1,i);
- numbers.add(number);
- idx = i;
- }
- }
- String number = s.substring(idx+1);
- numbers.add(number);
- return numbers;
- }
- /**
- * @param s the String to test
- * @param charToFind the char to find
- * @return boolean[] true where the char is
- */
- public static boolean[] charBoolArray(String s,Character charToFind){
- boolean[] commas = new boolean[s.length()];
- int idx = 0;
- for (int i = 0; i < s.length(); i++) {
- Character c = s.toCharArray()[i];
- if(c.equals(charToFind)){
- commas[idx] = c.equals(charToFind);
- idx++;
- } else {
- idx++;
- }
- }
- return commas;
- }
- public static ArrayList<String> sort(ArrayList<String> numbers){
- while(!isSorted(new ArrayList<>(numbers))){
- numbers = trySort(numbers);
- }
- return numbers;
- }
- public static boolean isSorted(ArrayList<String> numbers){
- long min = Long.MIN_VALUE;
- for(String number : numbers){
- Long l = Long.parseLong(number);
- if(l<new Long(min)) return false;
- else min = l;
- }
- return true;
- }
- public static ArrayList<String> trySort(ArrayList<String> numbers){
- HashSet<String> failed_combos = new HashSet<>(1);
- for (int i = 0; ; i++) {
- try {
- Long li = failed_combos.contains(i + " " + 0) ? 0 : Long.parseLong(numbers.get(i));
- boolean swapped = false;
- for (int j = 0; j < numbers.size(); j++) {
- try {
- if (i > j) {
- failed_combos.add(i + " " + j);
- throw new Exception("i cant be bigger then j");
- }
- Long lj = failed_combos.contains(i + " " + j) ? 0 : Long.parseLong(numbers.get(j));
- if (li > lj && !failed_combos.contains(i + " " + j) && !swapped) {
- numbers = swap(numbers, new AtomicInteger(i), new AtomicInteger(j));
- swapped = true;
- } else {
- failed_combos.add(i + " " + j);
- }
- } catch (Exception e) {
- failed_combos.add(i + " " + j);
- }
- }
- }catch (IndexOutOfBoundsException e){
- break;
- }
- }
- return numbers;
- }
- private static ArrayList<String> swap(ArrayList<String> numbers, AtomicInteger a, AtomicInteger b) {
- if(a.equals(b)) return numbers;
- ArrayList<String> swapped = new ArrayList<>(1);
- for (int i = 0; i < numbers.size(); i++) {
- if(i == a.get()){
- swapped.add(numbers.get(b.get()));
- }
- if(i == b.get()){
- swapped.add(numbers.get(a.get()));
- }
- if(i != a.get() && i != b.get()) {
- swapped.add(numbers.get(i));
- }
- }
- return swapped;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment