SvetlanPetrova

AnonymousThreat SoftUni

Jun 29th, 2021
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class AnonymousThreat {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         String namesInput = scanner.nextLine(); //"Ivo Johny Tony Bony Mony"
  10.         List<String> names = Arrays.stream(namesInput.split("\\s+")).collect(Collectors.toList());
  11.  
  12.         String command = scanner.nextLine();
  13.         //•   "merge {startIndex} {endIndex}" -> split("\\s+") -> ["merge", "{startIndex}", "endIndex"]
  14.         //•   "divide {index} {partitions}" ->split("\\s+") -> ["divide", "{index}", "partitions"]
  15.         while(!command.equals("3:1")) {
  16.             String [] commandData = command.split("\\s+");
  17.             String commandName = commandData[0]; //merge or divide
  18.             if(commandName.equals("merge")) {
  19.                 int startIndex = Integer.parseInt(commandData[1]);
  20.                 int endIndex = Integer.parseInt(commandData[2]);
  21.                 if(startIndex < 0) {
  22.                     startIndex = 0;
  23.                 }
  24.                 if(endIndex > names.size() - 1) {
  25.                     endIndex = names.size() - 1;
  26.                 }
  27.                 //проверка за индексите
  28.                 boolean isStartIndexValid = isValidIndex(startIndex, names.size());
  29.                 boolean isEndIndexValid = isValidIndex(endIndex, names.size());
  30.                 // not sure why this check is needed since we've already compared the indexes to 0 & (names.size() - 1) ???
  31.                 //ако са валидни и двата
  32.                 if(isStartIndexValid && isEndIndexValid) {
  33.                     //{abc, def, ghi} -> merge 0 1 -> {abcdef, ghi}
  34.                     StringBuilder resultMerge = new StringBuilder();
  35.                     for (int index = startIndex; index <= endIndex; index++) {
  36.                         resultMerge.append(names.get(index));
  37.                     }
  38.                     for (int index = startIndex; index <= endIndex; index++) {
  39.                         names.remove(startIndex);
  40.                     }
  41.                     names.add(startIndex, resultMerge.toString());
  42.                 }
  43.             } else if (commandName.equals("divide")) {
  44.                 int index = Integer.parseInt(commandData[1]);
  45.                 int partitions = Integer.parseInt(commandData[2]); //number of parts after division
  46.                 String elementForDivide = names.get(index); //the element of the list which we will divide
  47.                 //{abcdef, ghi, jkl} -> {ghi, jkl}
  48.                 names.remove(index);
  49.                 //"abcdef" -> 6 / 3 = 2
  50.                 int partSize = elementForDivide.length() / partitions; //the size of each partition
  51.                 int begin = 0; //where we start iterating the element - from the start of the element => 0
  52.  
  53.                 for (int part = 1; part < partitions; part++) {
  54.                     names.add(index, elementForDivide.substring(begin, begin + partSize));
  55.                     //String.substring extracts part of the string between two indexes passed as parameters
  56.                     //if we pass only 1 parameter => the extracted substring is from the passed index to the end of the string
  57.                     index++; //to add the next partition after the current partition
  58.                     begin += partSize;
  59.                 }
  60.  
  61.                 names.add(index, elementForDivide.substring(begin));
  62.  
  63.             }
  64.             command = scanner.nextLine();
  65.         }
  66.  
  67.         System.out.println(String.join(" ", names));
  68.  
  69.     }
  70.  
  71.     public static boolean isValidIndex(int index, int size) {
  72.         return index >= 0 && index <= size - 1;
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment