aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-01-22 10:22:17 +0100
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-01-22 10:22:17 +0100
commita30a02a801156caa3e40c1a02e5f92fe08449dd3 (patch)
tree59a657869c53302cfc02dbe8617d586495fdc8d4
parentaab5052474cb534790d075a20d7c79521d5b68f1 (diff)
downloadperlweeklychallenge-club-a30a02a801156caa3e40c1a02e5f92fe08449dd3.tar.gz
perlweeklychallenge-club-a30a02a801156caa3e40c1a02e5f92fe08449dd3.tar.bz2
perlweeklychallenge-club-a30a02a801156caa3e40c1a02e5f92fe08449dd3.zip
Challenge 305
-rw-r--r--challenge-305/mahnkong/perl/ch-1.pl29
-rw-r--r--challenge-305/mahnkong/perl/ch-2.pl25
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-305/mahnkong/perl/ch-1.pl b/challenge-305/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..0e716c4bb6
--- /dev/null
+++ b/challenge-305/mahnkong/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use feature 'signatures';
+use experimental 'builtin';
+use builtin qw(true false);
+use Test::More 'no_plan';
+
+sub is_prime($number) {
+ return false if $number < 2;
+ for (my $d = 2 ; $d < $number; $d++) {
+ return false if $number % $d == 0;
+ }
+ return true;
+}
+
+sub run(@binary) {
+ my @result;
+ my $bin;
+ foreach my $b (@binary) {
+ $bin .= $b;
+ my $dec = oct('0b'. $bin);
+ push @result, is_prime($dec);
+ }
+
+ return @result;
+}
+
+is_deeply([run(1, 0, 1)], [(false, true, true)], "Example 1");
+is_deeply([run(1, 1, 0)], [(false, true, false)], "Example 2");
+is_deeply([run(1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1)], [(false, true, true, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true)], "Example 3");
diff --git a/challenge-305/mahnkong/perl/ch-2.pl b/challenge-305/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..74464d52b9
--- /dev/null
+++ b/challenge-305/mahnkong/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+use Data::Dumper;
+
+sub run($words, $alien) {
+ my %sorting_table;
+ my $leading_zeros = length(scalar(@$alien));
+ foreach my $c (@$alien) {
+ $sorting_table{$c} = sprintf("%0${leading_zeros}d", keys(%sorting_table) + 1);
+ }
+ return sort { get_sortable($a, \%sorting_table) cmp get_sortable($b, \%sorting_table) } @$words;
+}
+
+sub get_sortable($word, $sorting_table) {
+ my $sortable;
+ for my $c (split //, $word) {
+ $sortable .= $sorting_table->{$c};
+ }
+ return $sortable;
+}
+
+is_deeply([run(["perl", "python", "raku"], [qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/])], [("raku", "python", "perl")], "Example 1");
+is_deeply([run(["the", "weekly", "challenge"], [qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/])], [("challenge", "the", "weekly")], "Example 2");