myrdok123

08.Anonymous Threat

Oct 16th, 2024 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1.  
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Demo {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         List<String> input = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  11.         String command = scanner.nextLine();
  12.         while (!command.equals("3:1")) {
  13.             String[] commandParts = command.split(" ");
  14.             if (commandParts[0].equals("merge")) {
  15.                 mergeMethod(input, commandParts[1], commandParts[2]);
  16.             } else if (commandParts[0].equals("divide")) {
  17.                 divideMethod(input, commandParts[1], commandParts[2]);
  18.             }
  19.             command = scanner.nextLine();
  20.         }
  21.         for (String text : input) {
  22.             System.out.print(text + " ");
  23.         }
  24.  
  25.     }
  26.  
  27.     private static void divideMethod(List<String> input, String commandPart, String commandPart1) {
  28.  
  29.         int index = Integer.parseInt(commandPart);
  30.         int parts = Integer.parseInt(commandPart1);
  31.  
  32.         if (index >= 0 && index <= input.size() - 1) {
  33.  
  34.             String textToDivide = input.get(index);
  35.             input.remove(index);
  36.  
  37.             int symbolsCount = textToDivide.length() / parts;
  38.  
  39.             int startIndex = 0;
  40.             for (int part = 1; part < parts; part++) {
  41.                 String textPerPart = textToDivide.substring(startIndex, startIndex + symbolsCount);
  42.                 input.add(index, textPerPart);
  43.                 index++;
  44.                 startIndex += symbolsCount;
  45.             }
  46.  
  47.             String textLastParts = textToDivide.substring(startIndex, textToDivide.length());
  48.             input.add(index, textLastParts);
  49.  
  50.  
  51.         }
  52.     }
  53.  
  54.         private static void mergeMethod (List < String > input, String start, String end){
  55.             int starts = Integer.parseInt(start);
  56.             int ends = Integer.parseInt(end);
  57.  
  58.             if (starts < 0) {
  59.                 starts = 0;
  60.             }
  61.             if (ends > input.size() - 1) {
  62.                 ends = input.size() - 1;
  63.             }
  64.  
  65.             if (starts >= 0 && starts <= input.size() - 1 && ends >= 0 && ends <= input.size() - 1) {
  66.                 String merge = "";
  67.                 for (int i = starts; i <= ends; i++) {
  68.                     merge += input.get(i);
  69.  
  70.                 }
  71.  
  72.                 for (int index = starts; index <= ends; index++) {
  73.                     input.remove(starts);
  74.                 }
  75.  
  76.  
  77.                 input.add(starts, merge);
  78.             }
  79.  
  80.  
  81.         }
  82.     }
  83.  
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment