diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-11-16 12:59:59 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-11-16 12:59:59 +0000 |
| commit | f2108bcfd8d40bd168834463da23cf5575649048 (patch) | |
| tree | 0f1eb68b3a2bd0c7bc1c387a9993c89212eb4d21 | |
| parent | a8f0adadbe3bcd71c048916c8abed27533074f34 (diff) | |
| download | perlweeklychallenge-club-f2108bcfd8d40bd168834463da23cf5575649048.tar.gz perlweeklychallenge-club-f2108bcfd8d40bd168834463da23cf5575649048.tar.bz2 perlweeklychallenge-club-f2108bcfd8d40bd168834463da23cf5575649048.zip | |
- Added solutions by Peter Campbell Smith.
| -rwxr-xr-x | challenge-139/peter-campbell-smith/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-139/peter-campbell-smith/perl/ch-2.pl | 112 | ||||
| -rw-r--r-- | stats/pwc-current.json | 71 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-summary.json | 66 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown.json | 904 | ||||
| -rw-r--r-- | stats/pwc-leaders.json | 740 | ||||
| -rw-r--r-- | stats/pwc-summary-1-30.json | 42 | ||||
| -rw-r--r-- | stats/pwc-summary-121-150.json | 50 | ||||
| -rw-r--r-- | stats/pwc-summary-151-180.json | 112 | ||||
| -rw-r--r-- | stats/pwc-summary-181-210.json | 34 | ||||
| -rw-r--r-- | stats/pwc-summary-211-240.json | 48 | ||||
| -rw-r--r-- | stats/pwc-summary-241-270.json | 28 | ||||
| -rw-r--r-- | stats/pwc-summary-31-60.json | 126 | ||||
| -rw-r--r-- | stats/pwc-summary-61-90.json | 116 | ||||
| -rw-r--r-- | stats/pwc-summary-91-120.json | 44 | ||||
| -rw-r--r-- | stats/pwc-summary.json | 32 |
16 files changed, 1361 insertions, 1199 deletions
diff --git a/challenge-139/peter-campbell-smith/perl/ch-1.pl b/challenge-139/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..2863c6d460 --- /dev/null +++ b/challenge-139/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2021-11-15 +# PWC 139 task 1 + +use v5.20; +use warnings; +use strict; + +# You are given a list of numbers. + +# Write a script to implement JortSort. It should return true/false depending +# if the given list of numbers are already sorted. + +my ($lists, $list, $this, $result, $prev); + +# inputs to test +$lists = [[1, 2, 3, 4, 5], [1, 2, 5, 4, 3], [2, 1, 3, 4, 5], [1, 100, 100, 12345, 123456789, 123456788], + [-1, 0, 0, 0, 0, 1], [-100..100, 99], + [12.34, 12.35, 12.36], [-1e6 + 0.1, -1e5-0.1, 1e4 + 0.1, 1e4 + 0.100001]]; + +# loop over inputs +for $list (@$lists) { + say 'Input: ' . join(', ', @$list); + + # assume good until proved otherwise + $result = 1; + $prev = $list->[0]; + for $this (@$list) { + $result = $this < $prev ? 0 : 1; + last unless $result; # no need to check the rest of the list + $prev = $this; + } + say "Output: $result\n"; +} diff --git a/challenge-139/peter-campbell-smith/perl/ch-2.pl b/challenge-139/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..24e200a788 --- /dev/null +++ b/challenge-139/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,112 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2021-11-15 +# PWC 139 task 2 + +use v5.20; +use warnings; +use strict; + +# You are given a list of numbers. +# Write a script to generate first 5 Long Primes +# A prime number (p) is called Long Prime if (1/p) has an infinite decimal expansion repeating every (p-1) digits. +# +# As per Wikipedia, the *shortest* repeating sequence must contain (p-1) digits. + +# This script will work with any prime number up to the maximum integer value allowed by hardware and Perl + +my (@primes, $prime, $limit, $j, $inverse, $digits, $is_long, $min_repeat, $min_repeat_length, $max_prime); + +# make sieve of Eratosthenes to generate primes +$max_prime = 100; +@primes = make_sieve($max_prime); # primes[j] = 1 if j is prime + +# loop over primes +for $prime (2 .. $max_prime) { + next unless $primes[$prime]; + + # we need to calculate the inverse to at least (p - 1) * 2 significant digits + $inverse = precise_inverse($prime); # inverse as a string preceded by '* ' (to stop Perl making it a float) + $digits = substr($inverse, 3, $prime - 1); # first p - 1 digits + next unless substr($inverse, 2 + $prime, $prime - 1) eq $digits; # check whether the next (p - 1) digits are the same + + $min_repeat = min_repeat($inverse); # get the smallest group of repeating digits + $min_repeat_length = length($min_repeat); # and its length + next unless $min_repeat_length == $prime - 1; # and no good unless it is (p - 1) digits long + + # format and issue result + $inverse =~ m|(0\.0*)(.*)|; + $inverse = $1 . substr($2, 0, 2 * $min_repeat_length); + say qq[$prime is a long prime since 1/$prime is $inverse\n] . + qq[The repeating part ($min_repeat) size is ] . $min_repeat_length . + qq[ ie 1 less than the prime number $prime\n] if ($min_repeat_length == $prime - 1); + +} + +sub make_sieve { + + # make sieve of Eratosthenes + my ($arg, $j, $k, @sieve); + + # set all values provisionally to 1 (ie prime) + $arg = $_[0]; + 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; + } + } + return @sieve; +} + +sub precise_inverse { # ($value) where $value >= 1 + + # returns ('* ' . 1/$value), with (2 * $value) digits after the decimal point and following zeroes, as a string + # using manual long division as taught in primary school + + my ($divisor, $remainder, $quotient, $q, %seen); + $divisor = $_[0]; + $remainder = 1; + $quotient = '*0.'; + + # one digit at a time + while (1) { + $remainder *= 10; + $q = int($remainder / $divisor); + $remainder = ($remainder - $q * $divisor); + $quotient .= $q; + + # check that it is at least twice (p - 1) long so that we can check for repeat + if ($quotient =~ m|\*0\.0*(.*)|) { + last if length($1) >= 2 * ($divisor - 1); + } + } + return $quotient; +} + +sub min_repeat { # ($value) where $value == '*0.<zero or more zeroes><digits>' + + # returns the length of the shortest repeating string after '*0.<zero or more zeroes>' + + my ($test, $test_length, $j, $snippet, $snippets); + + # get the significant digits (ie folowwing '* 0.000...') + $_[0] =~ m|\*0\.0*(.*)|; + + # check to see if the first $j digits repeat to form $test + $test = $1; + $test_length = length($test); + for $j (1 .. int($test_length / 2)) { + $snippet = substr($test, 0, $j); + $snippets = $snippet x (int($test_length / length($snippet) + 1)); + $snippets = substr($snippets, 0, $test_length); + return $snippet if $snippets eq $test; + } +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 7c24f8da45..4214d08ce8 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,63 +1,78 @@ { + "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/>" + }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "enabled" : 1, "format" : "{point.y}" - } + }, + "borderWidth" : 0 } }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "title" : { - "text" : "The Weekly Challenge - 139" - }, - "xAxis" : { - "type" : "category" - }, - "legend" : { - "enabled" : 0 - }, - "subtitle" : { - "text" : "[Champions: 1] Last updated at 2021-11-16 12:42:13 GMT" - }, "drilldown" : { "series" : [ { - "id" : "Olivier Delouya", + "name" : "Olivier Delouya", "data" : [ [ "Perl", 1 ] ], - "name" : "Olivier Delouya" + "id" : "Olivier Delouya" + }, + { + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ] + ] } ] }, + "xAxis" : { + "type" : "category" + }, "series" : [ { - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 139", "data" : [ { "name" : "Olivier Delouya", "y" : 1, "drilldown" : "Olivier Delouya" + }, + { + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "y" : 2 } - ] + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 139" } ], - "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" + } + }, + "subtitle" : { + "text" : "[Champions: 2] Last updated at 2021-11-16 12:58:38 GMT" + }, + "legend" : { + "enabled" : 0 }, "chart" : { "type" : "column" + }, + "title" : { + "text" : "The Weekly Challenge - 139" } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index d56ed60122..768ab1cb38 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -2,11 +2,27 @@ "chart" : { "type" : "column" }, - "tooltip" : { - "pointFormat" : "<b>{point.y:.0f}</b>" + "title" : { + "text" : "The Weekly Challenge Contributions [2019 - 2021]" + }, + "legend" : { + "enabled" : "false" }, "series" : [ { + "name" : "Contributions", + "dataLabels" : { + "y" : 10, + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + }, + "format" : "{point.y:.0f}", + "color" : "#FFFFFF", + "align" : "right", + "rotation" : -90, + "enabled" : "true" + }, "data" : [ [ "Blog", @@ -14,50 +30,34 @@ ], [ "Perl", - 6644 + 6646 ], [ "Raku", 4020 ] - ], - "dataLabels" : { - "format" : "{point.y:.0f}", - "enabled" : "true", - "rotation" : -90, - "color" : "#FFFFFF", - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - }, - "align" : "right", - "y" : 10 - }, - "name" : "Contributions" + ] } ], + "yAxis" : { + "title" : { + "text" : null + }, + "min" : 0 + }, "subtitle" : { - "text" : "Last updated at 2021-11-16 12:42:13 GMT" + "text" : "Last updated at 2021-11-16 12:58:38 GMT" }, - "legend" : { - "enabled" : "false" + "tooltip" : { + "pointFormat" : "<b>{point.y:.0f}</b>" }, "xAxis" : { + "type" : "category", "labels" : { "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" + "fontSize" : "13px", + "fontFamily" : "Verdana, sans-serif" } - }, - "type" : "category" - }, - "title" : { - "text" : "The Weekly Challenge Contributions [2019 - 2021]" - }, - "yAxis" : { - "title" : { - "text" : null - }, - "min" : 0 + } } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index f7aa444cb6..b0e2d9468c 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,35 +1,4 @@ { - "xAxis" : { - "type" : "category" - }, - "legend" : { - "enabled" : "false" - }, - "title" : { - "text" : "The Weekly Challenge Language" - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "tooltip" : { - "followPointer" : "true", - "headerFormat" : "<span style=\"font-size:11px\"></span>", - "pointFormat" : "<span style=\"color:{point.color}\">Challenge {point.name}</span>: <b>{point.y:f}</b><br/>" - }, - "chart" : { - "type" : "column" - }, "drilldown" : { "series" : [ { @@ -69,6 +38,7 @@ "id" : "002" }, { + "id" : "003", "data" : [ [ "Perl", @@ -83,10 +53,10 @@ 9 ] ], - "id" : "003", "name" : "003" }, { + "id" : "004", "data" : [ [ "Perl", @@ -101,12 +71,10 @@ 10 ] ], - "id" : "004", "name" : "004" }, { "name" : "005", - "id" : "005", "data" : [ [ "Perl", @@ -120,11 +88,10 @@ "Blog", 12 ] - ] + ], + "id" : "005" }, { - "name" : "006", - "id" : "006", "data" : [ [ "Perl", @@ -138,10 +105,11 @@ "Blog", 7 ] - ] + ], + "name" : "006", + "id" : "006" }, { - "id" : "007", "data" : [ [ "Perl", @@ -156,10 +124,10 @@ 10 ] ], - "name" : "007" + "name" : "007", + "id" : "007" }, { - "name" : "008", "data" : [ [ "Perl", @@ -174,9 +142,11 @@ 12 ] ], + "name" : "008", "id" : "008" }, { + "name" : "009", "data" : [ [ "Perl", @@ -191,10 +161,11 @@ 13 ] ], - "id" : "009", - "name" : "009" + "id" : "009" }, { + "id" : "010", + "name" : "010", "data" : [ [ "Perl", @@ -208,9 +179,7 @@ "Blog", 11 ] - ], - "id" : "010", - "name" : "010" + ] }, { "name" : "011", @@ -231,6 +200,7 @@ "id" : "011" }, { + "id" : "012", "data" : [ [ "Perl", @@ -245,7 +215,6 @@ 11 ] ], - "id" : "012", "name" : "012" }, { @@ -263,10 +232,11 @@ 13 ] ], - "id" : "013", - "name" : "013" + "name" : "013", + "id" : "013" }, { + "id" : "014", "data" : [ [ "Perl", @@ -281,12 +251,9 @@ 15 ] ], - "id" : "014", "name" : "014" }, { - "name" : "015", - "id" : "015", "data" : [ [ "Perl", @@ -300,7 +267,9 @@ "Blog", 15 ] - ] + ], + "name" : "015", + "id" : "015" }, { "name" : "016", @@ -335,11 +304,10 @@ 12 ] ], - "id" : "017", - "name" : "017" + "name" : "017", + "id" : "017" }, { - "name" : "018", "data" : [ [ "Perl", @@ -354,10 +322,12 @@ 14 ] ], + "name" : "018", "id" : "018" }, { "id" : "019", + "name" : "019", "data" : [ [ "Perl", @@ -371,12 +341,11 @@ "Blog", 13 ] - ], - "name" : "019" + ] }, { - "name" : "020", "id" : "020", + "name" : "020", "data" : [ [ "Perl", @@ -393,6 +362,8 @@ ] }, { + "id" : "021", + "name" : "021", "data" : [ [ "Perl", @@ -406,9 +377,7 @@ "Blog", 10 ] - ], - "id" : "021", - "name" : "021" + ] }, { "name" : "022", @@ -429,7 +398,6 @@ "id" : "022" }, { - "id" : "023", "data" : [ [ "Perl", @@ -444,7 +412,8 @@ 12 ] ], - "name" : "023" + "name" : "023", + "id" : "023" }, { "data" : [ @@ -461,8 +430,8 @@ 11 ] ], - "id" : "024", - "name" : "024" + "name" : "024", + "id" : "024" }, { "name" : "025", @@ -483,7 +452,6 @@ "id" : "025" }, { - "name" : "026", "id" : "026", "data" : [ [ @@ -498,10 +466,11 @@ "Blog", 10 ] - ] + ], + "name" : "026" }, { - "id" : "027", + "name" : "027", "data" : [ [ "Perl", @@ -516,10 +485,9 @@ 9 ] ], - "name" : "027" + "id" : "027" }, { - "id" : "028", "data" : [ [ "Perl", @@ -534,7 +502,8 @@ 9 ] ], - "name" : "028" + "name" : "028", + "id" : "028" }, { "name" : "029", @@ -555,8 +524,8 @@ "id" : "029" }, { - "name" : "030", "id" : "030", + "name" : "030", "data" : [ [ "Perl", @@ -573,7 +542,6 @@ ] }, { - "name" : "031", "id" : "031", "data" : [ [ @@ -588,10 +556,10 @@ "Blog", 9 ] - ] + ], + "name" : "031" }, { - "name" : "032", "data" : [ [ "Perl", @@ -606,9 +574,11 @@ 10 ] ], + "name" : "032", "id" : "032" }, { + "name" : "033", "data" : [ [ "Perl", @@ -623,11 +593,10 @@ 10 ] ], - "id" : "033", - "name" : "033" + "id" : "033" }, { - "name" : "034", + "id" : "034", "data" : [ [ "Perl", @@ -642,11 +611,9 @@ 11 ] ], - "id" : "034" + "name" : "034" }, { - "name" : "035", - "id" : "035", "data" : [ [ "Perl", @@ -660,11 +627,13 @@ "Blog", 9 ] - ] + ], + "name" : "035", + "id" : "035" }, { - "name" : "036", "id" : "036", + "name" : "036", "data" : [ [ "Perl", @@ -695,11 +664,10 @@ 9 ] ], - "id" : "037", - "name" : "037" + "name" : "037", + "id" : "037" }, { - "name" : "038", "id" : "038", "data" : [ [ @@ -714,7 +682,8 @@ "Blog", 12 ] - ] + ], + "name" : "038" }, { "id" : "039", @@ -735,7 +704,6 @@ "name" : "039" }, { - "id" : "040", "data" : [ [ "Perl", @@ -750,10 +718,11 @@ 10 ] ], - "name" : "040" + "name" : "040", + "id" : "040" }, { - "name" : "041", + "id" : "041", "data" : [ [ "Perl", @@ -768,7 +737,7 @@ 9 ] ], - "id" : "041" + "name" : "041" }, { "data" : [ @@ -785,8 +754,8 @@ 11 ] ], - "id" : "042", - "name" : "042" + "name" : "042", + "id" : "042" }, { "name" : "043", @@ -807,7 +776,6 @@ "id" : "043" }, { - "name" : "044", "id" : "044", "data" : [ [ @@ -822,9 +790,11 @@ "Blog", 11 ] - ] + ], + "name" : "044" }, { + "id" : "045", "data" : [ [ "Perl", @@ -839,7 +809,6 @@ 11 ] ], - "id" : "045", "name" : "045" }, { @@ -857,11 +826,11 @@ 10 ] ], - "id" : "046", - "name" : "046" + "name" : "046", + "id" : "046" }, { - "id" : "047", + "name" : "047", "data" : [ [ "Perl", @@ -876,10 +845,9 @@ 10 ] ], - "name" : "047" + "id" : "047" }, { - "name" : "048", "id" : "048", "data" : [ [ @@ -894,10 +862,10 @@ "Blog", 12 ] - ] + ], + "name" : "048" }, { - "name" : "049", "data" : [ [ "Perl", @@ -912,9 +880,11 @@ 12 ] ], + "name" : "049", "id" : "049" }, { + "id" : "050", "data" : [ [ "Perl", @@ -929,12 +899,10 @@ 12 ] ], - "id" : "050", "name" : "050" }, { "name" : "051", - "id" : "051", "data" : [ [ "Perl", @@ -948,7 +916,8 @@ "Blog", 11 ] - ] + ], + "id" : "051" }, { "data" : [ @@ -965,11 +934,12 @@ 14 ] ], - "id" : "052", - "name" : "052" + "name" : "052", + "id" : "052" }, { "id" : "053", + "name" : "053", "data" : [ [ "Perl", @@ -983,11 +953,9 @@ "Blog", 15 ] - ], - "name" : "053" + ] }, { - "name" : "054", "id" : "054", "data" : [ [ @@ -1002,9 +970,11 @@ "Blog", 18 ] - ] + ], + "name" : "054" }, { + "id" : "055", "data" : [ [ "Perl", @@ -1019,10 +989,10 @@ 14 ] ], - "id" : "055", "name" : "055" }, { + "id" : "056", "data" : [ [ "Perl", @@ -1037,10 +1007,10 @@ 16 ] ], - "id" : "056", "name" : "056" }, { + "id" : "057", "name" : "057", "data" : [ [ @@ -1055,11 +1025,11 @@ "Blog", 15 ] - ], - "id" : "057" + ] }, { "id" : "058", + "name" : "058", "data" : [ [ "Perl", @@ -1073,11 +1043,9 @@ "Blog", 13 ] - ], - "name" : "058" + ] }, { - "id" : "059", "data" : [ [ "Perl", @@ -1092,7 +1060,8 @@ 16 ] ], - "name" : "059" + "name" : "059", + "id" : "059" }, { "name" : "060", @@ -1113,6 +1082,7 @@ "id" : "060" }, { + "id" : "061", "name" : "061", "data" : [ [ @@ -1127,10 +1097,11 @@ "Blog", 14 ] - ], - "id" : "061" + ] }, { + "id" : "062", + "name" : "062", "data" : [ [ "Perl", @@ -1144,9 +1115,7 @@ "Blog", 11 ] - ], - "id" : "062", - "name" : "062" + ] }, { "name" : "063", @@ -1185,6 +1154,7 @@ "id" : "064" }, { + "id" : "065", "data" : [ [ "Perl", @@ -1199,10 +1169,10 @@ 15 ] ], - "id" : "065", "name" : "065" |
