Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> input = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("3:1")) {
- String[] commandParts = command.split(" ");
- if (commandParts[0].equals("merge")) {
- mergeMethod(input, commandParts[1], commandParts[2]);
- } else if (commandParts[0].equals("divide")) {
- divideMethod(input, commandParts[1], commandParts[2]);
- }
- command = scanner.nextLine();
- }
- for (String text : input) {
- System.out.print(text + " ");
- }
- }
- private static void divideMethod(List<String> input, String commandPart, String commandPart1) {
- int index = Integer.parseInt(commandPart);
- int parts = Integer.parseInt(commandPart1);
- if (index >= 0 && index <= input.size() - 1) {
- String textToDivide = input.get(index);
- input.remove(index);
- int symbolsCount = textToDivide.length() / parts;
- int startIndex = 0;
- for (int part = 1; part < parts; part++) {
- String textPerPart = textToDivide.substring(startIndex, startIndex + symbolsCount);
- input.add(index, textPerPart);
- index++;
- startIndex += symbolsCount;
- }
- String textLastParts = textToDivide.substring(startIndex, textToDivide.length());
- input.add(index, textLastParts);
- }
- }
- private static void mergeMethod (List < String > input, String start, String end){
- int starts = Integer.parseInt(start);
- int ends = Integer.parseInt(end);
- if (starts < 0) {
- starts = 0;
- }
- if (ends > input.size() - 1) {
- ends = input.size() - 1;
- }
- if (starts >= 0 && starts <= input.size() - 1 && ends >= 0 && ends <= input.size() - 1) {
- String merge = "";
- for (int i = starts; i <= ends; i++) {
- merge += input.get(i);
- }
- for (int index = starts; index <= ends; index++) {
- input.remove(starts);
- }
- input.add(starts, merge);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment