diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-27 05:37:16 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-27 05:37:16 +0000 |
| commit | 8ff483fc1d2ccd130d73daeacd3077948ee1a76c (patch) | |
| tree | 7f42465e2b8faef9c756737bdf34f7a4fa319754 | |
| parent | 766ab3c38200e166651338496d10d8a7d925ac6d (diff) | |
| download | perlweeklychallenge-club-8ff483fc1d2ccd130d73daeacd3077948ee1a76c.tar.gz perlweeklychallenge-club-8ff483fc1d2ccd130d73daeacd3077948ee1a76c.tar.bz2 perlweeklychallenge-club-8ff483fc1d2ccd130d73daeacd3077948ee1a76c.zip | |
- Added solutions by Packy Anderson.
24 files changed, 2673 insertions, 2500 deletions
diff --git a/challenge-244/packy-anderson/README.md b/challenge-244/packy-anderson/README.md index ff23846b2d..9634569207 100644 --- a/challenge-244/packy-anderson/README.md +++ b/challenge-244/packy-anderson/README.md @@ -8,19 +8,16 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @nums = (1, 3, 2, 3, 1) -Output: 2 - -(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 +Input: @int = (8, 1, 2, 2, 3) +Output: (4, 0, 1, 1, 3) Example 2: -Input: @nums = (2, 4, 3, 5, 1) -Output: 3 +Input: @int = (6, 5, 4, 8) +Output: (2, 1, 0, 3) -(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 -(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 +Example 3: +Input: @int = (2, 2, 2) +Output: (0, 0, 0) ``` * [Task 2](raku/ch-2.raku) @@ -29,12 +26,8 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: @nums = (2, 5, 9) -Output: 10 - -Example 2: -Input: @nums = (7, 7, 7, 7, 7, 7, 7) -Output: 49 +Input: @int = (2, 1, 4) +Output: 141 ``` ## Perl @@ -45,19 +38,16 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @nums = (1, 3, 2, 3, 1) -Output: 2 - -(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 +Input: @arr = (8, 1, 2, 2, 3) +Output: (4, 0, 1, 1, 3) Example 2: -Input: @nums = (2, 4, 3, 5, 1) -Output: 3 +Input: @arr = (6, 5, 4, 8) +Output: (2, 1, 0, 3) -(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 -(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 +Example 3: +Input: @arr = (2, 2, 2) +Output: (0, 0, 0) ``` * [Task 2](perl/ch-2.pl) @@ -66,12 +56,8 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: @nums = (2, 5, 9) -Output: 10 - -Example 2: -Input: @nums = (7, 7, 7, 7, 7, 7, 7) -Output: 49 +Input: @int = (2, 1, 4) +Output: 141 ``` ## Guest Language: Python @@ -80,4 +66,4 @@ Output: 49 ## Blog Post -[Perl Weekly Challenge: Three of a Reverse Sum Pair](https://packy.dardan.com/b/E5) +[Perl Weekly Challenge: Count... just a little bit smaller...](https://packy.dardan.com/b/EK) diff --git a/challenge-244/packy-anderson/blog.txt b/challenge-244/packy-anderson/blog.txt new file mode 100644 index 0000000000..8cadf55070 --- /dev/null +++ b/challenge-244/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/11/20/perl-weekly-challenge-count-just-a-little-bit-smaller/ diff --git a/challenge-244/packy-anderson/perl/ch-1.pl b/challenge-244/packy-anderson/perl/ch-1.pl new file mode 100644 index 0000000000..a193ffc8b4 --- /dev/null +++ b/challenge-244/packy-anderson/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use v5.38; + +sub countSmaller { + my @int = @_; + my @counts; + foreach my $i ( 0 .. $#int ) { + $counts[$i] = 0; + for my $j ( 0 .. $#int ) { + $counts[$i]++ if $int[$j] < $int[$i]; + } + } + return @counts; +} + +sub solution { + my @int = @_; + say 'Input: @arr = (' . join(', ', @int) . ')'; + my @output = countSmaller(@int); + say 'Output: (' . join(', ', @output) . ')'; +} + +say "Example 1:"; +solution(8, 1, 2, 2, 3); + +say "\nExample 2:"; +solution(6, 5, 4, 8); + +say "\nExample 3:"; +solution(2, 2, 2); diff --git a/challenge-244/packy-anderson/perl/ch-2.pl b/challenge-244/packy-anderson/perl/ch-2.pl new file mode 100644 index 0000000000..ec5b556422 --- /dev/null +++ b/challenge-244/packy-anderson/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use v5.38; + +use Algorithm::Combinatorics qw(combinations); +use List::Util qw( min max sum ); + +sub power { + my $list = shift; + return( (max(@$list) ** 2) * min(@$list) ); +} + +sub groupHero(@nums) { + return sum( + # generate the list of powers for each combination + map { power($_) } + # generate the list of combinations + map { combinations(\@nums, $_) } 1 .. scalar(@nums) + ); +} + +sub solution { + my @nums = @_; + say 'Input: @int = (' . join(', ', @nums) . ')'; + my $output = groupHero(@nums); + say 'Output: ' . $output; +} + +say "Example 1:"; +solution(2, 1, 4); diff --git a/challenge-244/packy-anderson/python/ch-1.py b/challenge-244/packy-anderson/python/ch-1.py new file mode 100644 index 0000000000..f2aace0b05 --- /dev/null +++ b/challenge-244/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def countSmaller(arr): + counts = [] + for i, i_val in enumerate(arr): + counts.append(0) + for j, j_val in enumerate(arr): + if j_val < i_val: + counts[i] += 1 + return counts + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(arr): + print(f'Input: @int = ({comma_join(arr)})') + output = countSmaller(arr) + print(f'Output: ({comma_join(output)})') + +print('Example 1:') +solution([8, 1, 2, 2, 3]) + +print('\nExample 2:') +solution([6, 5, 4, 8]) + +print('\nExample 3:') +solution([2, 2, 2]) diff --git a/challenge-244/packy-anderson/python/ch-2.py b/challenge-244/packy-anderson/python/ch-2.py new file mode 100644 index 0000000000..3c13ba4d92 --- /dev/null +++ b/challenge-244/packy-anderson/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +from itertools import combinations + +def power(arr): + return( (max(arr) ** 2) * min(arr) ) + +def groupHero(nums): + # generate a list of combinations + comb = [] + for i in range(1, len(nums)+1): + for c in combinations(nums, i): + comb.append(c) + return sum( + # generate the list of powers for each combination + [ power(x) for x in comb ] + ) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(arr): + print(f'Input: @int = ({comma_join(arr)})') + output = groupHero(arr) + print(f'Output: {output}') + +print('Example 1:') +solution([2, 1, 4]) diff --git a/challenge-244/packy-anderson/raku/ch-1.raku b/challenge-244/packy-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..eb92753d14 --- /dev/null +++ b/challenge-244/packy-anderson/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub countSmaller(@int) { + my @counts; + for @int.keys -> $i { + @counts[$i] = 0; + for @int.keys -> $j { + @counts[$i]++ if @int[$j] < @int[$i]; + } + } + return @counts; +} + +sub solution(*@int) { + say 'Input: @int = (' ~ @int.join(', ') ~ ')'; + my @output = countSmaller(@int); + say 'Output: (' ~ @output.join(', ') ~ ')'; +} + +say "Example 1:"; +solution(8, 1, 2, 2, 3); + +say "\nExample 2:"; +solution(6, 5, 4, 8); + +say "\nExample 3:"; +solution(2, 2, 2); diff --git a/challenge-244/packy-anderson/raku/ch-2.raku b/challenge-244/packy-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..0fa821829f --- /dev/null +++ b/challenge-244/packy-anderson/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku +use v6; + +sub power(@nums) { + return( (@nums.max ** 2) * @nums.min ); +} + +sub groupHero(@nums) { + return [+] ( + power($_) for @nums.combinations: 1 .. @nums.elems + ); +} + +sub solution(*@nums) { + say 'Input: @int = (' ~ @nums.join(', ') ~ ')'; + my $output = groupHero(@nums); + say 'Output: ' ~ $output; +} + +say "Example 1:"; +solution(2, 1, 4); diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 9f8992a595..8f29ace616 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,16 +1,208 @@ { + "legend" : { + "enabled" : 0 + }, + "series" : [ + { + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 244", + "data" : [ + { + "y" : 4, + "drilldown" : "Adam Russell", + "name" : "Adam Russell" + }, + { + "name" : "Ali Moradi", + "drilldown" : "Ali Moradi", + "y" : 5 + }, + { + "name" : "Arne Sommer", + "y" : 3, + "drilldown" : "Arne Sommer" + }, + { + "name" : "Athanasius", + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "name" : "BarrOff", + "y" : 3, + "drilldown" : "BarrOff" + }, + { + "name" : "Bob Lied", + "drilldown" : "Bob Lied", + "y" : 3 + }, + { + "name" : "Bruce Gray", + "y" : 2, + "drilldown" : "Bruce Gray" + }, + { + "name" : "Cheok-Yin Fung", + "y" : 2, + "drilldown" : "Cheok-Yin Fung" + }, + { + "name" : "Clifton Wood", + "drilldown" : "Clifton Wood", + "y" : 2 + }, + { + "drilldown" : "Dave Jacoby", + "y" : 3, + "name" : "Dave Jacoby" + }, + { + "name" : "David Ferrone", + "y" : 2, + "drilldown" : "David Ferrone" + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "drilldown" : "Humberto Massa", + "y" : 2, + "name" : "Humberto Massa" + }, + { + "y" : 3, + "drilldown" : "Ian Rifkin", + "name" : "Ian Rifkin" + }, + { + "y" : 5, + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek", + "y" : 2 + }, + { + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey", + "y" : 3 + }, + { + "name" : "Laurent Rosenfeld", + "y" : 6, + "drilldown" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Lubos Kolouch", + "y" : 5, + "name" : "Lubos Kolouch" + }, + { + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari", + "y" : 10 + }, + { + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Matthew Neleigh", + "y" : 2, + "drilldown" : "Matthew Neleigh" + }, + { + "name" : "Matthias Muth", + "y" : 3, + "drilldown" : "Matthias Muth" + }, + { + "name" : "Nelo Tovar", + "y" : 2, + "drilldown" : "Nelo Tovar" + }, + { + "name" : "Niels van Dijke", + "y" : 2, + "drilldown" : "Niels van Dijke" + }, + { + "drilldown" : "Packy Anderson", + "y" : 5, + "name" : "Packy Anderson" + }, + { + "y" : 2, + "drilldown" : "Paulo Custodio", + "name" : "Paulo Custodio" + }, + { + "name" : "Peter Campbell Smith", + "y" : 3, + "drilldown" : "Peter Campbell Smith" + }, + { + "y" : 2, + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "y" : 2, + "drilldown" : "rcmlz", + "name" : "rcmlz" + }, + { + "drilldown" : "Robbie Hatley", + "y" : 3, + "name" : "Robbie Hatley" + }, + { + "drilldown" : "Robert Ransbottom", + "y" : 2, + "name" : "Robert Ransbottom" + }, + { + "drilldown" : "Roger Bell_West", + "y" : 5, + "name" : "Roger Bell_West" + }, + { + "drilldown" : "Simon Green", + "y" : 3, + "name" : "Simon Green" + }, + { + "drilldown" : "Thomas Kohler", + "y" : 4, + "name" : "Thomas Kohler" + }, + { + "drilldown" : "Ulrich Rieke", + "y" : 4, + "name" : "Ulrich Rieke" + }, + { + "name" : "W. Luis Mochan", + "y" : 3, + "drilldown" : "W. Luis Mochan" + } + ] + } + ], "chart" : { "type" : "column" }, - "xAxis" : { - "type" : "category" - }, - "subtitle" : { - "text" : "[Champions: 36] Last updated at 2023-11-27 05:05:31 GMT" - }, "drilldown" : { "series" : [ { + "name" : "Adam Russell", + "id" : "Adam Russell", "data" : [ [ "Perl", @@ -20,12 +212,10 @@ "Blog", 2 ] - ], - "id" : "Adam Russell", - "name" : "Adam Russell" + ] }, { - "id" : "Ali Moradi", + "name" : "Ali Moradi", "data" : [ [ "Perl", @@ -40,9 +230,10 @@ 1 ] ], - "name" : "Ali Moradi" + "id" : "Ali Moradi" }, { + "name" : "Arne Sommer", "id" : "Arne Sommer", "data" : [ [ @@ -53,12 +244,9 @@ "Blog", 1 ] - ], - "name" : "Arne Sommer" + ] }, { - "name" : "Athanasius", - "id" : "Athanasius", "data" : [ [ "Perl", @@ -68,7 +256,9 @@ "Raku", 2 ] - ] + ], + "id" : "Athanasius", + "name" : "Athanasius" }, { "name" : "BarrOff", @@ -85,7 +275,6 @@ ] }, { - "name" : "Bob Lied", "id" : "Bob Lied", "data" : [ [ @@ -96,40 +285,40 @@ "Blog", 1 ] - ] + ], + "name" : "Bob Lied" }, { "name" : "Bruce Gray", + "id" : "Bruce Gray", "data" : [ [ "Raku", 2 ] - ], - "id" : "Bruce Gray" + ] }, { - "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "name" : "Cheok-Yin Fung" + "id" : "Cheok-Yin Fung" }, { - "name" : "Clifton Wood", - "id" : "Clifton Wood", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Clifton Wood", + "name" : "Clifton Wood" }, { - "id" : "Dave Jacoby", "data" : [ [ "Perl", @@ -140,16 +329,17 @@ 1 ] ], + "id" : "Dave Jacoby", "name" : "Dave Jacoby" }, { - "id" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], + "id" : "David Ferrone", "name" : "David Ferrone" }, { @@ -163,17 +353,17 @@ "id" : "E. Choroba" }, { - "name" : "Humberto Massa", "data" : [ [ "Raku", 2 ] ], - "id" : "Humberto Massa" + "id" : "Humberto Massa", + "name" : "Humberto Massa" }, { - "id" : "Ian Rifkin", + "name" : "Ian Rifkin", "data" : [ [ "Perl", @@ -184,7 +374,7 @@ 1 ] ], - "name" : "Ian Rifkin" + "id" : "Ian Rifkin" }, { "id" : "Jaldhar H. Vyas", @@ -205,16 +395,18 @@ "name" : "Jaldhar H. Vyas" }, { + "name" : "Jan Krnavek", "id" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ], - "name" : "Jan Krnavek" + ] }, { + "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey", "data" : [ [ "Perl", @@ -224,12 +416,10 @@ "Blog", 1 ] - ], - "id" : "Jorg Sommrey", - "name" : "Jorg Sommrey" + ] }, { - "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -244,7 +434,7 @@ 2 ] ], - "name" : "Laurent Rosenfeld" + "id" : "Laurent Rosenfeld" }, { "data" : [ @@ -265,8 +455,6 @@ "name" : "Lubos Kolouch" }, { - "name" : "Luca Ferrari", - "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -276,29 +464,32 @@ "Blog", 8 ] - ] + ], + "id" : "Luca Ferrari", + "name" : "Luca Ferrari" }, { "name" : "Mark Anderson", + "id" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "id" : "Mark Anderson" + ] }, { - "id" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] ], + "id" : "Matthew Neleigh", "name" : "Matthew Neleigh" }, { + "name" : "Matthias Muth", "id" : "Matthias Muth", "data" : [ [ @@ -309,42 +500,58 @@ "Blog", 1 ] - ], - "name" : "Matthias Muth" + ] }, { - "name" : "Nelo Tovar", + "id" : "Nelo Tovar", "data" : [ [ "Perl", 2 ] ], - "id" : "Nelo Tovar" + "name" : "Nelo Tovar" }, { + "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] ], - "id" : "Niels van Dijke", "name" : "Niels van Dijke" }, { - "name" : "Paulo Custodio", + "name" : "Packy Anderson", "data" : [ [ "Perl", 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 ] ], - "id" : "Paulo Custodio" + "id" : "Packy Anderson" + }, + { + "name" : "Paulo Custodio", + "id" : "Paulo Custodio", + "data" : [ + [ + "Perl", + 2 + ] + ] }, { "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -354,7 +561,8 @@ "Blog", 1 ] - ] + ], + "id" : "Peter Campbell Smith" }, { "name" : "Peter Meszaros", @@ -367,16 +575,18 @@ ] }, { - "id" : "rcmlz", + "name" : "rcmlz", "data" : [ [ "Raku", 2 ] ], - "name" : "rcmlz" + "id" : "rcmlz" }, { + "name" : "Robbie Hatley", + "id" : "Robbie Hatley", "data" : [ [ "Perl", @@ -386,9 +596,7 @@ "Blog", 1 ] - ], - "id" : "Robbie Hatley", - "name" : "Robbie Hatley" + ] }, { "name" : "Robert Ransbottom", @@ -401,7 +609,6 @@ "id" : "Robert Ransbottom" }, { - "name" : "Roger Bell_West", "id" : "Roger Bell_West", "data" : [ [ @@ -416,9 +623,11 @@ "Blog", 1 ] - ] + ], + "name" : "Roger Bell_West" }, { + "id" : "Simon Green", "data" : [ [ "Perl", @@ -429,12 +638,9 @@ 1 ] ], - "id" : "Simon Green", "name" : "Simon Green" }, { - "name" : "Thomas Kohler", - "id" : "Thomas Kohler", "data" : [ [ "Perl", @@ -444,7 +650,9 @@ "Blog", 2 ] - ] + ], + "id" : "Thomas Kohler", + "name" : "Thomas Kohler" }, { "id" : "Ulrich Rieke", @@ -476,217 +684,32 @@ } ] }, + "subtitle" : { + "text" : "[Champions: 37] Last updated at 2023-11-27 05:34:15 GMT" + }, "title" : { "text" : "The Weekly Challenge - 244" }, "tooltip" : { - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "legend" : { - "enabled" : 0 + "xAxis" : { + "type" : "category" }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "format" : "{point.y}", "enabled" : 1 - } + }, + "borderWidth" : 0 } }, - "series" : [ - { - "name" : "The Weekly Challenge - 244", - "data" : [ - { - "name" : "Adam Russell", - "y" : 4, - "drilldown" : "Adam Russell" - }, - { - "name" : "Ali Moradi", - "y" : 5, - "drilldown" : "Ali Moradi" - }, - { - "y" : 3, - "name" : "Arne Sommer", - "drilldown" : "Arne Sommer" - }, - { - "y" : 4, - "name" : "Athanasius", - "drilldown" : "Athanasius" - }, - { - "name" : "BarrOff", - "y" : 3, - "drilldown" : "BarrOff" - }, - { - "drilldown" : "Bob Lied", - "name" : "Bob Lied", - "y" : 3 - }, - { - "drilldown" : "Bruce Gray", - "y" : 2, - "name" : "Bruce Gray" - }, - { - "drilldown" : "Cheok-Yin Fung", - "y" : 2, - "name" : "Cheok-Yin Fung" - }, - { - |
