Advertisement
ChonoChonovUk

Anonymous Threat

Oct 20th, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. import com.sun.source.tree.IfTree;
  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.         Scanner scanner = new Scanner(System.in);
  12.         //single input line containing STRINGS separated by spaces
  13.         String lineToMOrD = scanner.nextLine();
  14.         // list of lineToMOrD
  15.         List<String> lineToList = Arrays.stream(lineToMOrD.split("\\s+")).collect(Collectors.toList());
  16.         String command = "";
  17.         //receiving merge or divide until "3:1"
  18.         while (!"3:1".equals(command = scanner.nextLine())){
  19.             //make an array of command
  20.             String[] commArr = command.split("\\s+");
  21.             switch (commArr[0]){
  22.                 case "merge":
  23.  
  24.                     int startIndex = Integer.parseInt(commArr[1]);
  25.                     int endIndex = Integer.parseInt(commArr[2]);
  26.  
  27.  
  28.                     String toMerge = "";
  29.  
  30.  
  31.  
  32.                     if (startIndex < 0){
  33.                         startIndex = 0;
  34.                     }else if (startIndex > lineToList.size() - 1){
  35.                         startIndex = lineToList.size() - 1;
  36.                     }
  37.  
  38.                     if (endIndex > lineToList.size() - 1){
  39.  
  40.                         endIndex = lineToList.size() - 1;
  41.  
  42.                     }else if (endIndex < 0){
  43.                         endIndex = 0;
  44.                     }
  45.                     //Use loop to concat string in List lineToList
  46.                         for (int i = startIndex; i < endIndex ; i++) {
  47.  
  48.                             toMerge = lineToList.get(i) + lineToList.get(i + 1);
  49.  
  50.                             lineToList.set(startIndex,toMerge);
  51.  
  52.                             lineToList.remove(i + 1);
  53.  
  54.                             endIndex--;
  55.                             i -= 1;
  56.                         }
  57.  
  58.  
  59.                     break;
  60.                 case "divide":
  61.  
  62.                     int indexDiv = Integer.parseInt(commArr[1]);
  63.                     int parts = Integer.parseInt(commArr[2]);
  64.  
  65.                        // get the string to divide intote list
  66.                         String checkIfDiv = lineToList.get(indexDiv);
  67.                         //Check string.length - even or add
  68.                         int divOrNot = checkIfDiv.length() % parts;
  69.                         if (divOrNot == 1) {
  70.                             divOrNot = checkIfDiv.length() / parts;
  71.                             String div = "";
  72.                             int inx = 0;
  73.                          //loop where to divide string to substrings
  74.                             for (int i = 0; i < parts; i++) {
  75.                                 for (int j = 0; j < divOrNot; j++) {
  76.                                    //for Odd use this to add the last char
  77.                                     if (i + 1 == parts) {
  78.                                         div += String.valueOf(checkIfDiv.charAt(inx));
  79.                                         inx++;
  80.                                         if ((j + 1) == divOrNot) {
  81.                                             div += String.valueOf(checkIfDiv.charAt(inx));
  82.                                         }
  83.                                     } else {
  84.                                         div += String.valueOf(checkIfDiv.charAt(inx));
  85.                                         inx++;
  86.                                     }
  87.                                 }
  88.  
  89.                                 lineToList.add(indexDiv, div);
  90.                                 indexDiv++;
  91.                                 div = "";
  92.  
  93.                             }
  94.  
  95.                             lineToList.remove(checkIfDiv);
  96.  
  97.                         } else if (divOrNot == 0) {
  98.                             divOrNot = checkIfDiv.length() / parts;
  99.                             String div = "";
  100.                             int inx = 0;
  101.                             for (int i = 0; i < parts; i++) {
  102.                                 for (int j = 0; j < divOrNot; j++) {
  103.                                     div += String.valueOf(checkIfDiv.charAt(inx));
  104.                                     inx++;
  105.                                 }
  106.  
  107.                                 lineToList.add(indexDiv, div);
  108.                                 indexDiv++;
  109.                                 div = "";
  110.                             }
  111.  
  112.                             lineToList.remove(checkIfDiv);
  113.                         }
  114.  
  115.  
  116.  
  117.  
  118.                     break;
  119.             }
  120.         }
  121.  
  122.         System.out.println(lineToList.toString().replaceAll("[\\[\\],]",""));
  123.     }
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement