aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-18 19:25:30 +0100
committerGitHub <noreply@github.com>2023-04-18 19:25:30 +0100
commit8b4d23342028492adc05a32ac3fee3a5d7223fe9 (patch)
tree0931788ffabc8a0ec45f68281bdc9f70e2296916
parent0308a868f07abc043a2005b29ff7fda416e4c6ca (diff)
parent274b7ea9884e80ab5ebddb0d3c1de8815da63533 (diff)
downloadperlweeklychallenge-club-8b4d23342028492adc05a32ac3fee3a5d7223fe9.tar.gz
perlweeklychallenge-club-8b4d23342028492adc05a32ac3fee3a5d7223fe9.tar.bz2
perlweeklychallenge-club-8b4d23342028492adc05a32ac3fee3a5d7223fe9.zip
Merge pull request #7925 from PerlBoy1967/branch-for-challenge-213
w213 - Task 1 & 2
-rwxr-xr-xchallenge-213/perlboy1967/perl/ch1.pl34
-rwxr-xr-xchallenge-213/perlboy1967/perl/ch2.pl53
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-213/perlboy1967/perl/ch1.pl b/challenge-213/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..f971f8fb10
--- /dev/null
+++ b/challenge-213/perlboy1967/perl/ch1.pl
@@ -0,0 +1,34 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 213
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-213
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Fun Sort
+Submitted by: Mohammad S Anwar
+
+You are given a list of positive integers.
+
+Write a script to sort the all even integers first then all odds in ascending order.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+sub evenOddSort {
+ sort { (($a & 1) <=> ($b & 1)) || ($a <=> $b) } @_;
+}
+
+cmp_deeply([evenOddSort(1,2,3,4,5,6)],[2,4,6,1,3,5]);
+cmp_deeply([evenOddSort(1,2)],[2,1]);
+cmp_deeply([evenOddSort(1)],[1]);
+
+done_testing;
diff --git a/challenge-213/perlboy1967/perl/ch2.pl b/challenge-213/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..3c26e59c5c
--- /dev/null
+++ b/challenge-213/perlboy1967/perl/ch2.pl
@@ -0,0 +1,53 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 213
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-213
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Shortest Route
+Submitted by: Mohammad S Anwar
+
+You are given a list of bidirectional routes defining a network of nodes, as well
+as source and destination node numbers.
+
+Write a script to find the route from source to destination that passes through
+fewest nodes.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use Paths::Graph;
+use List::MoreUtils qw(slide);
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+sub shortestRoute ($$\@) {
+ my ($o,$d,$ar) = @_;
+ my %g;
+
+ for (@$ar) {
+ slide {
+ ($g{$a}{$b},$g{$b}{$a}) = (1,1);
+ } @$_;
+ }
+
+ my @p = Paths::Graph->new(-origin => $o, -destiny => $d, -graph => \%g)->shortest_path;
+
+ return (scalar @p == 1 and defined $p[0][1] ? $p[0] : undef);
+}
+
+cmp_deeply(shortestRoute(1,7,@{[[1,2,6],[5,6,7]]}),
+ [1,2,6,7]);
+cmp_deeply(shortestRoute(2,5,@{[[1,2,3],[4,5,6]]}),
+ undef);
+cmp_deeply(shortestRoute(1,7,@{[[1,2,3],[4,5,6],[3,8,9],[7,8]]}),
+ [1,2,3,8,7]);
+
+done_testing;