Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class AnonymousThreat {
  10.     public static void main(String[] args) {
  11.  
  12.         Scanner scanner = new Scanner(System.in);
  13.         String lineToMOrD = scanner.nextLine();
  14.         List<String> lineToList = Arrays.stream(lineToMOrD.split("\\s+")).collect(Collectors.toList());
  15.         String command = "";
  16.  
  17.         while (!"3:1".equals(command = scanner.nextLine())) {
  18.             String[] commArr = command.split("\\s+");
  19.             switch (commArr[0]) {
  20.                 case "merge":
  21.                     int startIndex = Integer.parseInt(commArr[1]);
  22.                     int endIndex = Integer.parseInt(commArr[2]);
  23.                     merge(lineToList, startIndex, endIndex);
  24.  
  25.                     break;
  26.                 case "divide":
  27.                     int divideAtIndex = Integer.parseInt(commArr[1]);
  28.                     int numOfParts = Integer.parseInt(commArr[2]);
  29.                     divide(lineToList, divideAtIndex, numOfParts);
  30.  
  31.                     break;
  32.             }
  33.  
  34.         }
  35.         System.out.println(lineToList.toString().replaceAll("[\\[\\],]",""));
  36.     }
  37.  
  38.     private static void merge(List<String> strings, int startIndex, int endIndex) {
  39.         // Normalize indexes.
  40.         if (startIndex < 0) {
  41.             startIndex = 0;
  42.         }
  43.         else if (startIndex > strings.size() - 1) {
  44.             startIndex = strings.size() - 1;
  45.         }
  46.  
  47.         if (endIndex > strings.size() - 1) {
  48.             endIndex = strings.size() - 1;
  49.  
  50.         } else if (endIndex < 0) {
  51.             endIndex = 0;
  52.         }
  53.  
  54.         // For equal start and end indexes there is nothing to do.
  55.         if (startIndex == endIndex) {
  56.             return;
  57.         }
  58.  
  59.  
  60.         String merged = "";
  61.  
  62.         // Two possible approaches.
  63.  
  64.         // Approach 1:
  65.         for (int i = startIndex; i <= endIndex; i++) {
  66.             // We use the start index as the list shrinks every time.
  67.             merged += strings.remove(startIndex);
  68.         }
  69.         strings.add(startIndex, merged);
  70.  
  71.         // Approach 2:
  72.         // List<String> stringsToMerge = strings.subList(startIndex, endIndex);
  73.         // merged = String.join("", stringsToMerge);
  74.         // for (int i = startIndex; i <= endIndex; i++) {
  75.         //    // We use the start index as the list shrinks every time.
  76.         //    strings.remove(startIndex);
  77.         // }
  78.         // strings.add(startIndex, merged);
  79.     }
  80.  
  81.     private static void divide(List<String> strings, int divideAtIndex, int numOfParts) {
  82.         List<String> divided = new ArrayList<>();
  83.  
  84.         String stringToDivide = strings.get(divideAtIndex);
  85.         int stringLength = stringToDivide.length();
  86.         int partitionSize = stringLength / numOfParts;
  87.  
  88.         // Iterate through all partitions expect the last one.
  89.         int subStringStartPos = 0;
  90.         int subStringEndPos;
  91.         String partitionString;
  92.         for (int i = 1; i < numOfParts; i++) {
  93.             // Calculate the end position of the current partition.
  94.             subStringEndPos = subStringStartPos + partitionSize;
  95.  
  96.             partitionString = stringToDivide.substring(subStringStartPos, subStringEndPos);
  97.             divided.add(partitionString);
  98.  
  99.             // For the following partition start at the index after the current partition.
  100.             subStringStartPos = subStringEndPos;
  101.         }
  102.  
  103.         // Retrieve the last partition, which matches the remaining string. It might be empty if all the
  104.         // partitions were of an equal size.
  105.         partitionString = stringToDivide.substring(subStringStartPos);
  106.         if (partitionString.length() != 0) {
  107.             divided.add(partitionString);
  108.         }
  109.  
  110.         // Remove the string to divide and insert at its place the partitions.
  111.         strings.remove(divideAtIndex);
  112.         strings.addAll(divideAtIndex, divided);
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement