# Perl Weekly Challenge 269.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 269.

# Task 1: Bitwise OR

``````Submitted by: Mohammad Sajid Anwar
You are given an array of positive integers, @ints.

Write a script to find out if it is possible to select two or more elements
of the given array such that the bitwise OR of the selected elements has at
least one trailing zero in its binary representation.

Example 1
Input: @ints = (1, 2, 3, 4, 5)
Output: true

Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 110.
Return true since we have one trailing zero.
Example 2
Input: @ints = (2, 3, 8, 16)
Output: true

Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is 1010.
Return true since we have one trailing zero.
Example 3
Input: @ints = (1, 2, 5, 7, 9)
Output: false
``````

The answer is true if I have two or more even numbers in the array. I can `grep` them and count them, which yields a half-liner.

Example 1:

``````perl -E 'say "@ARGV -> ", (@x=grep {(\$_&1)==0} @ARGV)>=2?"True":"False"; ' 1 2 3 4 5
``````

Results:

``````1 2 3 4 5 -> True
``````

Example 2:

``````perl -E 'say "@ARGV -> ", (@x=grep {(\$_&1)==0} @ARGV)>=2?"True":"False"; ' 2 3 8 16
``````

Results:

``````2 3 8 16 -> True
``````

Output: true

Example 3:

``````perl -E 'say "@ARGV -> ", (@x=grep {(\$_&1)==0} @ARGV)>=2?"True":"False"; ' 1 2 5 7 9
``````

Results:

``````1 2 5 7 9 -> False

1  # Perl weekly challenge 269
2  # Task 1:  Bitwise OR
3  #
4  # See https://wlmb.github.io/2024/05/13/PWC269/#task-1-bitwise-or
5  use v5.36;
6  die <<~"FIN" unless @ARGV;
7      Usage: \$0 N1 N2...
8      to find if there are two or more numbers Ni whose bitwise
9      or has the zeroth bit turned off.
10      FIN
11  say "@ARGV -> ", (my @x=grep {(\$_&1)==0} @ARGV)>=2?"True":"False";
``````

Examples:

``````./ch-1.pl 1 2 3 4 5
./ch-1.pl 2 3 8 16
./ch-1.pl 1 2 5 7 9
``````

Results:

``````1 2 3 4 5 -> True
2 3 8 16 -> True
1 2 5 7 9 -> False
``````

# Task 2: Distribute Elements

``````Submitted by: Mohammad Sajid Anwar
You are given an array of distinct integers, @ints.

Write a script to distribute the elements as described below:

1. Put the 1st element of the given array to a new array @arr1.
2. Put the 2nd element of the given array to a new array @arr2.
Once you have one element in each arrays, @arr1 and @arr2, then follow the rule below:

If the last element of the array @arr1 is greater than the last
element of the array @arr2 then add the first element of the
given array to @arr1 otherwise to the array @arr2.
When done distribution, return the concatenated arrays. @arr1 and @arr2.

Example 1
Input: @ints = (2, 1, 3, 4, 5)
Output: (2, 3, 4, 5, 1)

1st operation:
Add 1 to @arr1 = (2)

2nd operation:
Add 2 to @arr2 = (1)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (2, 3).

4th operation:
Again the last element of @arr1 is greate than the last element
of @arr2, add 4 to @arr1 = (2, 3, 4)

5th operation:
Finally, the last element of @arr1 is again greater than the last
element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)

Mow we have two arrays:
@arr1 = (2, 3, 4, 5)
@arr2 = (1)

Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
Example 2
Input: @ints = (3, 2, 4)
Output: (3, 4, 2)

1st operation:
Add 1 to @arr1 = (3)

2nd operation:
Add 2 to @arr2 = (2)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 4 to @arr1 = (3, 4).

Mow we have two arrays:
@arr1 = (3, 4)
@arr2 = (2)

Concatenate the two arrays and return the final array: (3, 4, 2).
Example 3
Input: @ints = (5, 4, 3 ,8)
Output: (5, 3, 4, 8)

1st operation:
Add 1 to @arr1 = (5)

2nd operation:
Add 2 to @arr2 = (4)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (5, 3).

4th operation:
Again the last element of @arr2 is greate than the last element
of @arr1, add 8 to @arr2 = (4, 8)

Mow we have two arrays:
@arr1 = (5, 3)
@arr2 = (4, 8)

Concatenate the two arrays and return the final array: (5, 3, 4, 8).
``````

I simply follow the instructions. I use a reference to the array to which I should push the following element. This yields a one-liner.

Example 1:

``````perl -E '
@x=@ARGV;(\$y[0],\$z[0])=(shift,shift);for(@ARGV){\$l=\$y[-1]>\$z[-1]?\@y:\@z;push @\$l,\$_}say "@x->@y @z";
' 2 1 3 4 5
``````

Results:

``````2 1 3 4 5->2 3 4 5 1

perl -E '
@x=@ARGV;(\$y[0],\$z[0])=(shift,shift);for(@ARGV){\$l=\$y[-1]>\$z[-1]?\@y:\@z;push @\$l,\$_}say "@x->@y @z";
' 3 2 4
``````

Results:

``````3 2 4->3 4 2
``````

Example 3:

``````perl -E '
@x=@ARGV;(\$y[0],\$z[0])=(shift,shift);for(@ARGV){\$l=\$y[-1]>\$z[-1]?\@y:\@z;push @\$l,\$_}say "@x->@y @z";
' 5 4 3 8
``````

Results:

``````5 4 3 8->5 3 4 8
``````

The full code follows:

`````` 1  # Perl weekly challenge 269
2  # Task 2:  Distribute Elements
3  #
4  # See https://wlmb.github.io/2024/05/13/PWC269/#task-2-distribute-elements
5  use v5.36;
6  die <<~"FIN" unless @ARGV>=2;
7      Usage: \$0 N1 N2...
8      to redistribute the numbers N1 N2...
9      FIN
10  my @in=@ARGV;
11  my @y=(shift);
12  my @z=(shift);
13  for(@ARGV){
14      my \$which=\$y[-1]>\$z[-1]?\@y:\@z;
15      push @\$which,\$_;
16  }
17  say "@in->@y @z";
``````

Examples:

``````./ch-2.pl 2 1 3 4 5
./ch-2.pl 3 2 4
./ch-2.pl 5 4 3 8
``````

Results:

``````2 1 3 4 5->2 3 4 5 1
3 2 4->3 4 2
5 4 3 8->5 3 4 8
``````
Written on May 13, 2024