Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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. int counter = startIndex;
  41.  
  42. for (int i = startIndex; i < endIndex; i++) {
  43. String concat = input.get(counter) + input.get(counter + 1);
  44. input.set(counter, concat);
  45. input.remove(counter + 1);
  46. }
  47. break;
  48. case "divide":
  49. int index = Integer.parseInt(splitTheCommand[1]);
  50. int partitions = Integer.parseInt(splitTheCommand[2]);
  51.  
  52. if (index >= 0 && index < input.size() && partitions >= 0 && partitions <= 100) {
  53. String element = input.get(index);
  54. List<String> newList = new ArrayList<>();
  55.  
  56. if (element.length() % partitions == 0) {
  57. int portionLength = element.length() / partitions;
  58. int count = 0;
  59. for (int i = 0; i < partitions; i++) {
  60. String concat = "";
  61. for (int j = 0; j < portionLength; j++) {
  62. char symbol = element.charAt(count);
  63. concat += symbol;
  64. count++;
  65. }
  66. newList.add(concat);
  67. }
  68. } else {
  69. int portionLength = element.length() / partitions;
  70. int count = 0;
  71. for (int i = 0; i < partitions; i++) {
  72. String concat = "";
  73.  
  74. if (i == partitions - 1) {
  75. for (int j = count; j < element.length(); j++) {
  76. char symbol = element.charAt(count);
  77. concat += symbol;
  78. count++;
  79. }
  80. } else {
  81. for (int j = 0; j < portionLength; j++) {
  82. char symbol = element.charAt(count);
  83. concat += symbol;
  84. count++;
  85. }
  86. }
  87. newList.add(concat);
  88. }
  89. }
  90. input.remove(index);
  91. input.addAll(index, newList);
  92. }
  93. break;
  94. }
  95. command = scanner.nextLine();
  96. }
  97.  
  98. for (String item : input) {
  99. System.out.print(item + " ");
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement