Didart

Anonymous Threat

Oct 17th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.util.ArrayList;
  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 AnonymousThreat {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.        
  11.         String input = scanner.nextLine();
  12.        
  13.         List<String> names = Arrays
  14.                 .stream(input.split("\\s+"))
  15.                 .collect(Collectors.toList());
  16.  
  17.         String command = scanner.nextLine();
  18.        
  19.         while(!command.equals("3:1")) {
  20.             String [] commandData = command.split("\\s+");
  21.             String commandName = commandData[0];
  22.             if(commandName.equals("merge")) {
  23.                 int startIndex = Integer.parseInt(commandData[1]);
  24.                 int endIndex = Integer.parseInt(commandData[2]);
  25.                 if(startIndex < 0) {
  26.                     startIndex = 0;
  27.                 }
  28.                 if(endIndex > names.size() - 1) {
  29.                     endIndex = names.size() - 1;
  30.                 }
  31.                
  32.                 boolean isStartIndexValid = isValidIndex(startIndex, names.size());
  33.                 boolean isEndIndexValid = isValidIndex(endIndex, names.size());
  34.                
  35.                 if(isStartIndexValid && isEndIndexValid) {
  36.                     StringBuilder resultMerge = new StringBuilder();
  37.                    
  38.                     for (int index = startIndex; index <= endIndex; index++) {
  39.                         resultMerge.append(names.get(index));
  40.                     }
  41.                     for (int index = startIndex; index <= endIndex; index++) {
  42.                         names.remove(startIndex);
  43.                     }
  44.                     names.add(startIndex, resultMerge.toString());
  45.                 }
  46.             } else if (commandName.equals("divide")) {
  47.                 int index = Integer.parseInt(commandData[1]);
  48.                 int partitions = Integer.parseInt(commandData[2]);
  49.                
  50.                 String elementForDivide = names.get(index);
  51.                 names.remove(index);
  52.            
  53.                 int partSize = elementForDivide.length() / partitions;
  54.                 int begin = 0;
  55.  
  56.                 for (int part = 1; part < partitions; part++) {
  57.                     names.add(index, elementForDivide.substring(begin, begin + partSize));
  58.                     index++;
  59.                     begin += partSize;
  60.                 }
  61.                 names.add(index, elementForDivide.substring(begin));
  62.             }
  63.             command = scanner.nextLine();
  64.         }
  65.         System.out.println(String.join(" ", names));
  66.     }
  67.  
  68.     public static boolean isValidIndex(int index, int size) {
  69.         return index >= 0 && index <= size - 1;
  70.     }
  71. }
  72.  
  73.    
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment