diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-02-25 16:38:42 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-02-25 16:38:42 +0000 |
| commit | 69661710e709e0644cead2418aa9bb9dccbe534d (patch) | |
| tree | 838c22fd3a4a0825c94be8196690607f97fdf54b | |
| parent | 220a9d8c87758b29a8597d4888f97bece843f15a (diff) | |
| download | perlweeklychallenge-club-69661710e709e0644cead2418aa9bb9dccbe534d.tar.gz perlweeklychallenge-club-69661710e709e0644cead2418aa9bb9dccbe534d.tar.bz2 perlweeklychallenge-club-69661710e709e0644cead2418aa9bb9dccbe534d.zip | |
- Added solutions by Duane Powell.
| -rwxr-xr-x | challenge-049/duane-powell/perl/ch-1.pl | 60 | ||||
| -rwxr-xr-x | challenge-049/duane-powell/perl/ch-2.pl | 189 | ||||
| -rw-r--r-- | stats/pwc-current.json | 109 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-summary.json | 64 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown.json | 740 | ||||
| -rw-r--r-- | stats/pwc-leaders.json | 758 | ||||
| -rw-r--r-- | stats/pwc-summary-1-30.json | 104 | ||||
| -rw-r--r-- | stats/pwc-summary-121-150.json | 110 | ||||
| -rw-r--r-- | stats/pwc-summary-31-60.json | 28 | ||||
| -rw-r--r-- | stats/pwc-summary-61-90.json | 114 | ||||
| -rw-r--r-- | stats/pwc-summary-91-120.json | 46 | ||||
| -rw-r--r-- | stats/pwc-summary.json | 332 |
12 files changed, 1459 insertions, 1195 deletions
diff --git a/challenge-049/duane-powell/perl/ch-1.pl b/challenge-049/duane-powell/perl/ch-1.pl new file mode 100755 index 0000000000..91aa359812 --- /dev/null +++ b/challenge-049/duane-powell/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ TASK #1 + +my $number = shift || 55; +my ($multiple, $next, $solved) = (0,1,0); +until ($solved) { + $multiple = $number * ++$next; + + # Test if number is comprised just 0's and 1's + if ($multiple =~ m/^([01]+)$/) { + # Confirm it's not just all 1's + $solved = ($multiple =~ m/0/); + } +} +say "$number x $next = ".$multiple; +exit; + + +__END__ + +./ch-1.pl +55 x 2 = 110 + +./ch-1.pl 1 +1 x 10 = 10 + +./ch-1.pl 2 +2 x 5 = 10 + +./ch-1.pl 3 +3 x 337 = 1011 + +./ch-1.pl 4 +4 x 25 = 100 + +./ch-1.pl 5 +5 x 2 = 10 + +./ch-1.pl 6 +6 x 185 = 1110 + +./ch-1.pl 7 +7 x 143 = 1001 + +./ch-1.pl 8 +8 x 125 = 1000 + +./ch-1.pl 9 +9 x 112345679 = 1011111111 + +./ch-1.pl 1031 +1031 x 971 = 1001101 + +./ch-1.pl 2324 +2324 x 434595525 = 1010000000100 + diff --git a/challenge-049/duane-powell/perl/ch-2.pl b/challenge-049/duane-powell/perl/ch-2.pl new file mode 100755 index 0000000000..0c93713492 --- /dev/null +++ b/challenge-049/duane-powell/perl/ch-2.pl @@ -0,0 +1,189 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ TASK #2 +# This one is easy to write but I fear will be hard to read later. Needs to be polished. + +my $capacity = shift || 3; +my $verbose = shift; +my $q = LRU_Cache->new($capacity, $verbose); + +$q->set(1,'red'); +$q->set(2,'orange'); +$q->set(3,'yellow'); +$q->queue_say(); +$q->get(2); +$q->queue_say(); +$q->get(1); +$q->queue_say(); +$q->get(4); +$q->queue_say(); +$q->set(4,'green'); +$q->queue_say(); +$q->get(3); +# $q->queue_dump(); # see the object internals +exit; + +package LRU_Cache; +use Data::Dumper; +use constant { + EVICTED => '-1', + VERBOSE => 1, + SILENT => 1, +}; + +sub new { + my $class = shift; + my $capacity = shift; + my $verbose = shift; + my $self = { + cap => $capacity, # size limit of the cache + verb => $verbose, + set => 0, # how many times set() has been called + head => undef, # most recent key + tail => undef, # least recent key + cache => { + # the cache is a hash of hashes, called nodes + # see node_generate() + }, + }; + say "capacity = $capacity" if ($verbose); + return bless $self, $class; +} +sub node_generate { + my $self = shift; + my $data = shift; + return { # double linked list node + prev => undef, + next => undef, + data => $data, + } +} +sub set { + my $self = shift; + my $curr = shift; + my $data = shift; + + # Count how many times we've been called + $self->{set}++; + + # On first call the vars head, current and tail are all the same + if ( $self->{set} == 1 ) { + $self->{tail} = $curr; + $self->{head} = $curr; + } + + # Try to get data before setting data. + # As written cache values can not be changed, they + # must be evicted and then re-set + if ( $self->get($curr, SILENT) eq EVICTED) { + # Generate new node, set it as the head. + # Update caches internal pointers. + my $node = $self->node_generate($data); + my $old_head = $self->{head}; + my $new_head = $curr; + $self->{cache}{$old_head}{prev} = $new_head; + $self->{cache}{$new_head} = $node; + $self->{cache}{$new_head}{next} = $old_head; + $self->{head} = $new_head; + + # If the cache is full set new tail and evict old tail + if ($self->{set} > $self->{cap}) { + my $old_tail = $self->{tail}; + $self->{tail} = $self->{cache}{$old_tail}{prev}; + delete $self->{cache}{$old_tail}; + } + } + say "set($curr,$data)" if ($self->{verb}); + return $data; +} +sub get { + my $self = shift; + my $curr = shift; + my $silent = shift; + + # If get() is called internally by set() be quiet + my $verbose = ($silent) ? 0 : $self->{verb}; + + my $data = EVICTED; # Assume cache miss + if ($self->{cache}{$curr}{data}) { + # Cache hit + $data = $self->{cache}{$curr}{data}; + + # Update cache internal pointers, there are three cases + if ($self->{head} == $curr) { + # Nothing to do, we are already most recently used, aka the head + say "get($curr) # returns $data" if ($verbose); + return $data; + } + elsif ($self->{tail} == $curr) { + # We are the tail node, make our previous node the new tail + my $prev = $self->{cache}{$curr}{prev}; + $self->{cache}{$prev}{next} = undef; # tail doesn't have a next + $self->{tail} = $prev; + } + else { + # We are being pulled from the middle, fill our + # gap by linking our prev and next nodes together + my $prev = $self->{cache}{$curr}{prev}; + my $next = $self->{cache}{$curr}{next}; + $self->{cache}{$prev}{next} = $next; + $self->{cache}{$next}{prev} = $prev; + } + + # Finally, make current the node the most recently used + # and changle old head's pointers + my $old_head = $self->{head}; + $self->{cache}{$curr}{next} = $old_head; # I am on top the old head + $self->{cache}{$old_head}{prev} = $curr; + $self->{cache}{$curr}{prev} = undef; # head doesn't have a previous + $self->{head} = $curr; + } + say "get($curr) # returns $data" if ($verbose); + return $data; +} +sub queue_dump { + my $self = shift; + say Dumper($self); +} +sub queue_say { + my $self = shift; + print "\n[LRU] "; + my $node = $self->{tail}; + until ($node == $self->{head}) { + print "$node=>", $self->{cache}{$node}{data}, ", "; + $node = $self->{cache}{$node}{prev}; + } + say "$node=>", $self->{cache}{$node}{data}, " [MRU]\n"; +} + +__END__ + +capacity = 3 +set(1,red) +set(2,orange) +set(3,yellow) +[LRU] 1 => red, 2 => orange, 3 => yellow [MRU] + +get(2) # returns orange + +[LRU] 1 => red, 3 => yellow, 2 => orange [MRU] + +get(1) # returns red + +[LRU] 3 => yellow, 2 => orange, 1 => red [MRU] + +get(4) # returns -1 + +[LRU] 3 => yellow, 2 => orange, 1 => red [MRU] + +set(4,green) +[LRU] 2 => orange, 1 => red, 4 => green [MRU] + +get(3) # returns -1 + + + + diff --git a/stats/pwc-current.json b/stats/pwc-current.json index e6fea189d6..695b57fb67 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,36 +1,29 @@ { - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } - } - }, - "subtitle" : { - "text" : "[Champions: 8] Last updated at 2020-02-25 14:18:59 GMT" - }, - "legend" : { - "enabled" : 0 - }, - "chart" : { - "type" : "column" - }, "drilldown" : { "series" : [ { + "id" : "Duane Powell", + "name" : "Duane Powell", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { "name" : "E. Choroba", + "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ], - "id" : "E. Choroba" + ] }, { "id" : "Luca Ferrari", + "name" : "Luca Ferrari", "data" : [ [ "Raku", @@ -40,22 +33,21 @@ "Blog", 2 ] - ], - "name" : "Luca Ferrari" + ] }, { + "id" : "Markus Holzer", "name" : "Markus Holzer", "data" : [ [ "Raku", 1 ] - ], - "id" : "Markus Holzer" + ] }, { - "id" : "Mohammad S Anwar", "name" : "Mohammad S Anwar", + "id" : "Mohammad S Anwar", "data" : [ [ "Perl", @@ -68,14 +60,14 @@ ] }, { + "id" : "Peter Scott", "name" : "Peter Scott", "data" : [ [ "Perl", 1 ] - ], - "id" : "Peter Scott" + ] }, { "id" : "Roger Bell West", @@ -106,47 +98,65 @@ ] }, { - "id" : "Wanderdoc", - "name" : "Wanderdoc", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Wanderdoc", + "name" : "Wanderdoc" } ] }, + "legend" : { + "enabled" : 0 + }, + "subtitle" : { + "text" : "[Champions: 9] Last updated at 2020-02-25 16:38:29 GMT" + }, + "chart" : { + "type" : "column" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, "title" : { "text" : "Perl Weekly Challenge - 049" }, - "tooltip" : { - "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/>", - "followPointer" : 1 - }, "xAxis" : { "type" : "category" }, "series" : [ { - "colorByPoint" : 1, "name" : "Perl Weekly Challenge - 049", + "colorByPoint" : 1, "data" : [ { - "drilldown" : "E. Choroba", - "name" : "E. Choroba", + "drilldown" : "Duane Powell", + "name" : "Duane Powell", "y" : 2 }, { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { "name" : "Luca Ferrari", "y" : 4, "drilldown" : "Luca Ferrari" }, { - "y" : 1, + "drilldown" : "Markus Holzer", "name" : "Markus Holzer", - "drilldown" : "Markus Holzer" + "y" : 1 }, { "y" : 3, @@ -154,28 +164,33 @@ "drilldown" : "Mohammad S Anwar" }, { - "y" : 1, "name" : "Peter Scott", + "y" : 1, "drilldown" : "Peter Scott" }, { - "drilldown" : "Roger Bell West", "y" : 4, - "name" : "Roger Bell West" + "name" : "Roger Bell West", + "drilldown" : "Roger Bell West" }, { + "drilldown" : "Simon Proctor", "y" : 3, - "name" : "Simon Proctor", - "drilldown" : "Simon Proctor" + "name" : "Simon Proctor" }, { - "drilldown" : "Wanderdoc", "name" : "Wanderdoc", - "y" : 2 + "y" : 2, + "drilldown" : "Wanderdoc" } ] } ], + "tooltip" : { + "followPointer" : 1, + "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/>" + }, "yAxis" : { "title" : { "text" : "Total Solutions" diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 613bdc3dc8..c5fc3cbacd 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,6 +1,6 @@ { - "title" : { - "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" + "subtitle" : { + "text" : "Last updated at 2020-02-25 16:38:29 GMT" }, "chart" : { "type" : "column" @@ -8,39 +8,17 @@ "legend" : { "enabled" : "false" }, - "subtitle" : { - "text" : "Last updated at 2020-02-25 14:18:59 GMT" - }, "yAxis" : { + "min" : 0, "title" : { "text" : null - }, - "min" : 0 + } }, - "xAxis" : { - "labels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - } - }, - "type" : "category" + "tooltip" : { + "pointFormat" : "<b>{point.y:.0f}</b>" }, "series" : [ { - "dataLabels" : { - "rotation" : -90, - "color" : "#FFFFFF", - "enabled" : "true", - "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" - }, - "format" : "{point.y:.0f}", - "y" : 10, - "align" : "right" - }, - "name" : "Contributions", "data" : [ [ "Blog", @@ -48,16 +26,38 @@ ], [ "Perl", - 2010 + 2012 ], [ "Raku", 1233 ] - ] + ], + "name" : "Contributions", + "dataLabels" : { + "y" : 10, + "style" : { + "fontSize" : "13px", + "fontFamily" : "Verdana, sans-serif" + }, + "color" : "#FFFFFF", + "align" : "right", + "rotation" : -90, + "format" : "{point.y:.0f}", + "enabled" : "true" + } } ], - "tooltip" : { - "pointFormat" : "<b>{point.y:.0f}</b>" + "xAxis" : { + "type" : "category", + "labels" : { + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + } + } + }, + "title" : { + "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index ec614d7086..3acd4ac80b 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,289 +1,15 @@ { - "series" : [ - { - "data" : [ - { - "y" : 140, - "name" : "#001", - "drilldown" : "001" - }, - { - "y" : 109, - "name" : "#002", - "drilldown" : "002" - }, - { - "drilldown" : "003", - "name" : "#003", - "y" : 71 - }, - { - "drilldown" : "004", - "name" : "#004", - "y" : 91 - }, - { - "name" : "#005", - "y" : 71, - "drilldown" : "005" - }, - { - "y" : 48, - "name" : "#006", - "drilldown" : "006" - }, - { - "drilldown" : "007", - "name" : "#007", - "y" : 56 - }, - { - "name" : "#008", - "y" : 70, - "drilldown" : "008" - }, - { - "y" : 68, - "name" : "#009", - "drilldown" : "009" - }, - { - "y" : 60, - "name" : "#010", - "drilldown" : "010" - }, - { - "name" : "#011", - "y" : 79, - "drilldown" : "011" - }, - { - "y" : 83, - "name" : "#012", - "drilldown" : "012" - }, - { - "y" : 76, - "name" : "#013", - "drilldown" : "013" - }, - { - "drilldown" : "014", - "name" : "#014", - "y" : 96 - }, - { - "y" : 93, - "name" : "#015", - "drilldown" : "015" - }, - { - "drilldown" : "016", - "y" : 66, - "name" : "#016" - }, - { - "drilldown" : "017", - "name" : "#017", - "y" : 79 - }, - { - "y" : 76, - "name" : "#018", - "drilldown" : "018" - }, - { - "y" : 95, - "name" : "#019", - "drilldown" : "019" - }, - { - "y" : 95, - "name" : "#020", - "drilldown" : "020" - }, - { - "drilldown" : "021", - "y" : 67, - "name" : "#021" - }, - { - "y" : 63, - "name" : "#022", - "drilldown" : "022" - }, - { - "y" : 91, - "name" : "#023", - "drilldown" : "023" - }, - { - "drilldown" : "024", - "y" : 70, - "name" : "#024" - }, - { - "y" : 55, - "name" : "#025", - "drilldown" : "025" - }, - { - "y" : 70, - "name" : "#026", - "drilldown" : "026" - }, - { - "drilldown" : "027", - "y" : 58, - "name" : "#027" - }, - { - "drilldown" : "028", - "name" : "#028", - "y" : 78 - }, - { - "drilldown" : "029", - "y" : 77, - "name" : "#029" - }, - { - "drilldown" : "030", - "y" : 115, - "name" : "#030" - }, - { - "drilldown" : "031", - "name" : "#031", - "y" : 87 - }, - { - "drilldown" : "032", - "name" : "#032", - "y" : 92 - }, - { - "name" : "#033", - "y" : 108, - "drilldown" : "033" - }, - { - "y" : 60, - "name" : "#034", - "drilldown" : "034" - }, - { - "name" : "#035", - "y" : 60, - "drilldown" : "035" - }, - { - "name" : "#036", - "y" : 61, - "drilldown" : "036" - }, - { - "drilldown" : "037", - "y" : 63, - "name" : "#037" - }, - { - "drilldown" : "038", - "name" : "#038", - "y" : 60 - }, - { - "drilldown" : "039", - "y" : 60, - "name" : "#039" - }, - { - "name" : "#040", - "y" : 66, - "drilldown" : "040" - }, - { - "y" : 69, - "name" : "#041", - "drilldown" : "041" - }, - { - "name" : "#042", - "y" : 88, - "drilldown" : "042" - }, - { - "name" : "#043", - "y" : 65, - "drilldown" : "043" - }, - { - "name" : "#044", - "y" : 81, - "drilldown" : "044" - }, - { - "drilldown" : "045", - "y" : 94, - "name" : "#045" - }, - { - "drilldown" : "046", - "y" : 82, - "name" : "#046" - }, - { - "drilldown" : "047", - "name" : "#047", - "y" : 80 - }, - { - "y" : 101, - "name" : "#048", - "drilldown" : "048" - }, - { - "y" : 20, - "name" : "#049", - "drilldown" : "049" - } - ], - "name" : "Perl Weekly Challenge Languages", - "colorByPoint" : "true" - } - ], - "xAxis" : { - "type" : "category" - }, - "tooltip" : { - "headerFormat" : "<span style=\"font-size:11px\"></span>", - "pointFormat" : "<span style=\"color:{point.color}\">Challenge {point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : "true" - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } + "chart" : { + "type" : "column" }, "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2020-02-25 14:18:59 GMT" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } - } - }, - "title" : { - "text" : "Perl Weekly Challenge Language" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2020-02-25 16:38:29 GMT" }, "drilldown" : { "series" : [ { "id" : "001", + "name" : "001", "data" : [ [ "Perl", @@ -297,12 +23,9 @@ "Blog", 11 ] - ], - "name" : "001" + ] }, { - "id" : "002", - "name" : "002", "data" : [ [ "Perl", @@ -316,10 +39,11 @@ "Blog", 10 ] - ] + ], + "name" : "002", + "id" : "002" }, { - "id" : "003", "data" : [ [ "Perl", @@ -334,11 +58,10 @@ 9 ] ], + "id" : "003", "name" : "003" }, { - "id" : "004", - "name" : "004", "data" : [ [ "Perl", @@ -352,10 +75,13 @@ "Blog", 10 ] - ] + ], + "id" : "004", + "name" : "004" }, { "name" : "005", + "id" : "005", "data" : [ [ "Perl", @@ -369,12 +95,9 @@ "Blog", 12 ] - ], - "id" : "005" + ] }, { - "id" : "006", - "name" : "006", "data" : [ [ "Perl", @@ -388,9 +111,12 @@ "Blog", 7 ] - ] + ], + "id" : "006", + "name" : "006" }, { + "id" : "007", "name" : "007", "data" : [ [ @@ -405,11 +131,9 @@ "Blog", 10 ] - ], - "id" : "007" + ] }, { - "name" : "008", "data" : [ [ "Perl", @@ -424,9 +148,12 @@ 12 ] ], + "name" : "008", "id" : "008" }, { + "id" : "009", + "name" : "009", "data" : [ [ "Perl", @@ -440,13 +167,9 @@ "Blog", 13 ] - ], - "name" : "009", - "id" : "009" + ] }, { - "id" : "010", - "name" : "010", "data" : [ [ "Perl", @@ -460,9 +183,12 @@ "Blog", 11 ] - ] + ], + "name" : "010", + "id" : "010" }, { + "id" : "011", "name" : "011", "data" : [ [ @@ -477,11 +203,9 @@ "Blog", 10 ] - ], - "id" : "011" + ] }, { - "id" : "012", "data" : [ [ "Perl", @@ -496,9 +220,11 @@ 11 ] ], + "id" : "012", "name" : "012" }, { + "name" : "013", "id" : "013", "data" : [ [ @@ -513,11 +239,9 @@ "Blog", 13 ] - ], - "name" : "013" + ] }, { - "name" : "014", "data" : [ [ "Perl", @@ -532,10 +256,10 @@ 15 ] ], + "name" : "014", "id" : "014" }, { - "name" : "015", "data" : [ [ "Perl", @@ -550,11 +274,10 @@ 15 ] ], + "name" : "015", "id" : "015" }, { - "id" : "016", - "name" : "016", "data" : [ [ "Perl", @@ -568,10 +291,13 @@ "Blog", 12 ] - ] + ], + "name" : "016", + "id" : "016" }, { "id" : "017", + "name" : "017", "data" : [ [ "Perl", @@ -585,10 +311,10 @@ "Blog", 12 ] - ], - "name" : "017" + ] }, { + "name" : "018", "id" : "018", "data" : [ [ @@ -603,10 +329,10 @@ "Blog", 14 ] - ], - "name" : "018" + ] }, { + "id" : "019", "name" : "019", "data" : [ [ @@ -621,8 +347,7 @@ "Blog", 13 ] - ], - "id" : "019" + ] }, { "data" : [ @@ -644,6 +369,7 @@ }, { "id" : "021", + "name" : "021", "data" : [ [ "Perl", @@ -657,8 +383,7 @@ "Blog", 10 ] |
