diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-27 21:58:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-27 21:58:22 +0100 |
| commit | 9cc77d8fb45f6113343904f24ae2bb0d555c7d7e (patch) | |
| tree | 320b5c2eece3c4c6edbb9b0f0fdae51915ef2c6f | |
| parent | 2a18805dfc167ed9cad533bc2456e57786a3f4b1 (diff) | |
| parent | 05d03fa95f8978504c51967a56286e020e1f5c16 (diff) | |
| download | perlweeklychallenge-club-9cc77d8fb45f6113343904f24ae2bb0d555c7d7e.tar.gz perlweeklychallenge-club-9cc77d8fb45f6113343904f24ae2bb0d555c7d7e.tar.bz2 perlweeklychallenge-club-9cc77d8fb45f6113343904f24ae2bb0d555c7d7e.zip | |
Merge pull request #10723 from jacoby/master
DAJ 284
| -rw-r--r-- | challenge-284/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-284/dave-jacoby/perl/ch-2.pl | 34 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-284/dave-jacoby/perl/ch-1.pl b/challenge-284/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2227e9d64a --- /dev/null +++ b/challenge-284/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ postderef say signatures state }; + +use List::Util qw{ uniq }; + +my @examples = ( # added a couple test entries + + [ 2, 2, 3, 4 ], + [ 1, 2, 2, 3, 3, 3 ], + [ 1, 1, 1, 3 ], + +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = lucky_integer($example); + say <<"END"; + Input: \@int = ($input) + Output: $output +END +} + +sub lucky_integer ($input) { + my %hash; + map { $hash{$_} ++ } $input->@*; + my @lucky = reverse sort grep { $hash{$_} == $_ } keys %hash; + return shift @lucky if scalar @lucky; + return -1; +} diff --git a/challenge-284/dave-jacoby/perl/ch-2.pl b/challenge-284/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..ae52adf2f9 --- /dev/null +++ b/challenge-284/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures }; + +my @examples = ( # added a couple test entries + + [ [ 2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5 ], [ 2, 1, 4, 3, 5, 6 ], ], + [ [ 3, 3, 4, 6, 2, 4, 2, 1, 3 ], [ 1, 3, 2 ], ], + [ [ 3, 0, 5, 0, 2, 1, 4, 1, 1 ], [ 1, 0, 3, 2 ], ] +); + +for my $example (@examples) { + my $list1 = join ', ', $example->[0]->@*; + my $list2 = join ', ', $example->[1]->@*; + my $output = join ', ', relative_sort($example) ; + say <<"END"; + Input: \@list1 = ($list1) + \@list2 = ($list2) + Output: ($output) +END +} + +sub relative_sort ($input) { + my @list1 = $input->[0]->@*; + my @list2 = $input->[1]->@*; + my @output; + for my $d (@list2) { + push @output, grep { $_ == $d } @list1; + @list1 = grep { $_ != $d } @list1; + } + return @output, sort @list1; +} |
