Advertisement
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 AnonymousThreat {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- List<String> elements = Arrays.stream(input.nextLine().split("\\s+")).collect(Collectors.toList());
- String expression;
- String[] command;
- while(!(expression = input.nextLine().toLowerCase()).equals("3:1")){
- command = expression.split("\\s+");
- switch (command[0]){
- case "merge": elements = mergesStrings(elements, Integer.parseInt(command[1]), Integer.parseInt(command[2])); break;
- case "divide": elements = dividesString(elements, Integer.parseInt(command[1]), Integer.parseInt(command[2])); break;
- default: break;
- }
- }
- elements.forEach(e -> System.out.print(e + " "));
- }
- static List<String> mergesStrings(List<String> str, int startIndex, int endIndex){
- String newExpression = "";
- if(startIndex < 0){
- startIndex = 0;
- }
- if(endIndex > str.size() - 1){
- endIndex = str.size() - 1;
- }
- int indexOfTheNewExpression = startIndex;
- for (int otherIndex = startIndex; otherIndex <= endIndex; otherIndex++) {
- newExpression += str.get(indexOfTheNewExpression);
- str.remove(indexOfTheNewExpression);
- }
- if(str.size() < indexOfTheNewExpression){
- str.add(newExpression);
- }
- else{
- str.add(indexOfTheNewExpression, newExpression);
- }
- return str;
- }
- static List<String> dividesString(List<String> str, int index, int partitions){
- if(index >= 0 && index < str.size() && partitions >= 0 && partitions <= 100){
- String newExpression = str.get(index);
- str.remove(index);
- int number = newExpression.length();
- int portionLength = number / partitions;
- if(number % partitions == 0){
- int anotherStartIndex = 0;
- int anotherEndIndex = portionLength;
- for(int a = 0; a < partitions; a++){
- str.add(index + a, newExpression.substring(anotherStartIndex, anotherEndIndex));
- anotherStartIndex += portionLength;
- anotherEndIndex += portionLength;
- }
- }
- else if(number % partitions != 0){
- int anotherStartIndex = 0;
- int anotherEndIndex = portionLength;
- for(int a = 0; a < partitions; a++){
- if(a == partitions - 1){
- str.add(index + partitions - 1, newExpression.substring(number - 1 - portionLength, number - 1));
- break;
- }
- str.add(index + a, newExpression.substring(anotherStartIndex, anotherEndIndex));
- anotherStartIndex += portionLength;
- anotherEndIndex += portionLength;
- }
- }
- }
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement