From 9a29a498d91ce334562ece670f6774de775a04fe Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 24 Oct 2023 23:01:16 -0400 Subject: Correct README for Challenge 239. --- challenge-239/packy-anderson/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/challenge-239/packy-anderson/README.md b/challenge-239/packy-anderson/README.md index 5e38fa1145..32ec4659ae 100644 --- a/challenge-239/packy-anderson/README.md +++ b/challenge-239/packy-anderson/README.md @@ -36,7 +36,21 @@ Using @arr2, word2 => "abcde" => "abcde" Sample output ``` +$ perl/ch-2.pl +Example 1: +Input: @str = ("ad", "bd", "aaab", "baa", "badab") + $allowed = "ab" +Output: 2 + +Example 2: +Input: @str = ("a", "b", "c", "ab", "ac", "bc", "abc") + $allowed = "abc" +Output: 7 +Example 3: +Input: @str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d") + $allowed = "cad" +Output: 4 ``` ## Raku @@ -75,7 +89,21 @@ Using @arr2, word2 => "abcde" => "abcde" Sample output ``` +$ raku/ch-2.raku +Example 1: +Input: @str = ("ad", "bd", "aaab", "baa", "badab") + $allowed = "ab" +Output: 2 + +Example 2: +Input: @str = ("a", "b", "c", "ab", "ac", "bc", "abc") + $allowed = "abc" +Output: 7 +Example 3: +Input: @str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d") + $allowed = "cad" +Output: 4 ``` ## Guest Language: Python -- cgit From f008267ad67a177cc8a10c16b0994ee948264a29 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 25 Oct 2023 01:48:44 -0400 Subject: Challenge 240 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-240/packy-anderson/README.md | 81 +++++++++++++---------------- challenge-240/packy-anderson/blog.txt | 1 + challenge-240/packy-anderson/perl/ch-1.pl | 27 ++++++++++ challenge-240/packy-anderson/perl/ch-2.pl | 23 ++++++++ challenge-240/packy-anderson/python/ch-1.py | 26 +++++++++ challenge-240/packy-anderson/python/ch-2.py | 20 +++++++ challenge-240/packy-anderson/raku/ch-1.raku | 25 +++++++++ challenge-240/packy-anderson/raku/ch-2.raku | 22 ++++++++ 8 files changed, 179 insertions(+), 46 deletions(-) create mode 100644 challenge-240/packy-anderson/blog.txt create mode 100755 challenge-240/packy-anderson/perl/ch-1.pl create mode 100755 challenge-240/packy-anderson/perl/ch-2.pl create mode 100755 challenge-240/packy-anderson/python/ch-1.py create mode 100755 challenge-240/packy-anderson/python/ch-2.py create mode 100755 challenge-240/packy-anderson/raku/ch-1.raku create mode 100755 challenge-240/packy-anderson/raku/ch-2.raku diff --git a/challenge-240/packy-anderson/README.md b/challenge-240/packy-anderson/README.md index 5e38fa1145..ca5343fc14 100644 --- a/challenge-240/packy-anderson/README.md +++ b/challenge-240/packy-anderson/README.md @@ -1,95 +1,84 @@ # Solutions by Packy Anderson -## Perl +## Raku -* [Task 1](perl/ch-1.pl) +* [Task 1](raku/ch-1.raku) Sample output ``` -$ perl/ch-1.pl +$ raku/ch-1.raku Example 1: -Input: @arr1 = ("ab", "c") - @arr2 = ("a", "bc") +Input: @str = ("Perl", "Python", "Pascal") + $chk = ("ppp") Output: true -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "a" . "bc" => "abc" - Example 2: -Input: @arr1 = ("ab", "c") - @arr2 = ("ac", "b") +Input: @str = ("Perl", "Raku") + $chk = ("rp") Output: false -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "ac" . "b" => "acb" - Example 3: -Input: @arr1 = ("ab", "cd", "e") - @arr2 = ("abcde") +Input: @str = ("Oracle", "Awk", "C") + $chk = ("oac") Output: true - -Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" -Using @arr2, word2 => "abcde" => "abcde" ``` -* [Task 2](perl/ch-2.pl) +* [Task 2](raku/ch-2.raku) Sample output ``` +$ raku/ch-2.raku +Example 1: +Input: @int = (0, 2, 1, 5, 3, 4) +Output: (0, 1, 2, 4, 5, 3) +Example 2: +Input: @int = (5, 0, 1, 2, 3, 4) +Output: (4, 5, 0, 1, 2, 3) ``` -## Raku +## Perl -* [Task 1](raku/ch-1.raku) +* [Task 1](perl/ch-1.pl) Sample output ``` -$ raku/ch-1.raku +$ perl/ch-1.pl Example 1: -Input: @arr1 = ("ab", "c") - @arr2 = ("a", "bc") +Input: @str = ("Perl", "Python", "Pascal") + $chk = ("ppp") Output: true -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "a" . "bc" => "abc" - Example 2: -Input: @arr1 = ("ab", "c") - @arr2 = ("ac", "b") +Input: @str = ("Perl", "Raku") + $chk = ("rp") Output: false -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "ac" . "b" => "acb" - Example 3: -Input: @arr1 = ("ab", "cd", "e") - @arr2 = ("abcde") +Input: @str = ("Oracle", "Awk", "C") + $chk = ("oac") Output: true - -Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" -Using @arr2, word2 => "abcde" => "abcde" ``` -* [Task 2](raku/ch-2.raku) +* [Task 2](perl/ch-2.pl) Sample output ``` +$ perl/ch-2.pl +Example 1: +Input: @int = (0, 2, 1, 5, 3, 4) +Output: (0, 1, 2, 4, 5, 3) +Example 2: +Input: @int = (5, 0, 1, 2, 3, 4) +Output: (4, 5, 0, 1, 2, 3) ``` ## Guest Language: Python * [Task 1](python/ch-1.py) * [Task 2](python/ch-2.py) -## Guest Language: Java - -To be completed later... - -## Guest Language: Elixir - -To be completed later... ## Blog Post -[Perl Weekly Challenge: Now it’s the same old string, but with consistency since you’ve been gone…](https://packy.dardan.com/2023/10/16/perl-weekly-challenge-now-its-the-same-old-string-but-with-consistency-since-youve-been-gone/) +[Perl Weekly Challenge: Building Acronym Arrays](https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/) diff --git a/challenge-240/packy-anderson/blog.txt b/challenge-240/packy-anderson/blog.txt new file mode 100644 index 0000000000..4b630f6b05 --- /dev/null +++ b/challenge-240/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/ \ No newline at end of file diff --git a/challenge-240/packy-anderson/perl/ch-1.pl b/challenge-240/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..a6e4fe3670 --- /dev/null +++ b/challenge-240/packy-anderson/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( reduce ); + +sub makeAcronym { + my $str = shift; + my $acronym = reduce { $a . substr($b, 0, 1) } '', @$str; + return lc($acronym); +} + +sub solution($chk, $str) { + say 'Input: @str = ("' . join('", "', @$str). '")'; + say ' $chk = ("' . $chk . '")'; + my ($acronym) = makeAcronym($str); + say 'Output: ' . ($acronym eq $chk ? 'true' : 'false'); +} + +say "Example 1:"; +solution("ppp", ["Perl", "Python", "Pascal"]); + +say "\nExample 2:"; +solution("rp", ["Perl", "Raku"]); + +say "\nExample 3:"; + +solution("oac", ["Oracle", "Awk", "C"]); \ No newline at end of file diff --git a/challenge-240/packy-anderson/perl/ch-2.pl b/challenge-240/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..c4e9485868 --- /dev/null +++ b/challenge-240/packy-anderson/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use v5.38; + +sub buildArray(@old) { + my @new; + foreach my $i (0 .. $#old) { + push @new, $old[$old[$i]]; + } + return @new; +} + +sub solution { + my @int = @_; + say 'Input: @int = (' . join(', ', @int) . ')'; + my @output = buildArray(@int); + say 'Output: (' . join(', ', @output) . ')'; +} + +say "Example 1:"; +solution(0, 2, 1, 5, 3, 4); + +say "\nExample 2:"; +solution(5, 0, 1, 2, 3, 4); \ No newline at end of file diff --git a/challenge-240/packy-anderson/python/ch-1.py b/challenge-240/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..6eb01f2285 --- /dev/null +++ b/challenge-240/packy-anderson/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +from functools import reduce + +def makeAcronym(str_list): + # add empty string to beginning of list + str_list = [''] + str_list + acronym = reduce(lambda a, b: a + b[0], str_list) + return acronym.lower() + +def solution(chk, str_list): + as_list = '"' + '", "'.join(str_list) + '"' + print(f'Input: @str = ({as_list})') + print(f' $chk = "{chk}"') + acronym = makeAcronym(str_list) + same = 'true' if acronym == chk else 'false' + print(f'Output: {same}') + +print('Example 1:') +solution("ppp", ["Perl", "Python", "Pascal"]) + +print('\nExample 2:') +solution("rp", ["Perl", "Raku"]) + +print('\nExample 3:') +solution("oac", ["Oracle", "Awk", "C"]) \ No newline at end of file diff --git a/challenge-240/packy-anderson/python/ch-2.py b/challenge-240/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..555d8c77a4 --- /dev/null +++ b/challenge-240/packy-anderson/python/ch-2.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +def buildArray(old): + new = [] + for i in range(0, len(old)): + new.append(old[old[i]]) + return new + +def solution(ints): + as_list = ', '.join(map(lambda i: str(i), ints)) + print(f'Input: @int = ({as_list})') + output = buildArray(ints) + as_list = ', '.join(map(lambda i: str(i), output)) + print(f'Output: ({as_list})') + +print('Example 1:') +solution([0, 2, 1, 5, 3, 4]) + +print('\nExample 2:') +solution([5, 0, 1, 2, 3, 4]) \ No newline at end of file diff --git a/challenge-240/packy-anderson/raku/ch-1.raku b/challenge-240/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..8370826d2b --- /dev/null +++ b/challenge-240/packy-anderson/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku +use v6; + +sub firstOfSecond { $^a ~ substr($^b, 0, 1) }; + +sub makeAcronym(@str) { + my $acronym = [[&firstOfSecond]] ('', |@str); + return $acronym.lc; +} + +sub solution($chk, @str) { + say 'Input: @str = ("' ~ @str.join('", "') ~ '")'; + say ' $chk = ("' ~ $chk ~ '")'; + my ($acronym) = makeAcronym(@str); + say 'Output: ' ~ ($acronym eq $chk ?? 'true' !! 'false'); +} + +say "Example 1:"; +solution("ppp", ["Perl", "Python", "Pascal"]); + +say "\nExample 2:"; +solution("rp", ["Perl", "Raku"]); + +say "\nExample 3:"; +solution("oac", ["Oracle", "Awk", "C"]); \ No newline at end of file diff --git a/challenge-240/packy-anderson/raku/ch-2.raku b/challenge-240/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..40b40da644 --- /dev/null +++ b/challenge-240/packy-anderson/raku/ch-2.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku +use v6; + +sub buildArray(@old) { + my @new; + for 0 .. @old.elems - 1 -> $i { + @new.push(@old[@old[$i]]); + } + return @new; +} + +sub solution(*@int) { + say 'Input: @int = (' ~ @int.join(', ') ~ ')'; + my @output = buildArray(@int); + say 'Output: (' ~ @output.join(', ') ~ ')'; +} + +say "Example 1:"; +solution(0, 2, 1, 5, 3, 4); + +say "\nExample 2:"; +solution(5, 0, 1, 2, 3, 4); \ No newline at end of file -- cgit From 1b80709ac09ecd2d9fc73b50cb2015f3c958bd0d Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 25 Oct 2023 01:58:38 -0400 Subject: Fix blog link for Packy Anderson challenge 240 --- challenge-240/packy-anderson/README.md | 2 +- challenge-240/packy-anderson/blog.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-240/packy-anderson/README.md b/challenge-240/packy-anderson/README.md index ca5343fc14..90e10e72b2 100644 --- a/challenge-240/packy-anderson/README.md +++ b/challenge-240/packy-anderson/README.md @@ -81,4 +81,4 @@ Output: (4, 5, 0, 1, 2, 3) ## Blog Post -[Perl Weekly Challenge: Building Acronym Arrays](https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/) +[Perl Weekly Challenge: Building Acronym Arrays](https://packy.dardan.com/2023/10/25/perl-weekly-challenge-building-acronym-arrays/) diff --git a/challenge-240/packy-anderson/blog.txt b/challenge-240/packy-anderson/blog.txt index 4b630f6b05..483113fdb9 100644 --- a/challenge-240/packy-anderson/blog.txt +++ b/challenge-240/packy-anderson/blog.txt @@ -1 +1 @@ -https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/ \ No newline at end of file +https://packy.dardan.com/2023/10/25/perl-weekly-challenge-building-acronym-arrays/ \ No newline at end of file -- cgit From b3d99b8c34d68d2eab5c0255dffe5943bd6515b1 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 30 Oct 2023 10:22:27 +0100 Subject: Solve 241: Arithmetic Triplets & Prime Order by E. Choroba --- challenge-241/e-choroba/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-241/e-choroba/perl/ch-2.pl | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100755 challenge-241/e-choroba/perl/ch-1.pl create mode 100755 challenge-241/e-choroba/perl/ch-2.pl diff --git a/challenge-241/e-choroba/perl/ch-1.pl b/challenge-241/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6a01ce9bb7 --- /dev/null +++ b/challenge-241/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub arithmetic_triplets($diff, @nums) { + my %in; + @in{@nums} = (); + + my $count = 0; + for my $num (@nums) { + ++$count if exists $in{ $num + $diff } + && exists $in{ $num + 2 * $diff }; + } + return $count +} + +use Test::More tests => 2 + 1; + +is arithmetic_triplets(3, 0, 1, 4, 6, 7, 10), 2, 'Example 1'; +is arithmetic_triplets(2, 4, 5, 6, 7, 8, 9), 2, 'Example 2'; +is arithmetic_triplets(10, 1 .. 19), 0, 'None'; diff --git a/challenge-241/e-choroba/perl/ch-2.pl b/challenge-241/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..06a6a1b487 --- /dev/null +++ b/challenge-241/e-choroba/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Math::Prime::Util qw{ factor }; +use List::UtilsBy qw{ nsort_by }; + +sub prime_order(@int) { + [nsort_by { scalar factor($_) } nsort_by { $_ } @int] +} + +use Test2::V0; +plan 1 + 1; + +is prime_order(11, 8, 27, 4), [11, 4, 8, 27], 'Example 1'; +is prime_order(27, 8), [8, 27], 'Secondary order'; -- cgit From c385438197d45a0777f3ad4aa30dd041553e8196 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 30 Oct 2023 02:40:57 -0700 Subject: Robbie Hatley's Perl solutions for The Weekly Challenge #241. --- challenge-241/robbie-hatley/blog.txt | 1 + challenge-241/robbie-hatley/perl/ch-1.pl | 139 ++++++++++++++++++++++++++++ challenge-241/robbie-hatley/perl/ch-2.pl | 150 +++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 challenge-241/robbie-hatley/blog.txt create mode 100755 challenge-241/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-241/robbie-hatley/perl/ch-2.pl diff --git a/challenge-241/robbie-hatley/blog.txt b/challenge-241/robbie-hatley/blog.txt new file mode 100644 index 0000000000..6f8580531b --- /dev/null +++ b/challenge-241/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/10/robbie-hatleys-solutions-to-weekly_30.html \ No newline at end of file diff --git a/challenge-241/robbie-hatley/perl/ch-1.pl b/challenge-241/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..576d87bb8a --- /dev/null +++ b/challenge-241/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,139 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 241-1. +Written by Robbie Hatley on Sun Oct 29, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 1: Arithmetic Triplets +Submitted by: Mohammad S Anwar +Given an array "nums" of 3-or-more integers in increasing order, +and a positive integer "diff", write a script to find the number +of unique "Arithmetic Triplets", where an "Arithmetic Triplet" +is a trio of numbers from nums which satisfies these rules: +a) i < j < k +b) nums[j] - nums[i] == diff +c) nums[k] - nums[j] == diff + +Example 1: +Input: @nums = (0, 1, 4, 6, 7, 10), $diff = 3 +Output: 2 +Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. +Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. + +Example 2: +Input: @nums = (4, 5, 6, 7, 8, 9), $diff = 2 +Output: 2 +(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. +(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem, I could resort to using CPAN module Math::Combinatorics as I usually do any time +a problem involves permutations or combinations, but in this case that seems like overkill to me, so I'll +use triple-nested three-part loops instead. I'll check to make sure all array elements are integers, but I +won't enforce the restriction that they be in increasing order, because there's no need: it wouldn't make my +program malfunction, and subsequences of non-monotonic sequences can still be arithmetic triplets. For +example, (17, -32, 53, -34, 47, -36, 14) contains the arithmetic triplet (-32, -34, -36) with period -2. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +double-quoted array of arrays of integers, apostrophes escaped, in proper Perl syntax, like so: +./ch-1.pl "([17, -32, 53, -34, 47, -36, 14, -2], [1, 17, 20, 28, 30, 39, 42, 11])" + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +# Format an array of ints as (3, 17, -42, 487): +sub arraystr (@array) {return '(' . join(', ', @array) . ')'} + +# Is a given string a decimal representation of an integer? +sub is_int ($x) {return $x =~ m/^-[1-9]\d*$|^0$|^[1-9]\d*$/} + +# Does a given array contain only three-or-more integers? +sub three_plus_ints (@array) { + return 0 if scalar(@array) < 3; + for ( my $i = 0 ; $i <= $#array ; ++$i ) { + return 0 if !is_int($array[$i]); + } + return 1; +} + +sub arithmetic_triplets ($aref, $diff) { + my @triplets; + for ( my $i = 0 ; $i <= $#$aref - 2 ; ++ $i ) { + for ( my $j = $i + 1 ; $j <= $#$aref - 1 ; ++ $j ) { + for ( my $k = $j + 1 ; $k <= $#$aref - 0 ; ++ $k ) { + $$aref[$k]-$$aref[$j] == $diff + && $$aref[$j]-$$aref[$i] == $diff + and push @triplets, [@$aref[$i, $j, $k]]; + } + } + } + return @triplets; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 inputs: + [0, 1, 4, 6, 7, 10, 3], + + # Example 2 inputs: + [4, 5, 6, 7, 8, 9, 2], +); + +# Main loop: +for my $aref (@arrays) { + say ''; + my @array = @$aref; + my $diff = pop @array; + say 'Array = ', arraystr(@$aref); + !three_plus_ints(@array) + and say 'Error: Array may contain only 3-or-more integers; skipping to next array.' + and next; + say "Diff = $diff"; + my @triplets = arithmetic_triplets(\@array, $diff); + my $n = scalar(@triplets); + !$n and say "Array contains no arithmetic triplets of difference $diff."; + $n and say "Array contains $n arithmetic triplets of difference $diff:"; + $n and say arraystr(@$_) for @triplets; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ diff --git a/challenge-241/robbie-hatley/perl/ch-2.pl b/challenge-241/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..7f0a54db7b --- /dev/null +++ b/challenge-241/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,150 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 241-2. +Written by Robbie Hatley on Sun Oct 29, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 2: Prime Order +Submitted by: Mohammad S Anwar +Given an array of unique positive integers greater than 2, write +a script to sort them in ascending order of the count of their +prime factors, tie-breaking by ascending value. + +Example 1: +Input: @int = (11, 8, 27, 4) +Output: (11, 4, 8, 27)) +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 27 => 3, 3, 3 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem requires finding all prime factors of any given positive integer > 2. Fortunately, +I know a way to do that which doesn't require finding any prime numbers at all! The algorithm is like this: +1. First, find-and-divide-out all copies of 2 from number "$x". +2. Set variable "$divisor" to 3. +3. Loop steps 3,4,5 while $x is greater than 1. +4. Find-and-divide-out all copies of $divisor from $x. +5. Increase $divisor by 2. +That works because none of the divisors of $x we find will ever be non-prime, because all non-prime numbers +have prime divisors, all of which are lesser than themselves, so THOSE divisors will already have been +found-and-divided-out from $x. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +double-quoted array of arrays of unique positive integers greater than 2, in proper Perl syntax, like so: +./ch-2.pl "([40320, 3, 25, 9, 7, 8], [40320, 5040, 120, 720])" + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +# Format an array of ints as (3, 17, -42, 487): +sub arraystr (@array) {return '(' . join(', ', @array) . ')'} + +# Is a given string a decimal representation of a positive integer greater than 2? +sub is_three_plus_pos_int ($x) { + return 0 if $x !~ m/^[1-9]\d*$/; + return 0 if $x < 3; + return 1; +} + +# Does a given array contain only two-or-more unique positive integers? +sub two_plus_unique_three_plus_pos_ints (@array) { + return 0 if scalar(@array) < 2; + for ( my $i = 0 ; $i <= $#array ; ++$i ) { + return 0 if !is_three_plus_pos_int($array[$i]); + } + for ( my $i = 0 ; $i <= $#array - 1 ; ++ $i ) { + for ( my $j = $i + 1 ; $j <= $#array - 0 ; ++ $j ) { + return 0 if $array[$j] == $array[$i]; + } + } + return 1; +} + +# Return all prime factors of a given positive integer: +sub prime_factors ($x) { + my @factors; + while ( 0 == $x % 2) { + push @factors, 2; + $x /= 2; + } + my $divisor = 3; + while ( $x > 1 ) { + while ( 0 == $x % $divisor) { + push @factors, $divisor; + $x /= $divisor; + } + $divisor += 2; + } + return @factors; +} + +# Compare two different positive integers, based firstly on +# number of prime factors, and secondly on element value: +sub by_number_of_prime_factors { + my $cmp; + $cmp = scalar(prime_factors($a)) <=> scalar(prime_factors($b)) + or $cmp = $a <=> $b; + return $cmp +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [11, 8, 27, 4], +); + +# Main loop: +for my $aref (@arrays) { + say ''; + say 'Original array = ', arraystr(@$aref); + !two_plus_unique_three_plus_pos_ints(@$aref) + and say 'Error: Array must contain only two-or-more unique positive integers' + and say 'greater than 2. Moving on to next array.' + and next; + my @sorted = sort by_number_of_prime_factors @$aref; + say 'Sorted array = ', arraystr(@sorted); + say "Prime factors of $_ = ", arraystr(prime_factors($_)) for @sorted; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ -- cgit From ddda260c43ee2d70d5ce3d5f552d1b768b9ff6ff Mon Sep 17 00:00:00 2001 From: rcmlz Date: Mon, 30 Oct 2023 11:03:52 +0100 Subject: solution ch-241 --- challenge-241/rcmlz/raku/task-one.rakumod | 29 +++++++++++++++++++++++++++++ challenge-241/rcmlz/raku/task-two.rakumod | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-241/rcmlz/raku/task-one.rakumod create mode 100644 challenge-241/rcmlz/raku/task-two.rakumod diff --git a/challenge-241/rcmlz/raku/task-one.rakumod b/challenge-241/rcmlz/raku/task-one.rakumod new file mode 100644 index 0000000000..e3ed5a9147 --- /dev/null +++ b/challenge-241/rcmlz/raku/task-one.rakumod @@ -0,0 +1,29 @@ +unit module rcmlz::raku::task-one:ver<0.0.1>:auth:api<1>; + +# run in terminal: raku --optimize=3 -I challenge-nr241/rcmlz/raku/ -- test/challenge-nr241/raku/task-one.rakutest +# or raku --optimize=3 -I challenge-nr241 -- test/benchmark-scalability.raku --task=task-one --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr241; cat /tmp/nr241_task-one.csv + +#|[ +You are given an array (3 or more members) of integers in increasing order and a positive integer. + +Write a script to find out the number of unique Arithmetic Triplets satisfying the following rules: + +a) i < j < k +b) nums[j] - nums[i] == diff +c) nums[k] - nums[j] == diff +] +our sub solution([$diff, *@input]) is export { + my UInt $counter = 0; + my UInt \n = @input.elems; + + for ^n -> \i { + for i+1..^n -> \j { + if @input[j] - @input[i] == $diff { + for j+1..^n -> \k { + $counter++ if @input[k] - @input[j] == $diff + } + } + } + } + $counter +} \ No newline at end of file diff --git a/challenge-241/rcmlz/raku/task-two.rakumod b/challenge-241/rcmlz/raku/task-two.rakumod new file mode 100644 index 0000000000..f871118a42 --- /dev/null +++ b/challenge-241/rcmlz/raku/task-two.rakumod @@ -0,0 +1,18 @@ +unit module rcmlz::raku::task-two:ver<0.0.1>:auth:api<1>; + +use Prime::Factor; + +# run in terminal: raku --optimize=3 -I challenge-nr241/rcmlz/raku/ -- test/challenge-nr241/raku/task-two.rakutest +# or raku --optimize=3 -I challenge-nr241 -- test/benchmark-scalability.raku --task=task-two --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr241; cat /tmp/nr241_task-two.csv + +#|[ +You are given an array of unique positive integers greater than 2. + +- Write a script to sort them in ascending order of the count of their prime factors, tie-breaking by ascending value. +] +our sub solution(@input) is export { + @input ==> map( -> $n { $n => prime-factors($n).elems } ) + ==> sort( { my Order $o = $^a.value cmp $^b.value; + $o == Same ?? $^a.key cmp $^b.key !! $o } ) + ==> map( *.key ) +} \ No newline at end of file -- cgit From dd25c97a9fac61cef72171d769b678aa6e6b10f8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:46:25 +0000 Subject: Challenge 241 Solutions (Raku) --- challenge-241/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-241/mark-anderson/raku/ch-2.raku | 10 ++++++++++ 2 files changed, 23 insertions(+) create mode 100644 challenge-241/mark-anderson/raku/ch-1.raku create mode 100644 challenge-241/mark-anderson/raku/ch-2.raku diff --git a/challenge-241/mark-anderson/raku/ch-1.raku b/challenge-241/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..1c0c7e1b65 --- /dev/null +++ b/challenge-241/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +is arithmetic-triplets([0,1,4,6,7,10], 3), 2; +is arithmetic-triplets([4,5,6,7,8,9], 2), 2; + +sub arithmetic-triplets(@a, $diff) +{ + + @a.combinations(3).grep( + { + [==] -$diff, |.rotor(2 => -1).map({ [-] $_ }) + }) +} diff --git a/challenge-241/mark-anderson/raku/ch-2.raku b/challenge-241/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..147c746cdd --- /dev/null +++ b/challenge-241/mark-anderson/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Prime::Factor; +use Test; + +is-deeply prime-order(11,8,27,4), (11,4,8,27); + +sub prime-order +{ + @_.sort({ prime-factors($_).elems, $_ }) +} -- cgit From 6c6a0dd2ceeb1f6471948c257f8c16d814adcbae Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 30 Oct 2023 15:02:48 +0100 Subject: feat(challenge-241/lubos-kolouch/perl,python,raku,blog/): Challenge 241 LK Perl Python Raku Blog --- challenge-241/lubos-kolouch/blog.txt | 1 + challenge-241/lubos-kolouch/perl/ch-1.pl | 23 +++++++++++++++++++++++ challenge-241/lubos-kolouch/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ challenge-241/lubos-kolouch/python/ch-1.py | 17 +++++++++++++++++ challenge-241/lubos-kolouch/python/ch-2.py | 25 +++++++++++++++++++++++++ challenge-241/lubos-kolouch/raku/ch-1.raku | 18 ++++++++++++++++++ challenge-241/lubos-kolouch/raku/ch-2.raku | 26 ++++++++++++++++++++++++++ 7 files changed, 138 insertions(+) create mode 100644 challenge-241/lubos-kolouch/blog.txt create mode 100644 challenge-241/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-241/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-241/lubos-kolouch/python/ch-1.py create mode 100644 challenge-241/lubos-kolouch/python/ch-2.py create mode 100644 challenge-241/lubos-kolouch/raku/ch-1.raku create mode 100644 challenge-241/lubos-kolouch/raku/ch-2.raku diff --git a/challenge-241/lubos-kolouch/blog.txt b/challenge-241/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..c2c404f049 --- /dev/null +++ b/challenge-241/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-10-30_Weekly_challenge_241 diff --git a/challenge-241/lubos-kolouch/perl/ch-1.pl b/challenge-241/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..3e9b91de92 --- /dev/null +++ b/challenge-241/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; + +sub count_arithmetic_triplets { + my ($nums_ref, $diff) = @_; + my @nums = @{$nums_ref}; + my $count = 0; + my %nums_set = map { $_ => 1 } @nums; + + for my $num (@nums) { + if (exists $nums_set{$num + $diff} && exists $nums_set{$num - $diff}) { + $count++; + } + } + + return $count; +} + +# Test Cases +print count_arithmetic_triplets([0, 1, 4, 6, 7, 10], 3); # Should print 2 +print "\n"; +print count_arithmetic_triplets([4, 5, 6, 7, 8, 9], 2); # Should print 2 +print "\n"; diff --git a/challenge-241/lubos-kolouch/perl/ch-2.pl b/challenge-241/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..cda92ab4d3 --- /dev/null +++ b/challenge-241/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + +sub prime_factors { + my $n = shift; + my @factors; + while ($n % 2 == 0) { + push @factors, 2; + $n /= 2; + } + for (my $i = 3; $i <= sqrt($n); $i += 2) { + while ($n % $i == 0) { + push @factors, $i; + $n /= $i; + } + } + push @factors, $n if $n > 2; + return \@factors; +} + +sub sort_by_prime_factors { + my @nums = @_; + my @sorted_nums = sort { scalar(@{prime_factors($a)}) <=> scalar(@{prime_factors($b)}) || $a <=> $b } @nums; + return \@sorted_nums; +} + +# Test Cases +print join(", ", @{sort_by_prime_factors(11, 8, 27, 4)}), "\n"; # Output: 11, 4, 8, 27 diff --git a/challenge-241/lubos-kolouch/python/ch-1.py b/challenge-241/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..d6aab961be --- /dev/null +++ b/challenge-241/lubos-kolouch/python/ch-1.py @@ -0,0 +1,17 @@ +from typing import List + + +def count_arithmetic_triplets(nums: list[int], diff: int) -> int: + count = 0 + nums_set = set(nums) + + for num in nums: + if num + diff in nums_set and num - diff in nums_set: + count += 1 + + return count + + +# Test Cases +assert count_arithmetic_triplets([0, 1, 4, 6, 7, 10], 3) == 2 +assert count_arithmetic_triplets([4, 5, 6, 7, 8, 9], 2) == 2 diff --git a/challenge-241/lubos-kolouch/python/ch-2.py b/challenge-241/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2f45bd5248 --- /dev/null +++ b/challenge-241/lubos-kolouch/python/ch-2.py @@ -0,0 +1,25 @@ +import math + + +def prime_factors(n): + factors = [] + while n % 2 == 0: + factors.append(2) + n //= 2 + for i in range(3, int(math.sqrt(n)) + 1, 2): + while n % i == 0: + factors.append(i) + n //= i + if n > 2: + factors.append(n) + return factors + + +def sort_by_prime_factors(nums): + nums_with_factors = [(len(prime_factors(num)), num) for num in nums] + nums_with_factors.sort() + return [num for _, num in nums_with_factors] + + +# Test Cases +print(sort_by_prime_factors([11, 8, 27, 4])) # Output: [11, 4, 8, 27] diff --git a/challenge-241/lubos-kolouch/raku/ch-1.raku b/challenge-241/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..627efd8f8a --- /dev/null +++ b/challenge-241/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,18 @@ +sub count-arithmetic-triplets(@nums, $diff) { + my $count = 0; + my %nums-set = @nums.Set; + + for @nums -> $num { + if %nums-set{$num + $diff}:exists { + if %nums-set{$num - $diff}:exists { + $count++; + } + } + } + + return $count; +} + +# Test Cases +say count-arithmetic-triplets([0, 1, 4, 6, 7, 10], 3); # Should print 2 +say count-arithmetic-triplets([4, 5, 6, 7, 8, 9], 2); # Should print 2 diff --git a/challenge-241/lubos-kolouch/raku/ch-2.raku b/challenge-241/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..5335aa0cab --- /dev/null +++ b/challenge-241/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,26 @@ +# Function to get the prime factors of a number +sub prime-factors(Int $n is copy) { + my @factors; + while $n %% 2 { + @factors.push: 2; + $n div= 2; + } + for 3, 5 ... * -> $i { + last if $i > sqrt($n); + while $n %% $i { + @factors.push: $i; + $n div= $i; + } + } + @factors.push: $n if $n > 2; + return @factors; +} + +# Function to sort numbers by the count of their prime factors +sub sort-by-prime-factors(@nums) { + my @sorted-nums = @nums.sort: { prime-factors($_).elems, $_ }; + return @sorted-nums; +} + +# Test Cases +say sort-by-prime-factors([11, 8, 27, 4]); # Output: [11, 4, 8, 27] -- cgit From 78d2196554ebb81ecbcdc2e056e477497675c1ca Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 30 Oct 2023 10:13:50 -0600 Subject: Solve PWC241 --- challenge-241/wlmb/blog.txt | 1 + challenge-241/wlmb/perl/ch-1.pl | 17 +++++++++++++++++ challenge-241/wlmb/perl/ch-2.pl | 12 ++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 challenge-241/wlmb/blog.txt create mode 100755 challenge-241/wlmb/perl/ch-1.pl create mode 100755 challenge-241/wlmb/perl/ch-2.pl diff --git a/challenge-241/wlmb/blog.txt b/challenge-241/wlmb/blog.txt new file mode 100644 index 0000000000..82867ba55b --- /dev/null +++ b/challenge-241/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/10/30/PWC241/ diff --git a/challenge-241/wlmb/perl/ch-1.pl b/challenge-241/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..1dd1be854b --- /dev/null +++ b/challenge-241/wlmb/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +# Perl weekly challenge 241 +# Task 1: Arithmetic Triplets +# +# See https://wlmb.github.io/2023/10/30/PWC241/#task-1-arithmetic-triplets +use v5.36; +use List::Util qw(all); +use Algorithm::Combinatorics qw(combinations); +die <<~"FIN" if @ARGV<2; + Usage: $0 D N0 [N1...] + to count the triplets taken from N0 N1... with difference D between succesive terms; + FIN +my $diff=shift; +my @sorted=sort {$a <=> $b} @ARGV; +die "Array should be ordered" unless all {$sorted[$_]==$ARGV[$_]} 0..@ARGV-1; +say "Nums: @sorted, diff: $diff -> ", + 0+grep{$_->[1]-$_->[0]==$_->[2]-$_->[1]==$diff} combinations([@sorted],3) diff --git a/challenge-241/wlmb/perl/ch-2.pl b/challenge-241/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..daa9d93bfd --- /dev/null +++ b/challenge-241/wlmb/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +# Perl weekly challenge 241 +# Task 2: Prime Order +# +# See https://wlmb.github.io/2023/10/30/PWC241/#task-2-prime-order +use v5.36; +use Math::Prime::Util qw(factor); +die <<~"FIN" unless @ARGV; + Usage: $0 N0 [N1...] + to order the numbers N0 N1 according to the number of factors and their value. + FIN +say join " ", @ARGV, "->", sort{factor($a)<=>factor($b) || $a <=> $b} @ARGV -- cgit From 193f81ec4afbfcc448a7695f81eef0e2193c804d Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 30 Oct 2023 12:16:27 -0400 Subject: Week 241 --- challenge-241/zapwai/perl/ch-1.pl | 24 ++++++++++++++++ challenge-241/zapwai/perl/ch-2.pl | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-241/zapwai/perl/ch-1.pl create mode 100644 challenge-241/zapwai/perl/ch-2.pl diff --git a/challenge-241/zapwai/perl/ch-1.pl b/challenge-241/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..8bab017a33 --- /dev/null +++ b/challenge-241/zapwai/perl/ch-1.pl @@ -0,0 +1,24 @@ +use v5.30; +my @nums = (0, 1, 4, 6, 7, 10); +my $diff = 3; +print "Input: \@nums = (". join(", ", @nums) . ")"; +say "\t\$diff = $diff"; +my ($cnt,$output); +foreach my $i (0 .. $#nums - 2) { + foreach my $j ($i + 1 .. $#nums - 1) { + foreach my $k ($j + 1 .. $#nums) { + if ( is_trip($diff, $nums[$i],$nums[$j],$nums[$k]) ) { + $output .= "($nums[$i],$nums[$j],$nums[$k])\n"; + $cnt++; + } + } + } +} +say "Output: $cnt"; +chomp $output; +say $output; +sub is_trip { + my ($diff, $a, $b, $c) = @_; + return 1 if ( ($b - $a == $diff) && ($c - $b == $diff) ); + 0 +} diff --git a/challenge-241/zapwai/perl/ch-2.pl b/challenge-241/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..fed80beca2 --- /dev/null +++ b/challenge-241/zapwai/perl/ch-2.pl @@ -0,0 +1,59 @@ +use v5.30; +my @int = (11, 8, 27, 4); +say "Input: \@int = (". join(", ", @int) . ")"; +my $max = 0; +foreach my $v (@int) { + $max = $v if ($v > $max); +} +my @prime = myprimes($max); +my @factor = map {factors($_)} @int; +my @ans = @int; +my $flag = 0; +do { + $flag = 0; + foreach my $i (0 .. $#factor - 1) { + if ( ($factor[$i] > $factor[$i + 1]) || + ( ($factor[$i] == $factor[$i+1]) && ($ans[$i] > $ans[$i+1]) ) + ) { + $flag++; + my $aval = $ans[$i]; + my $fval = $factor[$i]; + $ans[$i] = $ans[$i+1]; + $ans[$i + 1] = $aval; + $factor[$i] = $factor[$i+1]; + $factor[$i+1] = $fval; + } + } +} while ($flag); +print "Output: ("; +say join(", ",@ans) . ")"; +sub factors { + my $num = shift; + my @list; + do { + foreach my $factor (@prime) { + if ( $num % $factor == 0 ) { + push @list, $factor; + $num = $num / $factor; + } + } + } while ($num != 1); + return $#list+1; +} + +sub myprimes { + my $N = shift; + my @p = (2); + for my $i (3 .. $N) { + push @p, $i if is_prime($i); + } + @p +} + +sub is_prime { + my $num = shift; + for my $i (2 .. sqrt($num)) { + return 0 if ( $num % $i == 0 ); + } + 1 +} -- cgit From 8ffb545124ff09e62563486bda8768e27a1b3bc7 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Oct 2023 21:41:28 +0200 Subject: PWC 241 Task 1 Raku done Task 2 in Raky done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Python task 1 done Python task 2 done --- challenge-241/luca-ferrari/blog-1.txt | 1 + challenge-241/luca-ferrari/blog-2.txt | 1 + challenge-241/luca-ferrari/blog-3.txt | 1 + challenge-241/luca-ferrari/blog-4.txt | 1 + challenge-241/luca-ferrari/blog-5.txt | 1 + challenge-241/luca-ferrari/blog-6.txt | 1 + challenge-241/luca-ferrari/blog-7.txt | 1 + challenge-241/luca-ferrari/blog-8.txt | 1 + challenge-241/luca-ferrari/postgresql/ch-1.plperl | 29 ++++++++++++ challenge-241/luca-ferrari/postgresql/ch-1.sql | 30 ++++++++++++ challenge-241/luca-ferrari/postgresql/ch-2.plperl | 40 ++++++++++++++++ challenge-241/luca-ferrari/postgresql/ch-2.sql | 57 +++++++++++++++++++++++ challenge-241/luca-ferrari/python/ch-1.py | 33 +++++++++++++ challenge-241/luca-ferrari/python/ch-2.py | 43 +++++++++++++++++ challenge-241/luca-ferrari/raku/ch-1.p6 | 32 +++++++++++++ challenge-241/luca-ferrari/raku/ch-2.p6 | 35 ++++++++++++++ 16 files changed, 307 insertions(+) create mode 100644 challenge-241/luca-ferrari/blog-1.txt create mode 100644 challenge-241/luca-ferrari/blog-2.txt create mode 100644 challenge-241/luca-ferrari/blog-3.txt create mode 100644 challenge-241/luca-ferrari/blog-4.txt create mode 100644 challenge-241/luca-ferrari/blog-5.txt create mode 100644 challenge-241/luca-ferrari/blog-6.txt create mode 100644 challenge-241/luca-ferrari/blog-7.txt create mode 100644 challenge-241/luca-ferrari/blog-8.txt create mode 100644 challenge-241/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-241/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-241/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-241/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-241/luca-ferrari/python/ch-1.py create mode 100644 challenge-241/luca-ferrari/python/ch-2.py create mode 100644 challenge-241/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-241/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-241/luca-ferrari/blog-1.txt b/challenge-241/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e6ea04a724 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task1 diff --git a/challenge-241/luca-ferrari/blog-2.txt b/challenge-241/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..b8f8581c56 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task2 diff --git a/challenge-241/luca-ferrari/blog-3.txt b/challenge-241/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..302ebacb19 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task1plperl diff --git a/challenge-241/luca-ferrari/blog-4.txt b/challenge-241/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..754961b9e0 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task2plperl diff --git a/challenge-241/luca-ferrari/blog-5.txt b/challenge-241/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..7d8241f08e --- /dev/null +++ b/challenge-241/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task1plpgsql diff --git a/challenge-241/luca-ferrari/blog-6.txt b/challenge-241/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..210fba1cd4 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task2plpgsql diff --git a/challenge-241/luca-ferrari/blog-7.txt b/challenge-241/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..ee3de8bd65 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task1python diff --git a/challenge-241/luca-ferrari/blog-8.txt b/challenge-241/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..023db6d660 --- /dev/null +++ b/challenge-241/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/30/PerlWeeklyChallenge241.html#task2python diff --git a/challenge-241/luca-ferrari/postgresql/ch-1.plperl b/challenge-241/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..4f9ba64e8b --- /dev/null +++ b/challenge-241/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 241 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc241; + +CREATE OR REPLACE FUNCTION +pwc241.task1_plperl( int[], int ) +RETURNS int +AS $CODE$ + my ( $nums, $diff ) = @_; + my @triplets; + + for my $i ( 0 .. $nums->@* - 1) { + for my $j ( $i + 1 .. $nums->@* - 1) { + for my $k ( $j + 1 .. $nums->@* - 1) { + if ( ( $nums->[ $k ] - $nums->[ $j ] ) == ( $nums->[ $j ] - $nums->[ $i ] ) + && ( $nums->[ $k ] - $nums->[ $j ] ) == $diff ) { + push @triplets, [ $nums->[ $k ], $nums->[ $j ], $nums->[ $i ] ]; + } + } + } + } + + return scalar @triplets; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-241/luca-ferrari/postgresql/ch-1.sql b/challenge-241/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..ee3f43ed73 --- /dev/null +++ b/challenge-241/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 241 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc241; + +CREATE OR REPLACE FUNCTION +pwc241.task1_plpgsql( nums int[], diff int ) +RETURNS int +AS $CODE$ +DECLARE + counter int := 0; +BEGIN + FOR i IN 1 .. array_length( nums, 1 ) LOOP + FOR j IN ( i + 1 ) .. array_length( nums, 1 ) LOOP + FOR k IN ( j + 1 ) .. array_length( nums, 1 ) LOOP + IF ( nums[ k ] - nums[ j ] ) = ( nums[ j ] - nums[ i ] ) AND ( nums[ k ] - nums[ j ] = diff ) THEN + counter := counter + 1; + END IF; + END LOOP; + END LOOP; + END LOOP; + + RETURN counter; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-241/luca-ferrari/postgresql/ch-2.plperl b/challenge-241/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..a0bd3cf677 --- /dev/null +++ b/challenge-241/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,40 @@ +-- +-- Perl Weekly Challenge 241 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc241; + +CREATE OR REPLACE FUNCTION +pwc241.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + my ( $nums ) = @_; + + my $factors = sub { + my ( $n ) = @_; + my $primes = []; + + for ( 2 .. $n ) { + while ( $n % $_ == 0 ) { + push $primes->@*, $_; + $n /= $_; + } + + last if $n == 1; + } + + return $primes; + }; + + my $sorted = {}; + push $sorted->{ scalar( $factors->( $_ )->@* ) }->@*, $_ for ( $nums->@* ); + + for my $key ( sort keys $sorted->%* ) { + return_next( $_ ) for ( $sorted->{ $key }->@* ); + } + +return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-241/luca-ferrari/postgresql/ch-2.sql b/challenge-241/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..9c188cc24d --- /dev/null +++ b/challenge-241/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,57 @@ +-- +-- Perl Weekly Challenge 241 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc241; + + +CREATE OR REPLACE FUNCTION +pwc241.primes( n int ) +RETURNS SETOF int +AS $CODE$ +BEGIN + FOR i IN 2 .. n LOOP + WHILE ( n % i ) = 0 LOOP + RETURN NEXT i; + n := n / i; + END LOOP; + + + IF n = 1 THEN + RETURN; + END IF; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION +pwc241.task2_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + v int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS sorted( n int, p int DEFAULT 0 ); + TRUNCATE sorted; + + FOREACH v IN ARRAY nums LOOP + INSERT INTO sorted( n, p ) + SELECT v, pp + FROM pwc241.primes( v ) pp; + END LOOP; + + RETURN QUERY + WITH q( n ) AS ( SELECT n FROM sorted ORDER BY p asc ) + SELECT distinct n + FROM Q qq; + + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-241/luca-ferrari/python/ch-1.py b/challenge-241/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..e717b52b6e --- /dev/null +++ b/challenge-241/luca-ferrari/python/ch-1.py @@ -0,0 +1,33 @@ +#!python + +# +# Perl Weekly Challenge 241 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + diff = int(argv[ 0 ]) + nums = list( map( int, argv[ 1: ] ) ) + counter = 0 + + for i in range( 0, len( nums ) ): + for j in range( i + 1, len( nums ) ): + for k in range( j + 1, len( nums ) ): + if ( ( nums[ k ] - nums[ j ] ) == ( nums[ j ] - nums[ i ] ) and ( nums[ k ] - nums[ j ] ) == diff ): + counter += 1 + + print( counter ) + return counter + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-241/luca-ferrari/python/ch-2.py b/challenge-241/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..824d187689 --- /dev/null +++ b/challenge-241/luca-ferrari/python/ch-2.py @@ -0,0 +1,43 @@ +#!python + +# +# Perl Weekly Challenge 241 +# Task 2 +# +# See +# + +import sys + +def get_primes( n ): + primes = [] + for i in range(2, n): + while n % i == 0: + primes.append( i ) + n /= i + + if n == 1: + break + + return primes + +# task implementation +def main( argv ): + sorted_dict = {} + for current in argv: + counter = len( get_primes( int( current ) ) ) + if not counter in sorted_dict: + sorted_dict[ counter ] = [] + + sorted_dict[ counter ].append( current ) + + for key in sorted( sorted_dict ): + for v in sorted_dict[ key ]: + print( v ) + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-241/luca-ferrari/raku/ch-1.p6 b/challenge-241/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..51223a532a --- /dev/null +++ b/challenge-241/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,32 @@ +#!raku + +# +# Perl Weekly Challenge 241 +# Task 1 +# +# See +# + +sub MAIN( Int :$diff + , Bool :$verbose = True + , *@nums where { @nums.grep( * ~~ Int ).Array.elems == @nums.elems } + ) { + + my @triplets; + # a) i < j < k + # b) nums[j] - nums[i] == diff + # c) nums[k] - nums[j] == diff + for 0 ..^ @nums.elems -> $i { + for $i ^..^ @nums.elems -> $j { + for $j ^..^ @nums.elems -> $k { + @triplets.push: [ @nums[ $k, $j, $i ] ] if ( ( @nums[ $j ] - @nums[ $i ] ) == ( @nums[ $k ] - @nums[ $j ] ) + && ( @nums[ $k ] - @nums[ $j ] ) == $diff ); + } + } + } + + + @triplets.elems.say; + @triplets.join( "\n" ).say; + +} diff --git a/challenge-241/luca-ferrari/raku/ch-2.p6 b/challenge-241/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..a48c56071d --- /dev/null +++ b/challenge-241/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,35 @@ +#!raku + +# +# Perl Weekly Challenge 241 +# Task 2 +# +# See +# + + +sub prime_factors (Int $n) { + return $n if $n <= 1; + my $residue = $n; + my @factors; + + for 2 .. $n { + while ( $residue %% $_ ) { + @factors.push: $_; + $residue /= $_; + } + + last if $residue == 1; + } + + return @factors; +} + +sub MAIN( *@nums where { @nums.grep( * ~~Int ).elems == @nums.elems } ) { + my %sorted; + for @nums { + %sorted{ prime_factors( $_.Int ).elems }.push: $_; + } + + %sorted{ $_ }.join( "," ).say for %sorted.keys.sort; +} -- cgit From 0f50b6a35bf1bcd4d4f1393f3f62a5a054d845f4 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 30 Oct 2023 17:41:58 +0000 Subject: Week 241 ... --- challenge-241/peter-campbell-smith/blog.txt | 1 + challenge-241/peter-campbell-smith/perl/ch-1.pl | 71 +++++++++++++++ challenge-241/peter-campbell-smith/perl/ch-2.pl | 114 ++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 challenge-241/peter-campbell-smith/blog.txt create mode 100755 challenge-241/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-241/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-241/peter-campbell-smith/blog.txt b/challenge-241/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..5723b19b21 --- /dev/null +++ b/challenge-241/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/241 diff --git a/challenge-241/peter-campbell-smith/perl/ch-1.pl b/challenge-241/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..3324057a01 --- /dev/null +++ b/challenge-241/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-10-30 +use utf8; # Week 241 task 1 - Arithmetic triplets +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +arithmetic_triplets ([0, 1, 4, 6, 7, 10], 3); +arithmetic_triplets ([(4, 5, 6, 7, 8, 9)], 2); + +# generate 200 sorted unique numbers in (0 .. 1999) +my ($j, @nums, $next, $count, @used); +$count = 0; +while ($count < 200) { + $next = int(rand(2000)); + next if $used[$next]; + push(@nums, $next); + $count ++; + $used[$next] = 1; +} +@nums = sort { $a <=> $b } @nums; +arithmetic_triplets (\@nums, 19); + +sub arithmetic_triplets { + + my (@nums, $diff, $last, $i, $j, $k, $count, $explain, $ji_diff, $kj_diff); + + # initialise + @nums = @{$_[0]}; + $diff = $_[1]; + $last = @nums - 1; + + # loop over triplets + $explain = ''; + $count = 0; + + # loop over i any i (except the last 2) could be part of a triplet + for $i (0 .. $last - 2) { + + # loop over j + for $j (1 .. $last - 1) { + $ji_diff = $nums[$j] - $nums[$i]; + + # if they differ by more than $diff then we can abandon this j + last if $ji_diff > $diff; + + # unless this pair of i and j differ by $diff there's no need to check k + next unless $ji_diff == $diff; + + # loop over k + for $k (2 .. $last) { + $kj_diff = $nums[$k] - $nums[$j]; + + # we can abandon this k if k differs from j by more than $diff + last if ($kj_diff) > $diff; + + # and at last we've found an answer! + if ($kj_diff == $diff) { + $count ++; + $explain .= qq{ \$nums[$i] = $nums[$i], \$nums[$j] = $nums[$j], \$nums[$k] = $nums[$k]\n}; + } + } + } + } + + # show results + say qq[\nInput: \@nums = (] . join(q[, ], @nums) . q[)]; + say qq[ \$diff = $diff]; + say qq[Output: $count\n] . ($explain ? substr($explain, 0, -1) : ''); +} + \ No newline at end of file diff --git a/challenge-241/peter-campbell-smith/perl/ch-2.pl b/challenge-241/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..c6af7e9fe3 --- /dev/null +++ b/challenge-241/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,114 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-10-30 +use utf8; # Week 241 task 2 - Prime order +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +my (@sieve, $j, @int, $next, $count, @used); + +prime_order(11, 8, 27, 4); + +# bigger example: 30 unique numbers in (2 .. 500) +$count = 0; +while ($count < 30) { + $next = int(rand(501)); + next if $used[$next] or $next < 2; + push(@int, $next); + $count ++; + $used[$next] = 1; +} +prime_order(@int); + +sub prime_order { + + my (@int, $largest, $i, $count, $list, %output, $k, $explain, $ordered); + + # initialise + @int = @_; + + # find the largest and create sieve of Eratosthenes + $largest = 0; + for $i (@int) { + $largest = $i if $i > $largest; + } + make_sieve($largest); + + # loop over @int, get prime factors and provide key to sort in desired order + for ($i = 0; $i < @int; $i ++) { + ($count, $list) = prime_factors($int[$i]); + $output{sprintf('%08d~%08d', $count, $int[$i])} = $list; + } + + # extract sorted results and prepare to display + $explain = ''; + $ordered = ''; + for $k (sort keys %output) { + $k =~ m|(\d+)~(\d+)|; + $i = $2 + 0; + $ordered .= qq[$i, ]; + $explain .= sprintf(qq[ Prime factors of %3d => %s\n], $i, $output{$k}); + } + + # show results + say qq[\nInput: \@int = (] . join(q[, ], @int) . q[)]; + say qq[Output: (], substr($ordered, 0, -2) . ')'; + say substr($explain, 0, -1); +} + +sub make_sieve { + + my ($arg, $j, $k); + + # set all values provisionally to 1 (ie prime) + $arg = shift; + for $j (0 .. $arg) { + $sieve[$j] = 1; + } + + # for each prime in turn, set its multiples to 0 (ie not prime) + for $j (2 .. $arg) { + next unless $sieve[$j]; # $j is not prime + last if $j ** 2 > $arg; + for $k ($j .. $arg) { + last if $k * $j > $arg; + $sieve[$k * $j] = 0; + } + } +} + +sub prime_factors { + + # returns count and list of prime factors + + my ($arg, $pf, $count, $list, $q, $prime); + + # initialise + $arg = shift; + $pf = ''; + $count = 0; + $list = ''; + + # loop over all primes <= input + for $prime (2 .. $arg) { + next unless $sieve[$prime]; + + # try dividing remaining number repeatedly by each prime + while (1) { + $q = $arg / $prime; + + # found a prime factor - add to count and list + if ($q == int($q)) { + $count ++; + $list .= qq[$prime, ]; + $arg = $q; + + # no more of this prime + } else { + last; + } + } + } + return ($count, substr($list, 0, -2)); +} + \ No newline at end of file -- cgit From 4ee504e83ea5e87a2007472f8672ff9a1f9978bd Mon Sep 17 00:00:00 2001 From: Kjetil Skotheim Date: Mon, 30 Oct 2023 21:50:12 +0100 Subject: challenge-241 --- challenge-241/kjetillll/perl/ch-1.pl | 39 ++++++++++++++++++++++++++++++ challenge-241/kjetillll/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-241/kjetillll/perl/ch-1.pl create mode 100644 challenge-241/kjetillll/perl/ch-2.pl diff --git a/challenge-241/kjetillll/perl/ch-1.pl b/challenge-241/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..09fa327ecf --- /dev/null +++ b/challenge-241/kjetillll/perl/ch-1.pl @@ -0,0 +1,39 @@ +use warnings; use strict; + +#use Algorithm::Combinatorics 'combinations'; #...or just use this one: + +sub comb{my($l,$k,$s)=(@_,0);$k?map{my$i=$_;map[$$l[$i],@$_],comb($l,$k-1,$i+1)}$s..@$l-$k:[]} + +sub triplets { comb(\@_,3) } + +sub number_of_arithmetic_triplets { + my($diff,@array) = @_; + scalar + grep { + my($ni, $nj, $nk) = @$_; + $nj-$ni == $diff and $nk-$nj == $diff + } + triplets(@array) +} + +for my $test ( + { + input_nums => [0, 1, 4, 6, 7, 10], + input_diff => 3, + output_expected => 2 + }, + { + input_nums => [4, 5, 6, 7, 8, 9], + input_diff => 2, + output_expected => 2 + } +){ + my $output_got = number_of_arithmetic_triplets( + $$test{input_diff}, + @{ $$test{input_nums} } + ); + my $result = $output_got == $$test{output_expected} + ? 'ok' + : 'NOT OK'; + print "$result expected: $$test{output_expected} got: $output_got\n"; +} diff --git a/challenge-241/kjetillll/perl/ch-2.pl b/challenge-241/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..c81e8bacc9 --- /dev/null +++ b/challenge-241/kjetillll/perl/ch-2.pl @@ -0,0 +1,46 @@ +use warnings; use strict; + +#use Math::Prime::Util 'factor' #...or this simpler but suboptimal one: + +sub factor { #returns the list of an ints prime factors + my($n) = @_; + my $factor; + $n % $_ == 0 and $factor = $_ and last for 2 .. sqrt $n; + $factor ? ( $factor, factor($n/$factor) ) : $n +} + +sub number_of_prime_factors { my @factor = factor(@_); scalar @factor } + +sub prime_order { + map $$_{value}, + sort { + $$a{sort_by} <=> $$b{sort_by} or + $$a{value} <=> $$b{value} + } + map {value=>$_, sort_by=>number_of_prime_factors($_)}, + @_ +} + +for my $test ( + { + input => [11, 8, 27, 4], + output_expected => [11, 4, 8, 27] + }, + { + input => [ + 2 * 3 * 4 * 5, #120 + 2 ** 8, #256 + 7 * 7 * 7, #343 + 3 * 7 * 11, #231 + 2 * 7 * 13, #182 + 19 * 23, #437 + ], + output_expected => [437, 182,231,343,120,256] + } +){ + my @output_got = prime_order( @{ $$test{input} } ); + print "@output_got" eq "@{ $$test{output_expected} }" + ? 'ok' + : 'NOT OK'; + print " got: @output_got\n"; +} -- cgit From 21ca935e0574358137f6cac6f08ec538ccb7ccad Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 30 Oct 2023 18:00:11 -0400 Subject: DAJ 241 --- challenge-241/dave-jacoby/perl/ch-1.pl | 42 ++++++++++++++++++++++++++ challenge-241/dave-jacoby/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 challenge-241/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-241/dave-jacoby/perl/ch-2.pl diff --git a/challenge-241/dave-jacoby/perl/ch-1.pl b/challenge-241/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..5dc464d0a8 --- /dev/null +++ b/challenge-241/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + { nums => [ 0, 1, 4, 6, 7, 10 ], diff => 3, }, + { nums => [ 4, 5, 6, 7, 8, 9 ], diff => 2, } +);