aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2023-04-20 00:05:26 +0200
committerE. Choroba <choroba@matfyz.cz>2023-04-20 00:05:26 +0200
commit6886d59ffdd4901ae1d8b5342ac90affc1889f7c (patch)
treefadd6bb2cd660bbd57c79c709d9248655c98b451
parent67be0f9ab853d4740b3c1d6d3954a46cc63afa13 (diff)
downloadperlweeklychallenge-club-6886d59ffdd4901ae1d8b5342ac90affc1889f7c.tar.gz
perlweeklychallenge-club-6886d59ffdd4901ae1d8b5342ac90affc1889f7c.tar.bz2
perlweeklychallenge-club-6886d59ffdd4901ae1d8b5342ac90affc1889f7c.zip
Add solutions to 213: Fun Sort & Shortest Route by E. Choroba
-rwxr-xr-xchallenge-213/e-choroba/perl/ch-1.pl15
-rwxr-xr-xchallenge-213/e-choroba/perl/ch-2.pl40
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-213/e-choroba/perl/ch-1.pl b/challenge-213/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..1ea9d0f520
--- /dev/null
+++ b/challenge-213/e-choroba/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub fun_sort(@list) {
+ [sort { $a % 2 <=> $b % 2 || $a <=> $b } @list]
+}
+
+use Test2::V0;
+plan 3;
+
+is fun_sort(1, 2, 3, 4, 5, 6), [2, 4, 6, 1, 3, 5], 'Example 1';
+is fun_sort(1, 2), [2, 1], 'Example 2';
+is fun_sort(1), [1], 'Example 3';
diff --git a/challenge-213/e-choroba/perl/ch-2.pl b/challenge-213/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..29515c86ad
--- /dev/null
+++ b/challenge-213/e-choroba/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use Graph::Undirected;
+
+sub shortest_route(%args) {
+ my $graph = 'Graph::Undirected'->new;
+ for my $route (@{ $args{routes} }) {
+ for my $i (0 .. $#$route - 1) {
+ $graph->add_edge($route->[$i], $route->[ $i + 1 ]);
+ }
+ }
+ my @route = $graph->SP_Dijkstra(@args{qw{ source destination }});
+ return \@route if @route;
+
+ return -1
+}
+
+use Test2::V0;
+plan 3;
+
+is shortest_route(routes => [[1, 2, 6], [5, 6, 7]],
+ source => 1,
+ destination => 7),
+ [1, 2, 6, 7],
+ 'Example 1';
+
+is shortest_route(routes => [[1, 2, 3], [4, 5, 6]],
+ source => 2,
+ destination => 5),
+ -1,
+ 'Example 2';
+
+is shortest_route(routes => [[1, 2, 3], [4, 5, 6], [3, 8, 9], [7, 8]],
+ source => 1,
+ destination => 7),
+ [1, 2, 3, 8, 7],
+ 'Example 3';