From a30a02a801156caa3e40c1a02e5f92fe08449dd3 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Wed, 22 Jan 2025 10:22:17 +0100 Subject: Challenge 305 --- challenge-305/mahnkong/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-305/mahnkong/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-305/mahnkong/perl/ch-1.pl create mode 100644 challenge-305/mahnkong/perl/ch-2.pl 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"); -- cgit