Advertisement
Guest User

SoftUni Java Fundamentals - Anonymous Threat

a guest
Oct 4th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class AnonymousThreat {
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.         List<String> elements = Arrays.stream(input.nextLine().split("\\s+")).collect(Collectors.toList());
  10.  
  11.         String expression;
  12.         String[] command;
  13.  
  14.         while(!(expression = input.nextLine().toLowerCase()).equals("3:1")){
  15.             command = expression.split("\\s+");
  16.             switch (command[0]){
  17.                 case "merge": elements = mergesStrings(elements, Integer.parseInt(command[1]), Integer.parseInt(command[2])); break;
  18.                 case "divide": elements = dividesString(elements, Integer.parseInt(command[1]), Integer.parseInt(command[2])); break;
  19.                 default: break;
  20.             }
  21.         }
  22.  
  23.         elements.forEach(e -> System.out.print(e + " "));
  24.     }
  25.  
  26.  
  27.     static List<String> mergesStrings(List<String> str, int startIndex, int endIndex){
  28.         String newExpression = "";
  29.  
  30.         if(startIndex < 0){
  31.             startIndex = 0;
  32.         }
  33.  
  34.         if(endIndex > str.size() - 1){
  35.             endIndex = str.size() - 1;
  36.         }
  37.  
  38.         int indexOfTheNewExpression = startIndex;
  39.  
  40.         for (int otherIndex = startIndex; otherIndex <= endIndex; otherIndex++) {
  41.             newExpression += str.get(indexOfTheNewExpression);
  42.             str.remove(indexOfTheNewExpression);
  43.         }
  44.  
  45.         if(str.size() < indexOfTheNewExpression){
  46.             str.add(newExpression);
  47.         }
  48.         else{
  49.             str.add(indexOfTheNewExpression, newExpression);
  50.         }
  51.  
  52.         return str;
  53.     }
  54.  
  55.  
  56.     static List<String> dividesString(List<String> str, int index, int partitions){
  57.         if(index >= 0 && index < str.size() && partitions >= 0 && partitions <= 100){
  58.             String newExpression = str.get(index);
  59.             str.remove(index);
  60.             int number = newExpression.length();
  61.             int portionLength = number / partitions;
  62.  
  63.             if(number % partitions == 0){
  64.                 int anotherStartIndex = 0;
  65.                 int anotherEndIndex = portionLength;
  66.  
  67.                 for(int a = 0; a < partitions; a++){
  68.                     str.add(index + a, newExpression.substring(anotherStartIndex, anotherEndIndex));
  69.                     anotherStartIndex += portionLength;
  70.                     anotherEndIndex += portionLength;
  71.                 }
  72.             }
  73.             else if(number % partitions != 0){
  74.                 int anotherStartIndex = 0;
  75.                 int anotherEndIndex = portionLength;
  76.  
  77.  
  78.                 for(int a = 0; a < partitions; a++){
  79.                     if(a == partitions - 1){
  80.                         str.add(index + partitions - 1, newExpression.substring(number - 1 - portionLength, number - 1));
  81.                         break;
  82.                     }
  83.  
  84.                     str.add(index + a, newExpression.substring(anotherStartIndex, anotherEndIndex));
  85.                     anotherStartIndex += portionLength;
  86.                     anotherEndIndex += portionLength;
  87.                 }
  88.             }
  89.         }
  90.  
  91.         return str;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement