Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package company;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- public class ArrangeIntegers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] numbers = Arrays.asList(scanner.nextLine().split("[\\s,]+"))
- .stream().mapToInt(Integer::parseInt).toArray();
- String[] sort = sorted(numbers);
- ArrayList<String> output = new ArrayList<>();
- for (int i = 0; i < sort.length; i++) {
- String[] line = sort[i].split("-");
- TreeMap<String, Integer> map = new TreeMap<>();
- map.put("zero", 0);
- map.put("one", 1);
- map.put("two", 2);
- map.put("three", 3);
- map.put("four", 4);
- map.put("five", 5);
- map.put("six", 6);
- map.put("seven", 7);
- map.put("eight", 8);
- map.put("nine", 9);
- StringBuilder num = new StringBuilder();
- for (int j = 0; j < line.length; j++) {
- int current = map.get(line[j]);
- num.append(current);
- }
- output.add(num.toString());
- }
- System.out.println(output.toString().substring(1, output.toString().length() - 1));
- }
- public static String[] sorted(int[] numbers) {
- String[] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- ArrayList<String> strNums = new ArrayList<>();
- for (int i = 0; i < numbers.length; i++) {
- String number = Integer.toString(numbers[i]);
- StringBuilder result = new StringBuilder();
- for (int j = 0; j < number.length(); j++) {
- int index = Integer.parseInt(Character.toString(number.charAt(j)));
- result.append(nums[index]);
- if (j < number.length() - 1) {
- result.append("-");
- }
- }
- strNums.add(result.toString());
- }
- String[] endResult = strNums
- .stream().sorted((str1, str2) -> str1.compareTo(str2))
- .collect(Collectors.toList()).toArray(new String[strNums.size()]);
- return endResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment