diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-14 13:56:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-14 13:56:14 +0100 |
| commit | 96f01a00cc745c76200b1bb0edca9a56a9aa3d0e (patch) | |
| tree | 6bb18ef49ce87b279e7039cf1e691208470d8228 | |
| parent | 39c0a7809fadf47232350104b9d8275d59d63b5c (diff) | |
| parent | ec5ca1a8d465ef00f2841ce254ad3e8c70bbbf3d (diff) | |
| download | perlweeklychallenge-club-96f01a00cc745c76200b1bb0edca9a56a9aa3d0e.tar.gz perlweeklychallenge-club-96f01a00cc745c76200b1bb0edca9a56a9aa3d0e.tar.bz2 perlweeklychallenge-club-96f01a00cc745c76200b1bb0edca9a56a9aa3d0e.zip | |
Merge pull request #10094 from jacoby/master
DAJ 269
| -rw-r--r-- | challenge-269/dave-jacoby/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-269/dave-jacoby/perl/ch-2.pl | 40 |
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-269/dave-jacoby/perl/ch-1.pl b/challenge-269/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..058e9fbe77 --- /dev/null +++ b/challenge-269/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = ( + + [ 1, 2, 3, 4, 5, ], + [ 2, 3, 8, 16, ], + [ 1, 2, 5, 7, 9, ], +); +for my $example (@examples) { + my $output = bitwise_or_fun(@$example); + $output = $output ? 'true' : 'false'; + my $array = join ', ', $example->@*; + say <<"END"; + Input: \@array = ($array) + Output: $output +END +} + +sub bitwise_or_fun (@array) { + my @copy = @array; + for my $i ( 0 .. -1 + scalar @array ) { + for my $j ( $i + 1 .. -1 + scalar @array ) { + my $x = $array[$i]; + my $y = $array[$j]; + my $z = $x | $y; + return 1 if $z % 2 == 0; + } + } + return 0; +} diff --git a/challenge-269/dave-jacoby/perl/ch-2.pl b/challenge-269/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..53fdabc8a2 --- /dev/null +++ b/challenge-269/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( + + [ 2, 1, 3, 4, 5 ], + [ 3, 2, 4 ], + [ 5, 4, 3, 8 ], +); + +for my $example (@examples) { + my @output = distribute_elements( $example->@* ); + my $output = join ', ', @output; + my $ints = join ', ', $example->@*; + + say <<"END"; + Input: \@ints = ($ints) + Output: ($output) +END +} + +sub distribute_elements (@ints) { + my @arr1; + my @arr2; + push @arr1, shift @ints; + push @arr2, shift @ints; + + while ( @ints ) { + my $i = shift @ints; + if ( $arr1[-1] > $arr2[-1]) { + push @arr1, $i; + } else { + push @arr2, $i; + } + } + return @arr1,@arr2; +} |
