Advertisement
desislava_topuzakova

08. Anonymous Threat

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