Didart

Array Manipulator

Oct 10th, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ArrayManipulator_11 {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int[] numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
  9.                 .mapToInt(Integer::parseInt)
  10.                 .toArray();
  11.  
  12.         String input = "";
  13.  
  14.         while (!"end".equals(input = scanner.nextLine())) {
  15.  
  16.             String[] command = input.split("\\s+");
  17.  
  18.             if ("exchange".equals(command[0])) {
  19.                 numbers = exchange(numbers, command[1]);
  20.  
  21.             } else if ("max".equals(command[0])) {
  22.  
  23.                 if ("even".equals(command[1])) {
  24.                     maxEven(numbers);
  25.  
  26.                 } else if ("odd".equals(command[1])) {
  27.                     maxOdd(numbers);
  28.                 }
  29.  
  30.             } else if ("min".equals(command[0])) {
  31.  
  32.                 if ("even".equals(command[1])) {
  33.                     minEven(numbers);
  34.                 } else if ("odd".equals(command[1])) {
  35.                     minOdd(numbers);
  36.                 }
  37.  
  38.             } else if ("first".equals(command[0])) {
  39.                 firstCountElements(numbers, Integer.parseInt(command[1]), command[2]);
  40.  
  41.             } else  if ("last".equals(command[0])) {
  42.                 lastCountElements(numbers, Integer.parseInt(command[1]), command[2]);
  43.             }
  44.         }
  45.         System.out.println(Arrays.toString(numbers));
  46.     }
  47.  
  48.     private static void lastCountElements(int[] numbers, int count, String command) {
  49.         if (count < 0 || count > numbers.length) {
  50.             System.out.println("Invalid count");
  51.             return;
  52.         }
  53.  
  54.         int num = command.equals("even") ? 0 : 1;
  55.  
  56.         int index = 0;
  57.         int[] result = new int[numbers.length];
  58.  
  59.         for (int i = 0; i < numbers.length ; i++) {
  60.             if (numbers[i] % 2 == num) {
  61.                 result[index++] = numbers[i];
  62.             }
  63.         }
  64.  
  65.         if (index == 0) {
  66.             System.out.println("[]");
  67.  
  68.         } else {
  69.             int startIndex = index - count;
  70.             if (startIndex < 0) {
  71.                 startIndex = 0;
  72.             }
  73.             String output = "[";
  74.             for (int i = startIndex; i < index ; i++) {
  75.  
  76.                 if (i < index - 1) {
  77.                     output += result[i] + ", ";
  78.                 } else {
  79.                     output += result[i] + "]";
  80.                 }
  81.             }
  82.             System.out.println(output);
  83.         }
  84.     }
  85.  
  86.     private static void minOdd(int[] numbers) {
  87.         int minOdd = Integer.MAX_VALUE;
  88.         int minIndex = -1;
  89.  
  90.         for (int i = 0; i < numbers.length; i++) {
  91.  
  92.             if (numbers[i] % 2 != 0 && numbers[i] <= minOdd) {
  93.                 minOdd = numbers[i];
  94.                 minIndex = i;
  95.             }
  96.         }
  97.  
  98.         if (minIndex == -1) {
  99.             System.out.println("No matches");
  100.         } else {
  101.             System.out.println(minIndex);
  102.         }
  103.     }
  104.  
  105.     private static void minEven(int[] numbers) {
  106.         int minEven = Integer.MAX_VALUE;
  107.         int minIndex = -1;
  108.  
  109.         for (int i = 0; i < numbers.length; i++) {
  110.  
  111.             if (numbers[i] % 2 == 0 && numbers[i] <= minEven) {
  112.                 minEven = numbers[i];
  113.                 minIndex = i;
  114.             }
  115.         }
  116.  
  117.         if (minIndex == -1) {
  118.             System.out.println("No matches");
  119.         } else {
  120.             System.out.println(minIndex);
  121.         }
  122.     }
  123.  
  124.     private static void maxOdd(int[] numbers) {
  125.         int maxOdd = Integer.MIN_VALUE;
  126.         int maxIndex = -1;
  127.  
  128.         for (int i = 0; i < numbers.length ; i++) {
  129.  
  130.             if (numbers[i] % 2 != 0 && numbers[i] >= maxOdd ) {
  131.                 maxOdd = numbers[i];
  132.                 maxIndex = i;
  133.             }
  134.         }
  135.         if (maxIndex == -1) {
  136.             System.out.println("No matches");
  137.         } else {
  138.             System.out.println(maxIndex);
  139.         }
  140.     }
  141.  
  142.     private static void maxEven(int[] numbers) {
  143.         int maxEven = Integer.MIN_VALUE;
  144.         int maxIndex = -1;
  145.  
  146.         for (int i = 0; i < numbers.length ; i++) {
  147.  
  148.             if (numbers[i] % 2 == 0 && numbers[i] >= maxEven ) {
  149.                     maxEven = numbers[i];
  150.                     maxIndex = i;
  151.             }
  152.         }
  153.  
  154.         if (maxIndex == -1) {
  155.             System.out.println("No matches");
  156.         } else {
  157.             System.out.println(maxIndex);
  158.         }
  159.     }
  160.  
  161.     private static int[] exchange(int[] numbers, String s) {
  162.         int index = Integer.parseInt(s);
  163.  
  164.         if (index < 0 || index >= numbers.length) {
  165.             System.out.println("Invalid index");
  166.         } else {
  167.             int count = 0;
  168.             int [] result = new int[numbers.length];
  169.  
  170.             for (int i = index + 1; i < numbers.length ; i++) {
  171.                 result[count++] = numbers[i];
  172.             }
  173.             for (int i = 0; i <= index ; i++) {
  174.                 result[count++] = numbers[i];
  175.             }
  176.             numbers = result;
  177.         }
  178.         return numbers;
  179.     }
  180.  
  181.     private static void firstCountElements(int[] numbers, int count, String command) {
  182.         if (count < 0 || count > numbers.length) {
  183.             System.out.println("Invalid count");
  184.             return;
  185.         }
  186.  
  187.         int num = command.equals("even") ? 0 : 1;
  188.  
  189.         int index = 0;
  190.         int[] result = new int[numbers.length];
  191.  
  192.         for (int i = 0; i < numbers.length ; i++) {
  193.  
  194.             if (numbers[i] % 2 == num) {
  195.                 result[index++] = numbers[i];
  196.  
  197.                 if (index == count) {
  198.                     break;
  199.                 }
  200.             }
  201.         }
  202.  
  203.         if (index == 0) {
  204.             System.out.println("[]");
  205.  
  206.         } else {
  207.             String output = "[";
  208.             for (int i = 0; i < index ; i++) {
  209.  
  210.                 if (i < index - 1) {
  211.                     output += result[i] + ", ";
  212.                 } else {
  213.                     output += result[i] + "]";
  214.                 }
  215.             }
  216.             System.out.println(output);
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment