Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static TreeMap<Double, Map<String, String>> parseInput(String s, List<String> cols) {
- TreeMap<Double, Map<String, String>> table = new TreeMap<>();
- int pos = 0;
- while (pos < s.length()) {
- int semicolonPos = s.indexOf(';', pos);
- String segment = s.substring(pos, semicolonPos == -1 ? s.length() : semicolonPos);
- if (!segment.isEmpty()) {
- int colonLPos = segment.indexOf(":L");
- String ratesPrices = segment.substring(0, colonLPos);
- String lock = segment.substring(colonLPos + 2);
- cols.add(lock);
- String[] ratePricePairs = ratesPrices.split(",");
- for (int i = 0; i < ratePricePairs.length; i += 2) {
- try {
- double rate = Double.parseDouble(ratePricePairs[i]);
- String price = ratePricePairs[i + 1];
- table.putIfAbsent(rate, new HashMap<>());
- table.get(rate).put(lock, price);
- } catch (NumberFormatException e) {
- System.err.println("Error parsing rate as a double: " + ratePricePairs[i]);
- // Handle the error or log it as needed
- }
- }
- }
- if (semicolonPos == -1) {
- break;
- } else {
- pos = semicolonPos + 1;
- }
- }
- return table;
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String s = scanner.nextLine();
- List<String> cols = new ArrayList<>();
- TreeMap<Double, Map<String, String>> table = parseInput(s, cols);
- for (int i = 0; i < cols.size() + 1; i++) {
- if (i == 0) {
- System.out.print(" ");
- } else {
- System.out.print(cols.get(i - 1) + " ");
- }
- }
- System.out.println();
- for (Map.Entry<Double, Map<String, String>> entry : table.entrySet()) {
- double rate = entry.getKey();
- System.out.print(rate + " ");
- Map<String, String> lockPriceMap = entry.getValue();
- for (String col : cols) {
- System.out.print(lockPriceMap.getOrDefault(col, "") + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement