diff options
| author | Packy Anderson <packy@cpan.org> | 2023-10-31 02:06:14 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2023-10-31 02:06:14 -0400 |
| commit | 886b69a2696ffd4a134e1aab4bbf30be5f2cbd4d (patch) | |
| tree | 7a058d3d9594ad3fc0f71e4937c3e20da0d41a8b /challenge-241 | |
| parent | 2a34bfaf2ba34539535040875128b7e5148f9d47 (diff) | |
| download | perlweeklychallenge-club-886b69a2696ffd4a134e1aab4bbf30be5f2cbd4d.tar.gz perlweeklychallenge-club-886b69a2696ffd4a134e1aab4bbf30be5f2cbd4d.tar.bz2 perlweeklychallenge-club-886b69a2696ffd4a134e1aab4bbf30be5f2cbd4d.zip | |
Challenge 241 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
Diffstat (limited to 'challenge-241')
| -rw-r--r-- | challenge-241/packy-anderson/README.md | 120 | ||||
| -rw-r--r-- | challenge-241/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/perl/ch-2.pl | 75 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/python/ch-2.py | 56 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/raku/ch-1.raku | 33 | ||||
| -rwxr-xr-x | challenge-241/packy-anderson/raku/ch-2.raku | 74 |
8 files changed, 374 insertions, 53 deletions
diff --git a/challenge-241/packy-anderson/README.md b/challenge-241/packy-anderson/README.md index 5e38fa1145..f92e561e12 100644 --- a/challenge-241/packy-anderson/README.md +++ b/challenge-241/packy-anderson/README.md @@ -1,95 +1,109 @@ # 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") -Output: true +Input: @nums = (0, 1, 4, 6, 7, 10) + $diff = 3 +Output: 2 -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "a" . "bc" => "abc" - -Example 2: -Input: @arr1 = ("ab", "c") - @arr2 = ("ac", "b") -Output: false +(1, 2, 4) is an arithmetic triplet because both 7 - 4 = 3 and 4 - 1 = 3 +(2, 4, 5) is an arithmetic triplet because both 10 - 7 = 3 and 7 - 4 = 3 -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "ac" . "b" => "acb" -Example 3: -Input: @arr1 = ("ab", "cd", "e") - @arr2 = ("abcde") -Output: true +Example 2: +Input: @nums = (4, 5, 6, 7, 8, 9) + $diff = 2 +Output: 2 -Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" -Using @arr2, word2 => "abcde" => "abcde" +(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 ``` -* [Task 2](perl/ch-2.pl) +* [Task 2](raku/ch-2.raku) Sample output ``` +$ raku/ch-2.raku +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 + +Example 2: +Input: @int = (2, 4, 8, 12, 11) +Output: (2, 11, 4, 8, 12) + +Prime factors of 2 => 2 +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 12 => 2, 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") -Output: true +Input: @nums = (0, 1, 4, 6, 7, 10) + $diff = 3 +Output: 2 -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "a" . "bc" => "abc" +(1, 2, 4) is an arithmetic triplet because both 7 - 4 = 3 and 4 - 1 = 3 +(2, 4, 5) is an arithmetic triplet because both 10 - 7 = 3 and 7 - 4 = 3 -Example 2: -Input: @arr1 = ("ab", "c") - @arr2 = ("ac", "b") -Output: false - -Using @arr1, word1 => "ab" . "c" => "abc" -Using @arr2, word2 => "ac" . "b" => "acb" -Example 3: -Input: @arr1 = ("ab", "cd", "e") - @arr2 = ("abcde") -Output: true +Example 2: +Input: @nums = (4, 5, 6, 7, 8, 9) + $diff = 2 +Output: 2 -Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" -Using @arr2, word2 => "abcde" => "abcde" +(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 ``` -* [Task 2](raku/ch-2.raku) +* [Task 2](perl/ch-2.pl) Sample output ``` +$ perl/ch-2.pl +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 + +Example 2: +Input: @int = (2, 4, 8, 12, 11) +Output: (2, 11, 4, 8, 12) + +Prime factors of 2 => 2 +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 12 => 2, 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: Triplets Prime](https://packy.dardan.com/2023/10/30/perl-weekly-challenge-triplets-prime/) diff --git a/challenge-241/packy-anderson/blog.txt b/challenge-241/packy-anderson/blog.txt new file mode 100644 index 0000000000..df72939a19 --- /dev/null +++ b/challenge-241/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/10/30/perl-weekly-challenge-triplets-prime/
\ No newline at end of file diff --git a/challenge-241/packy-anderson/perl/ch-1.pl b/challenge-241/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..fc32b9e6a0 --- /dev/null +++ b/challenge-241/packy-anderson/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use v5.38; + +sub findTriplets { + my($diff, $nums) = @_; + my $count = 0; + my $details = q{}; + foreach my $i ( 0 .. $#$nums - 2 ) { + foreach my $j ( $i + 1 .. $#$nums - 1 ) { + next unless $nums->[$j] - $nums->[$i] == $diff; + foreach my $k ( $j + 1 .. $#$nums ) { + next unless $nums->[$k] - $nums->[$j] == $diff; + $count++; + $details .= "($i, $j, $k) is an arithmetic triplet " + . "because both $nums->[$k] - $nums->[$j] = $diff " + . "and $nums->[$j] - $nums->[$i] = $diff\n"; + } + } + } + return ($count, $details); +} + +sub solution { + my($diff, $nums) = @_; + say 'Input: @nums = (' . join(', ', @$nums) . ')'; + say ' $diff = ' . $diff; + my ($count, $details) = findTriplets($diff, $nums); + say "Output: $count\n\n$details"; +} + +say "Example 1:"; +solution(3, [0, 1, 4, 6, 7, 10]); + +say "\nExample 2:"; +solution(2, [4, 5, 6, 7, 8, 9]);
\ No newline at end of file diff --git a/challenge-241/packy-anderson/perl/ch-2.pl b/challenge-241/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..bddee1f7b8 --- /dev/null +++ b/challenge-241/packy-anderson/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl +use v5.38; + +sub findPrimeFactors { + my $num = shift; + my @factors; + while ( $num % 2 == 0 ) { + push @factors, 2; + $num /= 2; + } + foreach my $i ( 3 .. int(sqrt($num)) ) { + while ( $num % $i == 0 ) { + push @factors, $i; + $num /= $i; + } + } + if ($num > 2) { + push @factors, $num; + } + return @factors; +} + +sub sortByPrimeFactors { + my @int = @_; + my %primeFactors; + # calculate the prime factors for each number + foreach my $n ( @int ) { + $primeFactors{$n} = [ findPrimeFactors($n) ]; + } + # now sort the numbers + my @sorted = sort { + # first, sort by number of factors + $#{$primeFactors{$a}} <=> $#{$primeFactors{$b}} + || + # then sort by the value of the factors + listCompare($primeFactors{$a}, $primeFactors{$b}) + } @int; + # now build the output + my $factors = q{}; + foreach my $n ( @sorted ) { + $factors .= sprintf 'Prime factors of %2d => ', $n; + $factors .= join(', ', @{$primeFactors{$n}}) . "\n"; + } + return \@sorted, $factors; +} + +sub listCompare($a, $b) { + # this is only getting called if both lists + # have the same number of elements + my $i = 0; + + # compare the corresponding element from each + # list until they're unequal + while ($i <= $#{$a} && $a->[$i] == $b->[$i]) { + $i++; + } + # if we ran off the end of the list, set $i to 0 + $i = 0 if $i > $#{$a}; + + return $a->[$i] <=> $b->[$i]; +} + +sub solution(@int) { + say 'Input: @int = (' . join(', ', @int) . ')'; + my ($sorted, $factors) = sortByPrimeFactors(@int); + say 'Output: (' . join(', ', @$sorted) . ')'; + say ''; + say $factors; +} + +say "Example 1:"; +solution(11, 8, 27, 4); + +say "Example 2:"; +solution(2, 4, 8, 12, 11);
\ No newline at end of file diff --git a/challenge-241/packy-anderson/python/ch-1.py b/challenge-241/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..679faea5cc --- /dev/null +++ b/challenge-241/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def findTriplets(diff, nums): + count = 0 + details = '' + for i in range(0, len(nums) - 2): + for j in range(i + 1, len(nums) - 1): + if not nums[j] - nums[i] == diff: + continue + for k in range(j + 1, len(nums)): + if not nums[k] - nums[j] == diff: + continue + count += 1 + details += ( + f"({i}, {j}, {k}) is an arithmetic " + + f"triplet because both " + + f"{nums[k]} - {nums[j]} = {diff} and " + + f"{nums[j]} - {nums[i]} = {diff}\n" + ) + return count, details + +def solution(diff, nums): + as_list = ', '.join(map(lambda i: str(i), nums)) + print(f'Input: @nums = ({as_list})') + print(f' $diff = {diff}') + (count, details) = findTriplets(diff, nums) + print(f'Output: {count}\n\n{details}') + +print('Example 1:') +solution(3, [0, 1, 4, 6, 7, 10]) + +print('\nExample 2:') +solution(2, [4, 5, 6, 7, 8, 9])
\ No newline at end of file diff --git a/challenge-241/packy-anderson/python/ch-2.py b/challenge-241/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..ec78033d84 --- /dev/null +++ b/challenge-241/packy-anderson/python/ch-2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +import math + +def findPrimeFactors(num): + factors = [] + while num % 2 == 0: + factors.append(2) + num /= 2 + + for i in range(3, int(math.sqrt(num))): + while num % i == 0: + factors.append(i) + num /= i + + if num > 2: + factors.append(int(num)) + + return factors + +def sortByPrimeFactors(nums): + primeFactors = {} + # calculate the prime factors for each number + for n in nums: + primeFactors[n] = findPrimeFactors(n) + # now sort the numbers + sorted_list = sorted(nums, + key=lambda x: ( + len(primeFactors[x]), + primeFactors[x] + ) + ) + + # now build the output + factors = '' + for n in sorted_list: + factors += f'Prime factors of {n:2d} => ' + as_list = ', '.join( + map(lambda i: str(i), primeFactors[n]) + ) + factors += as_list + '\n' + + return sorted_list, factors + +def solution(nums): + as_list = ', '.join(map(lambda i: str(i), nums)) + print(f'Input: @int = ({as_list})') + sorted_list, factors = sortByPrimeFactors(nums) + as_list = ', '.join(map(lambda i: str(i), sorted_list)) + print(f'Output: ({as_list})\n\n{factors}') + +print('Example 1:') +solution([11, 8, 27, 4]) + +print('\nExample 2:') +solution([2, 4, 8, 12, 11])
\ No newline at end of file diff --git a/challenge-241/packy-anderson/raku/ch-1.raku b/challenge-241/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..552b3093d6 --- /dev/null +++ b/challenge-241/packy-anderson/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub findTriplets($diff, @nums) { + my $count = 0; + my $details = q{}; + for 0 .. @nums.elems - 3 -> $i { + for $i + 1 .. @nums.elems - 2 -> $j { + next unless @nums[$j] - @nums[$i] == $diff; + for $j + 1 .. @nums.elems - 1 -> $k { + next unless @nums[$k] - @nums[$j] == $diff; + $count++; + $details ~= "($i, $j, $k) is an arithmetic triplet " + ~ "because both @nums[$k] - @nums[$j] = $diff " + ~ "and @nums[$j] - @nums[$i] = $diff\n"; + } + } + } + return ($count, $details); +} + +sub solution($diff, @nums) { + say 'Input: @nums = (' ~ @nums.join(', ') ~ ')'; + say ' $diff = ' ~ $diff; + my ($count, $details) = findTriplets($diff, @nums); + say "Output: $count\n\n$details"; +} + +say "Example 1:"; +solution(3, [0, 1, 4, 6, 7, 10]); + +say "\nExample 2:"; +solution(2, [4, 5, 6, 7, 8, 9]);
\ No newline at end of file diff --git a/challenge-241/packy-anderson/raku/ch-2.raku b/challenge-241/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..a5755bef6b --- /dev/null +++ b/challenge-241/packy-anderson/raku/ch-2.raku @@ -0,0 +1,74 @@ +#!/usr/bin/env raku +use v6; + +sub findPrimeFactors(Int $number) { + my @factors; + my $num = $number; + while ( $num % 2 == 0 ) { + @factors.push(2); + $num /= 2; + } + for 3 .. sqrt($num).Int -> $i { + while ( $num % $i == 0 ) { + @factors.push($i); + $num /= $i; + } + } + if ($num > 2) { + @factors.push($num); + } + return @factors; +} + +sub sortByPrimeFactors(@int) { + my %primeFactors; + # calculate the prime factors for each number + for @int -> $n { + %primeFactors{$n} = findPrimeFactors($n); + } + # now sort the numbers + my @sorted = @int.sort({ + # first, sort by number of factors + %primeFactors{$^a} <=> %primeFactors{$^b} + || + # then sort by the value of the factors + listCompare(%primeFactors{$^a}, %primeFactors{$^b}) + }); + # now build the output + my $factors = q{}; + for @sorted -> $n { + $factors ~= sprintf 'Prime factors of %2d => ', $n; + $factors ~= %primeFactors{$n}.join(', ') ~ "\n"; + } + return @sorted, $factors; +} + +sub listCompare(@a, @b) { + # this is only getting called if both lists + # have the same number of elements + my $i = 0; + + # compare the corresponding element from each + # list until they're unequal + while ($i < @a.elems && @a[$i] == @b[$i]) { + $i++; + } + # if we ran off the end of the list, set $i to 0 + $i = 0 if $i >= @a.elems; + + return @a[$i] <=> @b[$i]; +} + +sub solution(@int) { + say 'Input: @int = (' ~ @int.join(', ') ~ ')'; + my ($sorted, $factors) = sortByPrimeFactors(@int); + say 'Output: (' ~ $sorted.join(', ') ~ ')'; + say ''; + say $factors; +} + +say "Example 1:"; +solution([11, 8, 27, 4]); + +say "Example 2:"; +solution([2, 4, 8, 12, 11]);
\ No newline at end of file |
