diff options
| -rw-r--r-- | challenge-305/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-305/dave-jacoby/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-305/dave-jacoby/perl/ch-2.pl | 46 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-305/dave-jacoby/blog.txt b/challenge-305/dave-jacoby/blog.txt new file mode 100644 index 0000000000..ab3a5dd635 --- /dev/null +++ b/challenge-305/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/01/25/use-proxy-weekly-challenge-305.html diff --git a/challenge-305/dave-jacoby/perl/ch-1.pl b/challenge-305/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2473d8cdb9 --- /dev/null +++ b/challenge-305/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1 ], +); + +for my $example (@examples) { + my $binary = join ', ', $example->@*; + my @output = binary_prefix( $example->@* ); + my $output = join ', ', @output; + say <<"END"; + Input: \@binary = ($binary) + Output: ($output) +END +} + +sub binary_prefix (@binary) { + my @output; + for my $i ( 0 .. $#binary ) { + my @slice = @binary[ 0 .. $i ]; + my $bin = join '', @slice; + my $dec = oct( "0b" . $bin ); + push @output, is_prime($dec) ? 'true' : 'false'; + } + return @output; +} + +sub is_prime ($n) { + die "Bad number $n" unless length $n; + return 0 if $n == 0; + return 0 if $n == 1; + for ( 2 .. sqrt $n ) { return 0 unless $n % $_ } + return 1; +}
\ No newline at end of file diff --git a/challenge-305/dave-jacoby/perl/ch-2.pl b/challenge-305/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..0c34f5aa06 --- /dev/null +++ b/challenge-305/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + { + words => [ "perl", "python", "raku" ], + alien => [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/] + }, + { + words => [ "the", "weekly", "challenge" ], + alien => [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/] + }, +); + +for my $example (@examples) { + my $words = join ', ', map { qq{"$_"} } $example->{words}->@*; + my $alien = join ' ', $example->{alien}->@*; + my @output = alien_dictionary($example); + my $output = join ', ', @output; + say <<"END"; +Input: \@words = ($words) + \@alien = qw/$alien/ +Output: $output +END +} + +sub alien_dictionary ($object) { + my @words = $object->{words}->@*; + my @alien = $object->{alien}->@*; + my @alphabet = 'a' .. 'z'; + my %hash; + for my $i ( 0 .. $#alphabet ) { $hash{ $alien[$i] } = $alphabet[$i]; } + my @newwords = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { + my $new = join '', map { $hash{$_} } split '', $_; + [ $new, $_ ] + } @words; + return @newwords; +} + +sub alien ( $word, $hash ) { + return join '', map { $hash->{$_} } split '', $word; +}
\ No newline at end of file |
