diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-17 18:08:26 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-17 18:08:26 +0000 |
| commit | 916fda5a82a161847f3cddd3b2d85e2abe083fd5 (patch) | |
| tree | 1a154949612f501f2a45c2266530a1c91e4bd732 | |
| parent | fbd541c4cff600400e68f17263c3742e89dcfb39 (diff) | |
| download | perlweeklychallenge-club-916fda5a82a161847f3cddd3b2d85e2abe083fd5.tar.gz perlweeklychallenge-club-916fda5a82a161847f3cddd3b2d85e2abe083fd5.tar.bz2 perlweeklychallenge-club-916fda5a82a161847f3cddd3b2d85e2abe083fd5.zip | |
- Added solution by Duane Powell.
| -rw-r--r-- | challenge-100/duane-powell/perl/ch-2.pl | 229 | ||||
| -rw-r--r-- | stats/pwc-current.json | 323 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-summary.json | 54 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown.json | 604 | ||||
| -rw-r--r-- | stats/pwc-leaders.json | 702 | ||||
| -rw-r--r-- | stats/pwc-summary-1-30.json | 92 | ||||
| -rw-r--r-- | stats/pwc-summary-121-150.json | 52 | ||||
| -rw-r--r-- | stats/pwc-summary-151-180.json | 40 | ||||
| -rw-r--r-- | stats/pwc-summary-181-210.json | 38 | ||||
| -rw-r--r-- | stats/pwc-summary-211-240.json | 36 | ||||
| -rw-r--r-- | stats/pwc-summary-31-60.json | 34 | ||||
| -rw-r--r-- | stats/pwc-summary-61-90.json | 34 | ||||
| -rw-r--r-- | stats/pwc-summary-91-120.json | 106 | ||||
| -rw-r--r-- | stats/pwc-summary.json | 480 |
14 files changed, 1534 insertions, 1290 deletions
diff --git a/challenge-100/duane-powell/perl/ch-2.pl b/challenge-100/duane-powell/perl/ch-2.pl new file mode 100644 index 0000000000..1805988264 --- /dev/null +++ b/challenge-100/duane-powell/perl/ch-2.pl @@ -0,0 +1,229 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-100/ TASK #2 + +# Let user select triangle array, 1,2, or 3. +# 0 for random triangle array. +my $number = shift; +$number ||= 0; + +do { + print <<EOU; +Usage: +$0 [ 0|1|2 ] + $0 0 # random generate triangle array + $0 1 # use task example 1 triangle + $0 2 # use task example 2 triangle + $0 3 # total is zero triangle array +EOU + exit; +} unless ($number >= 0 and $number <= 3); + + + +# Construct new Triangle Array and init to user selected number +my $t = Triangle_Array->new(); +$t->init($number); + +# Find the best path using recursion +# The three positional params are: index, path, total +$t->min_path_find( [0,0], [], 0 ); + +# Explain solution +$t->total(); +$t->explain(); +$t->solution(); +exit; + + + +package Triangle_Array; +sub r { + # return random number between 0 and 9 + return int(rand(10)); +} + +sub new { + my $class = shift; + my $self = { + array => [], # the triangl array + path => [], # best path found + total => 99999, # best total found, init to big magic number + size => 3, # hard coded for triangle with 4 rows, 0 indexed + }; + bless $self, $class; + return $self; +} + +sub init { + my ($self,$number) = @_; + + # task examples + $self->{array} = [ [1], [2,4], [6,4,9], [5,1,7,2] ] if ( $number == 1 ); + $self->{array} = [ [3], [3,1], [5,2,3], [4,3,1,3] ] if ( $number == 2 ); + + # test array, best path is 0 + 0 + 0 + 0 = 0 + $self->{array} = [ [0], [1,0], [1,1,0], [1,1,1,0] ] if ( $number == 3 ); + + # generate randome triangle array + $self->{array} = [ + [ r() ], + [ r(),r() ], + [ r(),r(),r() ], + [ r(),r(),r(),r() ], + ] if ( $number == 0 ); +} + +sub total { + my ($self) = @_; + say "Output: " . $self->{total}; +} + +sub explain { + my ($self) = @_; + say "\nExplanation: The given triangle\n"; + + # Print triangle, need $x to determine leading pad + my $x = 0; + foreach my $row ( @{$self->{array}} ) { + print " " x ($self->{size} - $x); + foreach my $col ( @{$row} ) { + print " $col "; + } + print "\n"; + $x++; + } +} + +sub min_path_find { + my ($self, $index, $path, $total) = @_; + + # Total along the currect path + # Note: we're making a copy of $path because perl is pass-by-ref. + # Each instance of min_path_find() needs its own path, $this_path. + my $this_path = [ @{$path} ]; + push @{$this_path}, $index; + my ($row,$col) = @{$index}; + $total += $self->{array}[$row][$col]; + + # Recurse left and right down the triangle + if ( $row < $self->{size} ) { + $self->min_path_find( [$row+1,$col ], $this_path, $total ); + $self->min_path_find( [$row+1,$col+1], $this_path, $total ); + } + + # Terminal node reached, is this the best path so far? + else { + if ($total < $self->{total}) { + $self->{total} = $total; + $self->{path} = $this_path; + } + } +} + +sub solution { + my ($self) = @_; + + # Print out solution total + my $out = "The minimum path sum from top to bottom: "; + foreach my $index ( @{$self->{path}} ) { + my ($row,$col) = @{$index}; + $out .= $self->{array}[$row][$col] . " + "; + } + $out .= " = $self->{total}"; + $out =~ s/ \+ =/ =/; + say "\n$out\n"; + + # Print out solution path + # Iterate over array refs and print square brackets around + # the element if solution path index [r,c] equals [x,y] + my $x = 0; + foreach my $row ( @{$self->{array}} ) { + my ($r, $c) = @{ $self->{path}[$x] }; + print " " x ($self->{size} - $x); + my $y = 0; + foreach my $col ( @{$row} ) { + $out = ($x == $r and $y == $c) ? "[$col]" : " $col "; + print $out; + $y++; + } + $x++; + print "\n"; + } +} + +1; + +__END__ + +./ch-2.pl 1 +Output: 8 + +Explanation: The given triangle + + 1 + 2 4 + 6 4 9 + 5 1 7 2 + +The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 + + [1] + [2] 4 + 6 [4] 9 + 5 [1] 7 2 + +./ch-2.pl 2 +Output: 7 + +Explanation: The given triangle + + 3 + 3 1 + 5 2 3 + 4 3 1 3 + +The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 + + [3] + 3 [1] + 5 [2] 3 + 4 3 [1] 3 + +./ch-2.pl 3 +Output: 0 + +Explanation: The given triangle + + 0 + 1 0 + 1 1 0 + 1 1 1 0 + +The minimum path sum from top to bottom: 0 + 0 + 0 + 0 = 0 + + [0] + 1 [0] + 1 1 [0] + 1 1 1 [0] + +(hist:2226 jobs:0) >>> ./ch-2.pl +Output: 10 + +Explanation: The given triangle + + 0 + 8 7 + 4 3 7 + 2 0 0 4 + +The minimum path sum from top to bottom: 0 + 7 + 3 + 0 = 10 + + [0] + 8 [7] + 4 [3] 7 + 2 [0] 0 4 + diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 4b6526ff6e..4d0f3fc300 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -4,141 +4,20 @@ "text" : "Total Solutions" } }, - "series" : [ - { - "name" : "Perl Weekly Challenge - 100", - "data" : [ - { - "name" : "Alexander Karelas", - "y" : 2, - "drilldown" : "Alexander Karelas" - }, - { - "y" : 3, - "drilldown" : "Dave Jacoby", - "name" : "Dave Jacoby" - }, - { - "drilldown" : "Flavio Poletti", - "y" : 4, - "name" : "Flavio Poletti" - }, - { - "drilldown" : "Gustavo Chaves", - "y" : 2, - "name" : "Gustavo Chaves" - }, - { - "y" : 3, - "drilldown" : "James Smith", - "name" : "James Smith" - }, - { - "y" : 1, - "drilldown" : "Lance Wicks", - "name" : "Lance Wicks" - }, - { - "drilldown" : "Luca Ferrari", - "y" : 4, - "name" : "Luca Ferrari" - }, - { - "y" : 2, - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" - }, - { - "y" : 2, - "drilldown" : "Mohammad S Anwar", - "name" : "Mohammad S Anwar" - }, - { - "y" : 2, - "drilldown" : "Paulo Custodio", - "name" : "Paulo Custodio" - }, - { - "name" : "Pete Houston", - "y" : 2, - "drilldown" : "Pete Houston" - }, - { - "y" : 1, - "drilldown" : "Philip Hood", - "name" : "Philip Hood" - }, - { - "y" : 4, - "drilldown" : "Roger Bell_West", - "name" : "Roger Bell_West" - }, - { - "drilldown" : "Simon Green", - "y" : 3, - "name" : "Simon Green" - }, - { - "name" : "Simon Proctor", - "y" : 2, - "drilldown" : "Simon Proctor" - }, - { - "drilldown" : "Steven Wilson", - "y" : 1, - "name" : "Steven Wilson" - }, - { - "y" : 4, - "drilldown" : "Stuart Little", - "name" : "Stuart Little" - }, - { - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke", - "y" : 3 - }, - { - "name" : "Vinod Kumar K", - "y" : 1, - "drilldown" : "Vinod Kumar K" - }, - { - "name" : "W. Luis Mochan", - "y" : 3, - "drilldown" : "W. Luis Mochan" - } - ], - "colorByPoint" : 1 - } - ], - "xAxis" : { - "type" : "category" - }, - "tooltip" : { - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" - }, - "chart" : { - "type" : "column" - }, - "title" : { - "text" : "Perl Weekly Challenge - 100" - }, "drilldown" : { "series" : [ { - "name" : "Alexander Karelas", + "id" : "Alexander Karelas", "data" : [ [ "Perl", 2 ] ], - "id" : "Alexander Karelas" + "name" : "Alexander Karelas" }, { + "name" : "Dave Jacoby", "id" : "Dave Jacoby", "data" : [ [ @@ -149,8 +28,17 @@ "Blog", 1 ] - ], - "name" : "Dave Jacoby" + ] + }, + { + "name" : "Duane Powell", + "id" : "Duane Powell", + "data" : [ + [ + "Perl", + 1 + ] + ] }, { "name" : "Flavio Poletti", @@ -167,17 +55,16 @@ "id" : "Flavio Poletti" }, { - "name" : "Gustavo Chaves", "data" : [ [ "Perl", 2 ] ], - "id" : "Gustavo Chaves" + "id" : "Gustavo Chaves", + "name" : "Gustavo Chaves" }, { - "name" : "James Smith", "data" : [ [ "Perl", @@ -188,20 +75,20 @@ 1 ] ], - "id" : "James Smith" + "id" : "James Smith", + "name" : "James Smith" }, { - "name" : "Lance Wicks", - "id" : "Lance Wicks", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Lance Wicks", + "name" : "Lance Wicks" }, { - "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -212,17 +99,18 @@ 2 ] ], + "id" : "Luca Ferrari", "name" : "Luca Ferrari" }, { + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] ], - "id" : "Mark Anderson", - "name" : "Mark Anderson" + "id" : "Mark Anderson" }, { "name" : "Mohammad S Anwar", @@ -255,16 +143,17 @@ "id" : "Pete Houston" }, { - "name" : "Philip Hood", "data" : [ [ "Raku", 1 ] ], - "id" : "Philip Hood" + "id" : "Philip Hood", + "name" : "Philip Hood" }, { + "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -275,8 +164,7 @@ 2 ] ], - "id" : "Roger Bell_West", - "name" : "Roger Bell_West" + "id" : "Roger Bell_West" }, { "name" : "Simon Green", @@ -294,25 +182,26 @@ }, { "name" : "Simon Proctor", - "id" : "Simon Proctor", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Simon Proctor" }, { + "name" : "Steven Wilson", "data" : [ [ "Perl", 1 ] ], - "id" : "Steven Wilson", - "name" : "Steven Wilson" + "id" : "Steven Wilson" }, { + "id" : "Stuart Little", "data" : [ [ "Perl", @@ -323,7 +212,6 @@ 2 ] ], - "id" : "Stuart Little", "name" : "Stuart Little" }, { @@ -342,13 +230,13 @@ }, { "name" : "Vinod Kumar K", - "id" : "Vinod Kumar K", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Vinod Kumar K" }, { "name" : "W. Luis Mochan", @@ -366,19 +254,146 @@ } ] }, + "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 + }, + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" + }, + "xAxis" : { + "type" : "category" + }, + "subtitle" : { + "text" : "[Champions: 21] Last updated at 2021-02-17 18:07:38 GMT" + }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "format" : "{point.y}", "enabled" : 1 - } + }, + "borderWidth" : 0 } }, - "legend" : { - "enabled" : 0 + "title" : { + "text" : "Perl Weekly Challenge - 100" }, - "subtitle" : { - "text" : "[Champions: 20] Last updated at 2021-02-17 11:52:27 GMT" - } + "series" : [ + { + "data" : [ + { + "drilldown" : "Alexander Karelas", + "y" : 2, + "name" : "Alexander Karelas" + }, + { + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby", + "y" : 3 + }, + { + "drilldown" : "Duane Powell", + "y" : 1, + "name" : "Duane Powell" + }, + { + "name" : "Flavio Poletti", + "y" : 4, + "drilldown" : "Flavio Poletti" + }, + { + "name" : "Gustavo Chaves", + "drilldown" : "Gustavo Chaves", + "y" : 2 + }, + { + "y" : 3, + "drilldown" : "James Smith", + "name" : "James Smith" + }, + { + "y" : 1, + "drilldown" : "Lance Wicks", + "name" : "Lance Wicks" + }, + { + "name" : "Luca Ferrari", + "y" : 4, + "drilldown" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "y" : 2, + "drilldown" : "Mark Anderson" + }, + { + "name" : "Mohammad S Anwar", + "drilldown" : "Mohammad S Anwar", + "y" : 2 + }, + { + "name" : "Paulo Custodio", + "drilldown" : "Paulo Custodio", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Pete Houston", + "name" : "Pete Houston" + }, + { + "name" : "Philip Hood", + "drilldown" : "Philip Hood", + "y" : 1 + }, + { + "y" : 4, + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "drilldown" : "Simon Green", + "y" : 3 + }, + { + "drilldown" : "Simon Proctor", + "y" : 2, + "name" : "Simon Proctor" + }, + { + "name" : "Steven Wilson", + "y" : 1, + "drilldown" : "Steven Wilson" + }, + { + "y" : 4, + "drilldown" : "Stuart Little", + "name" : "Stuart Little" + }, + { + "name" : "Ulrich Rieke", + "y" : 3, + "drilldown" : "Ulrich Rieke" + }, + { + "name" : "Vinod Kumar K", + "drilldown" : "Vinod Kumar K", + "y" : 1 + }, + { + "y" : 3, + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan" + } + ], + "colorByPoint" : 1, + "name" : "Perl Weekly Challenge - 100" + } + ] } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index f4cffcee07..657f8924ed 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,18 +1,6 @@ { "series" : [ { - "dataLabels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - }, - "y" : 10, - "format" : "{point.y:.0f}", - "enabled" : "true", - "color" : "#FFFFFF", - "rotation" : -90, - "align" : "right" - }, "data" : [ [ "Blog", @@ -20,28 +8,28 @@ ], [ "Perl", - 4674 + 4675 ], [ "Raku", 3010 ] ], + "dataLabels" : { + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + }, + "format" : "{point.y:.0f}", + "enabled" : "true", + "y" : 10, + "color" : "#FFFFFF", + "rotation" : -90, + "align" : "right" + }, "name" : "Contributions" } ], - "yAxis" : { - "title" : { - "text" : null - }, - "min" : 0 - }, - "chart" : { - "type" : "column" - }, - "tooltip" : { - "pointFormat" : "<b>{point.y:.0f}</b>" - }, "xAxis" : { "type" : "category", "labels" : { @@ -51,13 +39,25 @@ } } }, + "subtitle" : { + "text" : "Last updated at 2021-02-17 18:07:38 GMT" + }, "title" : { "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" }, + "yAxis" : { + "min" : 0, + "title" : { + "text" : null + } + }, + "tooltip" : { + "pointFormat" : "<b>{point.y:.0f}</b>" + }, "legend" : { "enabled" : "false" }, - "subtitle" : { - "text" : "Last updated at 2021-02-17 11:52:27 GMT" + "chart" : { + "type" : "column" } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index c52f3d64f1..6b65682bd1 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,14 +1,7 @@ { - "title" : { - "text" : "Perl Weekly Challenge Language" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } + "yAxis" : { + "title" : { + "text" : "Total Solutions" } }, "drilldown" : { @@ -32,7 +25,6 @@ ] }, { - "name" : "002", "data" : [ [ "Perl", @@ -47,10 +39,11 @@ 10 ] ], - "id" : "002" + "id" : "002", + "name" : "002" }, { - "id" : "003", + "name" : "003", "data" : [ [ "Perl", @@ -65,10 +58,10 @@ 9 ] ], - "name" : "003" + "id" : "003" }, { - "name" : "004", + "id" : "004", "data" : [ [ "Perl", @@ -83,7 +76,7 @@ 10 ] ], - "id" : "004" + "name" : "004" }, { "name" : "005", @@ -104,7 +97,6 @@ "id" : "005" }, { - "name" : "006", "data" : [ [ "Perl", @@ -119,7 +111,8 @@ 7 ] ], - "id" : "006" + "id" : "006", + "name" : "006" }, { "data" : [ @@ -140,7 +133,6 @@ "name" : "007" }, { - "id" : "008", "data" : [ [ "Perl", @@ -155,6 +147,7 @@ 12 ] ], + "id" : "008", "name" : "008" }, { @@ -176,8 +169,6 @@ ] }, { - "name" : "010", - "id" : "010", "data" : [ [ "Perl", @@ -191,9 +182,12 @@ "Blog", 11 ] - ] + ], + "id" : "010", + "name" : "010" }, { + "name" : "011", "data" : [ [ "Perl", @@ -208,8 +202,7 @@ 10 ] ], - "id" : "011", - "name" : "011" + "id" : "011" }, { "data" : [ @@ -230,6 +223,8 @@ "name" : "012" }, { + "name" : "013", + "id" : "013", "data" : [ [ "Perl", @@ -243,11 +238,10 @@ "Blog", 13 ] - ], - "id" : "013", - "name" : "013" + ] }, { + "id" : "014", "data" : [ [ "Perl", @@ -262,10 +256,10 @@ 15 ] ], - "id" : "014", "name" : "014" }, { + "id" : "015", "data" : [ [ "Perl", @@ -280,10 +274,10 @@ 15 ] ], - "id" : "015", "name" : "015" }, { + "id" : "016", "data" : [ [ "Perl", @@ -298,11 +292,9 @@ 12 ] ], - "id" : "016", "name" : "016" }, { - "name" : "017", "id" : "017", "data" : [ [ @@ -317,7 +309,8 @@ "Blog", 12 ] - ] + ], + "name" : "017" }, { "name" : "018", @@ -356,7 +349,7 @@ "id" : "019" }, { - "name" : "020", + "id" : "020", "data" : [ [ "Perl", @@ -371,10 +364,9 @@ 13 ] ], - "id" : "020" + "name" : "020" }, { - "name" : "021", "data" : [ [ "Perl", @@ -389,10 +381,12 @@ 10 ] ], - "id" : "021" + "id" : "021", + "name" : "021" }, { "name" : "022", + "id" : "022", "data" : [ [ "Perl", @@ -406,11 +400,10 @@ "Blog", 10 ] - ], - "id" : "022" + ] }, { - "name" : "023", + "id" : "023", "data" : [ [ "Perl", @@ -425,11 +418,9 @@ 12 ] ], - "id" : "023" + "name" : "023" }, { - "name" : "024", - "id" : "024", "data" : [ [ "Perl", @@ -443,11 +434,11 @@ "Blog", 11 ] - ] + ], + "id" : "024", + "name" : "024" }, { - "name" : "025", - "id" : "025", "data" : [ [ "Perl", @@ -461,10 +452,11 @@ "Blog", 12 ] - ] + ], + "id" : "025", + "name" : "025" }, { - "name" : "026", "id" : "026", "data" : [ [ @@ -479,10 +471,10 @@ "Blog", 10 ] - ] + ], + "name" : "026" }, { - "name" : "027", "data" : [ [ "Perl", @@ -497,10 +489,10 @@ 9 ] ], - "id" : "027" + "id" : "027", + "name" : "027" }, { - "id" : "028", "data" : [ [ "Perl", @@ -515,6 +507,7 @@ 9 ] ], + "id" : "028", "name" : "028" }, { @@ -554,7 +547,6 @@ "name" : "030" }, { - "name" : "031", "data" : |
