aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-26 13:30:12 +0100
committerGitHub <noreply@github.com>2024-08-26 13:30:12 +0100
commit5ac7454b4ed7ef568b16d79a7e7a16b09e8f9664 (patch)
tree3eb63e8ff9753bcdd48eddc6c2e716b5634f8dc6
parent127abb5fa1848d7f2809f7807277123ef599f9b4 (diff)
parent9945f7086c0e3383a200f4c7578d505096705107 (diff)
downloadperlweeklychallenge-club-5ac7454b4ed7ef568b16d79a7e7a16b09e8f9664.tar.gz
perlweeklychallenge-club-5ac7454b4ed7ef568b16d79a7e7a16b09e8f9664.tar.bz2
perlweeklychallenge-club-5ac7454b4ed7ef568b16d79a7e7a16b09e8f9664.zip
Merge pull request #10701 from PerlBoy1967/branch-for-challenge-284
w284 - Task 1 & 2
-rwxr-xr-xchallenge-284/perlboy1967/perl/ch1.pl40
-rwxr-xr-xchallenge-284/perlboy1967/perl/ch2.pl49
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-284/perlboy1967/perl/ch1.pl b/challenge-284/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..d747e83e42
--- /dev/null
+++ b/challenge-284/perlboy1967/perl/ch1.pl
@@ -0,0 +1,40 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 284
+- L<https://theweeklychallenge.org/blog/perl-weekly-challenge-284>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Lucky Integer
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints.
+
+Write a script to find the lucky integer if found otherwise return -1. If there are more
+than one then return the largest.
+
+|| A lucky integer is an integer that has a frequency in the array equal to its value.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+use List::Util qw(max);
+
+sub luckyInteger (@ints) {
+ my %f; $f{$_}++ for (@ints);
+ max(-1,grep { $f{$_} == $_ } keys %f);
+}
+
+is(luckyInteger(2,2,3,4),2,'Example 1');
+is(luckyInteger(1,2,2,3,3,3),3,'Example 2');
+is(luckyInteger(1,1,1,3),-1,'Example 3');
+
+done_testing;
diff --git a/challenge-284/perlboy1967/perl/ch2.pl b/challenge-284/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..2d7eee15df
--- /dev/null
+++ b/challenge-284/perlboy1967/perl/ch2.pl
@@ -0,0 +1,49 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 284
+- L<https://theweeklychallenge.org/blog/perl-weekly-challenge-284>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Relative Sort
+Submitted by: Mohammad Sajid Anwar
+
+You are given two list of integers, @list1 and @list2. The elements in the @list2 are
+distinct and also in the @list1.
+
+Write a script to sort the elements in the @list1 such that the relative order of items
+in @list1 is same as in the @list2. Elements that is missing in @list2 should be placed
+at the end of @list1 in ascending order.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+use List::MoreUtils qw(indexes);
+
+sub relativeSort :prototype(\@\@) ($arL1,$arL2) {
+ my @r;
+ my @l = @$arL1;
+ for my $l2 (@$arL2) {
+ push(@r,splice(@l,$_,1)) for (sort {$b <=> $a} indexes {$_ == $l2} @l);
+ }
+ push(@r,sort {$a <=> $b} @l);
+ [@r];
+}
+
+is(relativeSort(@{[2,3,9,3,1,4,6,7,2,8,5]},@{[2,1,4,3,5,6]}),
+ [2,2,1,4,3,3,5,6,7,8,9],'Example 1');
+is(relativeSort(@{[3,3,4,6,2,4,2,1,3]},@{[1,3,2]}),
+ [1,3,3,3,2,2,4,4,6],'Example 2');
+is(relativeSort(@{[3,0,5,0,2,1,4,1,1]},@{[1,0,3,2]}),
+ [1,1,1,0,0,3,2,4,5],'Example 3');
+
+done_testing;
+