diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-01-22 09:43:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-22 09:43:42 +0000 |
| commit | 24d59f1e00bccc06340c6a6fcbf7862221530080 (patch) | |
| tree | aa92ac48f18ed414f141c0f6484599deb3e67bac | |
| parent | 0d47cb6d0529bb13aa8f8109c4994f33179a8fb2 (diff) | |
| parent | 089e55afba6a49ae8354005583658c682b5c4d07 (diff) | |
| download | perlweeklychallenge-club-24d59f1e00bccc06340c6a6fcbf7862221530080.tar.gz perlweeklychallenge-club-24d59f1e00bccc06340c6a6fcbf7862221530080.tar.bz2 perlweeklychallenge-club-24d59f1e00bccc06340c6a6fcbf7862221530080.zip | |
Merge pull request #11478 from jeanluc2020/jeanluc2020-305
Add solution 305.
| -rw-r--r-- | challenge-305/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-305/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-305/jeanluc2020/perl/ch-1.pl | 89 | ||||
| -rwxr-xr-x | challenge-305/jeanluc2020/perl/ch-2.pl | 57 |
4 files changed, 148 insertions, 0 deletions
diff --git a/challenge-305/jeanluc2020/blog-1.txt b/challenge-305/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..4af99d7359 --- /dev/null +++ b/challenge-305/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-305-1.html diff --git a/challenge-305/jeanluc2020/blog-2.txt b/challenge-305/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..64719cc1d1 --- /dev/null +++ b/challenge-305/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-305-2.html diff --git a/challenge-305/jeanluc2020/perl/ch-1.pl b/challenge-305/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..4d6ae5bf64 --- /dev/null +++ b/challenge-305/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-305/#TASK1 +# +# Task 1: Binary Prefix +# ===================== +# +# You are given a binary array. +# +# Write a script to return an array of booleans where the partial binary number +# up to that point is prime. +# +## Example 1 +## +## Input: @binary = (1, 0, 1) +## Output: (false, true, true) +## +## Sub-arrays (base-10): +## (1): 1 - not prime +## (1, 0): 2 - prime +## (1, 0, 1): 5 - prime +# +## Example 2 +## +## Input: @binary = (1, 1, 0) +## Output: (false, true, false) +## +## Sub-arrays (base-10): +## (1): 1 - not prime +## (1, 1): 3 - prime +## (1, 1, 0): 6 - not prime +# +## Example 3 +## +## Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1) +## Output: (false, true, true, false, false, true, false, false, false, false, +## false, false, false, false, false, false, false, false, false, true) +# +############################################################ +## +## discussion +## +############################################################ +# +# We can initialize $num as 0. Appending another binary digit is the same +# as multiplying $num by two, then adding the next digit. So we can go +# digit by digit, calculating the new number in each step and checking whether +# or not it is prime. Then we just need to print the result. +# Note: I used the is_prime() function from my solution to challenge 233. + +use strict; +use warnings; + +binary_prefix(1, 0, 1); +binary_prefix(1, 1, 0); +binary_prefix(1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1); + +sub binary_prefix { + my @binary = @_; + print "Input: (" . join(", ", @binary) . ")\n"; + my @result = (); + my $num = 0; + foreach my $digit (@binary) { + $num *= 2; + $num += $digit; + push @result, is_prime($num) ? "true" : "false"; + } + print "Output: (" . join(", ", @result) . ")\n"; +} + + +{ + my $cache; + sub is_prime { + my $num = shift; + return 0 if $num == 1; + return $cache->{$num} if defined $cache->{$num}; + my $divider = 2; + while($divider <= sqrt($num)) { + if(int($num/$divider) == $num/$divider) { + $cache->{$num} = 0; + return 0; + } + $divider++; + } + $cache->{$num} = 1; + return 1; + } +} + diff --git a/challenge-305/jeanluc2020/perl/ch-2.pl b/challenge-305/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..47e6e61c85 --- /dev/null +++ b/challenge-305/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-305/#TASK2 +# +# Task 2: Alien Dictionary +# ======================== +# +# You are given a list of words and alien dictionary character order. +# +# Write a script to sort lexicographically the given list of words based on the +# alien dictionary characters. +# +## Example 1 +## +## Input: @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/ +## Output: ("raku", "python", "perl") +# +## Example 2 +## +## Input: @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/ +## Output: ("challenge", "the", "weekly") +# +############################################################ +## +## discussion +## +############################################################ +# +# We transliterate all words to their conjugate word in the transliterated +# namespace, then we can sort alphabetically. The we transliterate the sorted +# words back to the original words. + +use strict; +use warnings; + +alien_dictionary( ["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/ ); +alien_dictionary( ["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/ ); + +sub alien_dictionary { + my ($words, @alien) = @_; + print "Input: (" . join(", ", @$words) . ")\n"; + print " qw/" . join(" ", @alien) . "/\n"; + my $alien = join("",@alien); + my @tmp = (); + foreach my $w (@$words) { + eval "\$w =~ tr/a-z/$alien/;"; + push @tmp, $w; + } + my @sorted = sort @tmp; + @tmp = (); + foreach my $w (@sorted) { + eval "\$w =~ tr /$alien/a-z/;"; + push @tmp, $w; + } + print "Output: (" . join(", ", @tmp) . ")\n"; +} |
