Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 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 kur {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         List<String> input = Arrays.stream(scanner.nextLine().split("\\s+")).collect(Collectors.toList());
  12.  
  13. //        for (int i = 0; i < input.size(); i++) {
  14. //            String element = input.get(i);
  15. //            for (int j = 0; j < element.length(); j++) {
  16. //                char symbol = element.charAt(j);
  17. //                if (symbol == 32) {
  18. //                    return;
  19. //                }
  20. //            }
  21. //        }
  22.  
  23.         String command = scanner.nextLine();
  24.  
  25.         while (!"3:1".equals(command)) {
  26.             String[] splitTheCommand = command.split("\\s+");
  27.  
  28.             switch (splitTheCommand[0]) {
  29.                 case "merge":
  30.  
  31.                     int startIndex = Integer.parseInt(splitTheCommand[1]);
  32.                     int endIndex = Integer.parseInt(splitTheCommand[2]);
  33.  
  34.                     if (startIndex < 0) {
  35.                         startIndex = 0;
  36.                     }
  37.                     if (endIndex > input.size() - 1) {
  38.                         endIndex = input.size() - 1;
  39.                     }
  40.                     if (startIndex < input.size()) {
  41.                         String mergedData = "";
  42.  
  43.                         int counter = startIndex;
  44.                         for (int i = startIndex; i < endIndex; i++) {
  45.                             String concat = input.get(counter) + input.get(counter + 1);
  46.                             input.set(counter, concat);
  47.                             input.remove(counter + 1);
  48.                         }
  49.                     }
  50.                     break;
  51.                 case "divide":
  52.                     int index = Integer.parseInt(splitTheCommand[1]);
  53.                     int partitions = Integer.parseInt(splitTheCommand[2]);
  54.  
  55.                     if (index >= 0 && index < input.size() && partitions >= 0 && partitions <= 100) {
  56.                         String element = input.get(index);
  57.                         List<String> newList = new ArrayList<>();
  58.  
  59.                         if (element.length() % partitions == 0) {
  60.                             int portionLength = element.length() / partitions;
  61.                             int count = 0;
  62.                             for (int i = 0; i < partitions; i++) {
  63.                                 String concat = "";
  64.                                 for (int j = 0; j < portionLength; j++) {
  65.                                     char symbol = element.charAt(count);
  66.                                     concat += symbol;
  67.                                     count++;
  68.                                 }
  69.                                 newList.add(concat);
  70.                             }
  71.                         } else {
  72.                             int portionLength = element.length() / partitions;
  73.                             int count = 0;
  74.                             for (int i = 0; i < partitions; i++) {
  75.                                 String concat = "";
  76.  
  77.                                 if (i == partitions - 1) {
  78.                                     for (int j = count; j < element.length(); j++) {
  79.                                         char symbol = element.charAt(count);
  80.                                         concat += symbol;
  81.                                         count++;
  82.                                     }
  83.                                 } else {
  84.                                     for (int j = 0; j < portionLength; j++) {
  85.                                         char symbol = element.charAt(count);
  86.                                         concat += symbol;
  87.                                         count++;
  88.                                     }
  89.                                 }
  90.                                 newList.add(concat);
  91.                             }
  92.                         }
  93.                         input.remove(index);
  94.                         input.addAll(index, newList);
  95.                     }
  96.                     break;
  97.             }
  98.             command = scanner.nextLine();
  99.         }
  100.  
  101.         for (String item : input) {
  102.             System.out.print(item + " ");
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement