diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-24 20:12:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-24 20:12:43 +0000 |
| commit | bcbd683e19b63a998b583baaaa42661bd892046d (patch) | |
| tree | c042504d7474000d342db69a53fe4c417f2bc805 | |
| parent | 15a251cfe0183a24bb4a933bd8689ccb2ec3e1f4 (diff) | |
| parent | 515c242bc70da927d6f81393e85fe4974353d76c (diff) | |
| download | perlweeklychallenge-club-bcbd683e19b63a998b583baaaa42661bd892046d.tar.gz perlweeklychallenge-club-bcbd683e19b63a998b583baaaa42661bd892046d.tar.bz2 perlweeklychallenge-club-bcbd683e19b63a998b583baaaa42661bd892046d.zip | |
Merge pull request #7620 from jacoby/master
DAJ #205
| -rw-r--r-- | challenge-205/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-205/dave-jacoby/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-205/dave-jacoby/perl/ch-2.pl | 36 |
3 files changed, 66 insertions, 0 deletions
diff --git a/challenge-205/dave-jacoby/blog.txt b/challenge-205/dave-jacoby/blog.txt new file mode 100644 index 0000000000..ab727aa55f --- /dev/null +++ b/challenge-205/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/02/22/reset-content-weekly-challenge-205.html diff --git a/challenge-205/dave-jacoby/perl/ch-1.pl b/challenge-205/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..794e99f3d4 --- /dev/null +++ b/challenge-205/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ uniq max }; + +my @examples = ( + + [ 5, 3, 4 ], + [ 5, 6 ], + [ 5, 4, 4, 3 ], +); + +for my $e (@examples) { + my $list = join ',', $e->@*; + my $out = third_highest( $e->@* ); + say <<"END"; + Input: \@array = ($list) + Output: $out +END +} + +sub third_highest ( @array ) { + @array = uniq sort { $b <=> $a } @array; + return max @array if scalar @array < 3; + return $array[2]; +} diff --git a/challenge-205/dave-jacoby/perl/ch-2.pl b/challenge-205/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..bcf621512c --- /dev/null +++ b/challenge-205/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max }; + +my @examples = ( + + [ 1, 2, 3, 4, 5, 6, 7 ], + [ 2, 4, 1, 3 ], + [ 10, 5, 7, 12, 8 ], +); + +for my $e (@examples) { + my $o = max_xor( $e->@* ); + my $array = $e->@*; + say <<"END"; + Input: \@array = $array + Output: $o +END +} + +sub max_xor ( @array ) { + my @output; + for my $i ( 0 .. -2 + scalar @array ) { + my $ii = $array[$i]; + for my $j ( $i + 1 .. -1 + scalar @array ) { + my $jj = $array[$j]; + my $x = $ii ^ $jj; + push @output, $x; + } + } + return max @output; +} |
