Advertisement
chono55

ArraysManipulator

Oct 13th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ArrayMainpulator {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String toArray = scanner.nextLine();
  8.         String[] ArrayStr = toArray.split(" ");
  9.         int[] ArrayInt = new int[ArrayStr.length];
  10.  
  11.         for (int y = 0 ; y < ArrayInt.length ; y++){
  12.             ArrayInt[y] = Integer.parseInt(ArrayStr[y]);
  13.         }
  14.  
  15.         String command = scanner.nextLine();
  16.  
  17.         while (!command.equals("end")){
  18.  
  19.             if (command.matches("exchange(.*)")){
  20.  
  21.                 String[] SplitInx = command.split(" ");
  22.                 int splitIndex = Integer.parseInt(SplitInx[1]);
  23.                 if (splitIndex < 0 || splitIndex >= ArrayInt.length){
  24.                     System.out.println("Invalid index");
  25.                 }else {
  26.                     ArrayInt = Exchange(splitIndex, ArrayInt);
  27.                     // Za nakraq
  28.                     //System.out.println(Arrays.toString(ArrayInt));
  29.                 }
  30.             }
  31.  
  32.             if (command.matches("max(.*)")){
  33.  
  34.                 int MaxInx = MaxInx(command,ArrayInt);
  35.  
  36.                 if (MaxInx == -1){
  37.                     System.out.println("No matches");
  38.                 }else{
  39.                     System.out.println(MaxInx);
  40.                 }
  41.             }
  42.  
  43.             if (command.matches("min(.*)")){
  44.  
  45.                 int MinInx = MinInx(command,ArrayInt);
  46.  
  47.                 if (MinInx == -1){
  48.                     System.out.println("No matches");
  49.                 }else{
  50.                     System.out.println(MinInx);
  51.                 }
  52.             }
  53.  
  54.             if (command.matches("first(.*)")){
  55.                 FirstInx(command,ArrayInt);
  56.             }
  57.  
  58.             if (command.matches("last(.*)")){
  59.                 LastInx(command,ArrayInt);
  60.             }
  61.  
  62.  
  63.             command = scanner.nextLine();
  64.         }
  65.  
  66.         System.out.println(Arrays.toString(ArrayInt));
  67.     }
  68.  
  69.     static int[] Exchange(int splitIndex , int[] ArrayInt) {
  70.         int[] ExchangeArr = new int[ArrayInt.length];
  71.  
  72.         int inx = 0;
  73.  
  74.         for (int f = splitIndex + 1; f < ArrayInt.length ; f++){
  75.             ExchangeArr[inx] = ArrayInt[f];
  76.             inx ++;
  77.         }
  78.  
  79.         for (int x = 0 ; x <= splitIndex ; x++){
  80.             ExchangeArr[inx] = ArrayInt[x];
  81.             inx ++;
  82.         }
  83.  
  84.         ArrayInt = ExchangeArr;
  85.  
  86.         return ArrayInt;
  87.  
  88.     }
  89.  
  90.     static int MaxInx(String c , int[] d){
  91.  
  92.         int max = Integer.MIN_VALUE;
  93.         int index = -1;
  94.  
  95.         if (c.matches("(.*)even")){
  96.  
  97.             for (int a = 0; a < d.length ; a++){
  98.  
  99.                 if (d[a] % 2 == 0){
  100.  
  101.                     if (d[a] >= max){
  102.                         max = d[a];
  103.                         index = a;
  104.                     }
  105.  
  106.                 }
  107.             }
  108.  
  109.         }
  110.  
  111.         if (c.matches("(.*)odd")){
  112.  
  113.             for (int a = 0; a < d.length ; a++){
  114.                 if (d[a] == 0){
  115.                     continue;
  116.                 }
  117.                 if (d[a] % 2 != 0){
  118.  
  119.                     if (d[a] >= max){
  120.                         max = d[a];
  121.                         index = a;
  122.                     }
  123.  
  124.                 }
  125.             }
  126.  
  127.         }
  128.  
  129.         return index;
  130.  
  131.     }
  132.  
  133.     static int MinInx(String c , int[] d){
  134.         int min = Integer.MAX_VALUE;
  135.         int index = -1;
  136.  
  137.         if (c.matches("(.*)even")){
  138.  
  139.             for (int a = 0; a < d.length ; a++){
  140.  
  141.                 if (d[a] % 2 == 0){
  142.  
  143.                     if (d[a] <= min){
  144.                         min = d[a];
  145.                         index = a;
  146.                     }
  147.  
  148.                 }
  149.             }
  150.  
  151.         }
  152.  
  153.         if (c.matches("(.*)odd")){
  154.  
  155.             for (int a = 0; a < d.length ; a++){
  156.                 if (d[a] == 0){
  157.                     continue;
  158.                 }
  159.  
  160.                 if (d[a] % 2 != 0){
  161.  
  162.                     if (d[a] <= min){
  163.                         min = d[a];
  164.                         index = a;
  165.                     }
  166.  
  167.                 }
  168.             }
  169.  
  170.         }
  171.  
  172.         return index;
  173.     }
  174.  
  175.     static void FirstInx(String c , int[] d) {
  176.  
  177.         String foundOddEven = "";
  178.         int foundNums = 0;
  179.         String[] threeArr = c.split(" ");
  180.  
  181.         int indexes = Integer.parseInt(threeArr[1]);
  182.         if (indexes > d.length) {
  183.             System.out.println("Invalid count");
  184.         } else {
  185.  
  186.             if (threeArr[2].equals("even")) {
  187.                 for (int k = 0; k < d.length; k++) {
  188.  
  189.                     if (d[k] % 2 == 0) {
  190.                         foundOddEven += d[k] + " ";
  191.  
  192.                         if (foundNums == indexes - 1) {
  193.                             break;
  194.                         }
  195.  
  196.                         foundNums++;
  197.                     }
  198.                 }
  199.  
  200.                 foundOddEven = foundOddEven.trim();
  201.             }
  202.  
  203.             if (threeArr[2].equals("odd")) {
  204.                 for (int k = 0; k < d.length; k++) {
  205.  
  206.                     if (d[k] == 0) {
  207.                         continue;
  208.                     }
  209.                     if (d[k] % 2 != 0) {
  210.                         foundOddEven += d[k] + " ";
  211.                         if (foundNums == indexes - 1) {
  212.                             break;
  213.                         }
  214.                         foundNums++;
  215.                     }
  216.                 }
  217.  
  218.                 foundOddEven = foundOddEven.trim();
  219.             }
  220.  
  221.  
  222.             if (foundNums == 0) {
  223.                 System.out.println("[]");
  224.             } else {
  225.  
  226.                 String[] reverse = foundOddEven.split(" ");
  227.                 System.out.println(Arrays.toString(reverse));
  228.             }
  229.  
  230.         }
  231.  
  232.     }
  233.  
  234.  
  235.  
  236.     static void LastInx(String c , int[] d){
  237.  
  238.         String foundOddEven = "";
  239.         int foundNums = 0;
  240.         String[] threeArr = c.split(" ");
  241.  
  242.         int indexes = Integer.parseInt(threeArr[1]);
  243.  
  244.         if (indexes > d.length) {
  245.             System.out.println("Invalid count");
  246.         } else {
  247.  
  248.             if (threeArr[2].equals("even")) {
  249.                 for (int k = d.length - 1; k >= 0 ; k--) {
  250.  
  251.                     if (d[k] % 2 == 0) {
  252.                         foundOddEven += d[k] + " ";
  253.  
  254.                         if (foundNums + 1 == indexes) {
  255.                             break;
  256.                         }
  257.  
  258.                         foundNums++;
  259.                     }
  260.                 }
  261.  
  262.                 foundOddEven = foundOddEven.trim();
  263.             }
  264.  
  265.             if (threeArr[2].equals("odd")) {
  266.                 for (int k = d.length - 1; k >= 0 ; k--) {
  267.                     if (d[k] == 0){
  268.                         continue;
  269.                     }
  270.                     if (d[k] % 2 != 0) {
  271.                         foundOddEven += d[k] + " ";
  272.                         if (foundNums + 1 == indexes) {
  273.                             break;
  274.                         }
  275.                         foundNums++;
  276.                     }
  277.                 }
  278.  
  279.                 foundOddEven = foundOddEven.trim();
  280.             }
  281.  
  282.             if (foundNums == 0) {
  283.                 System.out.println("[]");
  284.             } else {
  285.                 int i = 0;
  286.                 String[] reverse = foundOddEven.split(" ");
  287.                 String[] reverseItAll = new String[reverse.length];
  288.                 for (int m = reverseItAll.length - 1 ; m >= 0 ; m--){
  289.                     reverseItAll[i] = reverse[m];
  290.                     i++;
  291.                 }
  292.  
  293.                 System.out.println(Arrays.toString(reverseItAll));
  294.             }
  295.  
  296.         }
  297.     }
  298.  
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement