The weekly challenge 269 - Task 2: Distribute Elements

  1 #!/usr/bin/perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-269/#TASK2
  3 #
  4 # Task 2: Distribute Elements
  5 # ===========================
  6 #
  7 # You are given an array of distinct integers, @ints.
  8 #
  9 # Write a script to distribute the elements as described below:
 10 #
 11 ## 1) Put the 1st element of the given array to a new array @arr1.
 12 ## 2) Put the 2nd element of the given array to a new array @arr2.
 13 #
 14 # Once you have one element in each arrays, @arr1 and @arr2, then follow the rule below:
 15 #
 16 ## If the last element of the array @arr1 is greater than the last
 17 ## element of the array @arr2 then add the first element of the
 18 ## given array to @arr1 otherwise to the array @arr2.
 19 #
 20 # When done distribution, return the concatenated arrays. @arr1 and @arr2.
 21 #
 22 ## Example 1
 23 ##
 24 ## Input: @ints = (2, 1, 3, 4, 5)
 25 ## Output: (2, 3, 4, 5, 1)
 26 ##
 27 ## 1st operation:
 28 ## Add 1 to @arr1 = (2)
 29 ##
 30 ## 2nd operation:
 31 ## Add 2 to @arr2 = (1)
 32 ##
 33 ## 3rd operation:
 34 ## Now the last element of @arr1 is greater than the last element
 35 ## of @arr2, add 3 to @arr1 = (2, 3).
 36 ##
 37 ## 4th operation:
 38 ## Again the last element of @arr1 is greate than the last element
 39 ## of @arr2, add 4 to @arr1 = (2, 3, 4)
 40 ##
 41 ## 5th operation:
 42 ## Finally, the last element of @arr1 is again greater than the last
 43 ## element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)
 44 ##
 45 ## Mow we have two arrays:
 46 ## @arr1 = (2, 3, 4, 5)
 47 ## @arr2 = (1)
 48 ##
 49 ## Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
 50 #
 51 ## Example 2
 52 ##
 53 ## Input: @ints = (3, 2, 4)
 54 ## Output: (3, 4, 2)
 55 ##
 56 ## 1st operation:
 57 ## Add 1 to @arr1 = (3)
 58 ##
 59 ## 2nd operation:
 60 ## Add 2 to @arr2 = (2)
 61 ##
 62 ## 3rd operation:
 63 ## Now the last element of @arr1 is greater than the last element
 64 ## of @arr2, add 4 to @arr1 = (3, 4).
 65 ##
 66 ## Mow we have two arrays:
 67 ## @arr1 = (3, 4)
 68 ## @arr2 = (2)
 69 ##
 70 ## Concatenate the two arrays and return the final array: (3, 4, 2).
 71 #
 72 ## Example 3
 73 ##
 74 ## Input: @ints = (5, 4, 3 ,8)
 75 ## Output: (5, 3, 4, 8)
 76 ##
 77 ## 1st operation:
 78 ## Add 1 to @arr1 = (5)
 79 ##
 80 ## 2nd operation:
 81 ## Add 2 to @arr2 = (4)
 82 ##
 83 ## 3rd operation:
 84 ## Now the last element of @arr1 is greater than the last element
 85 ## of @arr2, add 3 to @arr1 = (5, 3).
 86 ##
 87 ## 4th operation:
 88 ## Again the last element of @arr2 is greate than the last element
 89 ## of @arr1, add 8 to @arr2 = (4, 8)
 90 ##
 91 ## Mow we have two arrays:
 92 ## @arr1 = (5, 3)
 93 ## @arr2 = (4, 8)
 94 ##
 95 ## Concatenate the two arrays and return the final array: (5, 3, 4, 8).
 96 #
 97 ############################################################
 98 ##
 99 ## discussion
100 ##
101 ############################################################
102 #
103 # Not much to do than to implement the given rules.
104 
105 use strict;
106 use warnings;
107 
108 distribute_elements(2, 1, 3, 4, 5);
109 distribute_elements(3, 2, 4);
110 distribute_elements(5, 4, 3 ,8);
111 
112 sub distribute_elements {
113    my @ints = @_;
114    my (@arr1, @arr2);
115    print "Input: (", join(", ", @ints), ")\n";
116    if($#ints <= 1) {
117       return print "Output: (", join(", ", @ints), ")\n";
118    }
119    ($arr1[0], $arr2[0], @ints) = @ints;
120    while(@ints) {
121       if($arr1[-1] > $arr2[-1]) {
122          push @arr1, shift @ints;
123       } else {
124          push @arr2, shift @ints;
125       }
126    }
127    @ints = (@arr1, @arr2);
128    print "Output: (", join(", ", @ints), ")\n";
129 }