diff options
| author | Adam Russell <adam.russell@optum.com> | 2019-04-04 13:04:12 -0400 |
|---|---|---|
| committer | Adam Russell <adam.russell@optum.com> | 2019-04-04 13:04:12 -0400 |
| commit | f99a6c0cec978e89f58147a8158d16d640d2060f (patch) | |
| tree | 18196538c1a51ecd293c87f6fc63e87fe00544e7 | |
| parent | f8508b1bd46ad82f1b13a21f0fe95c47030ae80f (diff) | |
| parent | 6af4da3c0647884da84538d52f6745bb657648ab (diff) | |
| download | perlweeklychallenge-club-f99a6c0cec978e89f58147a8158d16d640d2060f.tar.gz perlweeklychallenge-club-f99a6c0cec978e89f58147a8158d16d640d2060f.tar.bz2 perlweeklychallenge-club-f99a6c0cec978e89f58147a8158d16d640d2060f.zip | |
Merge remote-tracking branch 'upstream/master'
25 files changed, 811 insertions, 535 deletions
diff --git a/challenge-001/andrezgz/README b/challenge-001/andrezgz/README new file mode 100644 index 0000000000..f4fd0da88e --- /dev/null +++ b/challenge-001/andrezgz/README @@ -0,0 +1 @@ +Solution by Andrezgz diff --git a/challenge-001/andrezgz/perl5/ch-1.pl b/challenge-001/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..c39458af73 --- /dev/null +++ b/challenge-001/andrezgz/perl5/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/a-new-week-a-new-challenge/ +# Challenge #1 +# Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. +# Also print the number of times the character ‘e’ found in the string. + +use strict; + +my $str = 'Perl Weekly Challenge'; + +my $c = ($str =~ tr/e/E/); + +print $str . ' had ' . $c . ' e\'s that were replaced by E\'s'; diff --git a/challenge-001/andrezgz/perl5/ch-2.sh b/challenge-001/andrezgz/perl5/ch-2.sh new file mode 100644 index 0000000000..e3471a0222 --- /dev/null +++ b/challenge-001/andrezgz/perl5/ch-2.sh @@ -0,0 +1,47 @@ +# https://perlweeklychallenge.org/blog/a-new-week-a-new-challenge/ +# Challenge #2 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. +# However, any number divisible by 3 should be replaced by the word ‘fizz’ and any divisible by 5 +# by the word ‘buzz’. Those numbers that are both divisible by 3 and 5 become ‘fizzbuzz’. + + +perl -e ' + map { + print ( + (!($_ % 15) + ? "fizzbuzz" + : !($_ % 3) + ? "fizz" + : !($_ % 5) + ? "buzz" + : $_) + . "\n" + ) + } (1..20) +' + +perl -e ' + map { + print ( + (!($_ % 15) + ? "fizzbuzz" : "" + .!($_ % 3) + ? "fizz" : "" + .!($_ % 5) + ? "buzz" : "" + . $_) + . $/ + ) + } (1..20) +' + + +perl -e ' + print join $/, map { + my $d = $_; + $_ = "fizz" unless ($d % 3); + $_ = "buzz" unless ($d % 5); + $_ = "fizzbuzz" unless ($d % 15); + $_ + } (1..20) +' diff --git a/challenge-001/james-smith/perl6/ch-1.sh b/challenge-001/james-smith/perl6/ch-1.sh new file mode 100644 index 0000000000..97acafca4b --- /dev/null +++ b/challenge-001/james-smith/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e 'say Int( (my $s = "Perl Weekly Challenge" ) ~~ tr/e/E/); say $s;' diff --git a/challenge-001/james-smith/perl6/ch-2.sh b/challenge-001/james-smith/perl6/ch-2.sh new file mode 100644 index 0000000000..0ca8913e1f --- /dev/null +++ b/challenge-001/james-smith/perl6/ch-2.sh @@ -0,0 +1 @@ +perl6 -e 'say (($_%3??""!!"Fizz") ~ ($_%5??""!!"Buzz")||$_) for 1..20;' diff --git a/challenge-002/andrezgz/README b/challenge-002/andrezgz/README new file mode 100644 index 0000000000..f4fd0da88e --- /dev/null +++ b/challenge-002/andrezgz/README @@ -0,0 +1 @@ +Solution by Andrezgz diff --git a/challenge-002/andrezgz/perl5/ch-1.pl b/challenge-002/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..36addeca96 --- /dev/null +++ b/challenge-002/andrezgz/perl5/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/ +# Challenge #1 +# Write a script or one-liner to remove leading zeros from positive numbers. + +# Wikipedia - https://en.wikipedia.org/wiki/Leading_zero +# A leading zero is any 0 digit that comes before the first nonzero digit +# in a number string in positional notation. +# When leading zeros occupy the most significant digits of an integer, +# they could be left blank or omitted for the same numeric value. +# Therefore, the usual decimal notation of integers does not use leading zeros +# except for the zero itself, which would be denoted as an empty string otherwise. + +# Non-numeric data +# 0a -> 0a +# a -> a + +# Non-positive numbers +# 0 -> 0 +# 0. -> 0. +# 0.0 -> 0.0 +# 00 -> 00 +# 00. -> 00. +# 00.0 -> 00.0 +# -01 -> -01 +# -01. -> -01. +# -01.0 -> -01.0 + +# Leading zeros on positive numbers +# 01 -> 1 +# 01. -> 1. +# 01.0 -> 1.0 +# 010.0 -> 10.0 + +use strict; + +my $number = @ARGV[0]; + +$number =~ s/^0+([1-9][0-9]*(:?[.,]\d*)?)$/$1/; +print $number; diff --git a/challenge-002/andrezgz/perl5/ch-2.pl b/challenge-002/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..0f005d4553 --- /dev/null +++ b/challenge-002/andrezgz/perl5/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/ +# Challenge #2 +# Write a script that can convert integers to and from a base35 representation, +# using the characters 0-9 and A-Y. Dave Jacoby came up with nice description about base35, +# in case you needed some background. +# https://gist.github.com/jacoby/764bb4e8a5d3a819b5fbfa497fcb3454 + +#Usage +# ch-2.pl --to-base35 <base-10-number> +# ch-2.pl --from-base35 <base-35-number> + +use strict; + +my @base = (0..9,'A' .. 'Y'); + +print to_base35($ARGV[1]) if ($ARGV[0] eq '--to-base35'); +print from_base35($ARGV[1]) if ($ARGV[0] eq '--from-base35'); + + +sub to_base35 { + my ($i) = @_; + + return 0 if ($i == 0); + + my $sign = ($i =~ s/-//) ? '-' : ''; + + my $result; + + while ($i > 0) { + $result .= $base[$i % @base]; + $i = int($i / @base); + } + return $sign . reverse $result; +} + +sub from_base35 { + my ($t) = @_; + $t = reverse $t; + + my $sign = ($t =~ s/-//) ? '-' : ''; + + my $result = 0; + + for my $i (0..length($t)-1){ + my $c = substr($t, $i, 1); + my ($index) = grep { $base[$_] eq $c } (0 .. @base-1); + $result += $index * ( @base ** $i ); + } + return $sign . $result; +} diff --git a/challenge-002/james-smith/README b/challenge-002/james-smith/README deleted file mode 100644 index 573d9eb02a..0000000000 --- a/challenge-002/james-smith/README +++ /dev/null @@ -1 +0,0 @@ -Solution by James Smith diff --git a/challenge-002/james-smith/README.md b/challenge-002/james-smith/README.md new file mode 100644 index 0000000000..7ccf2dd6d9 --- /dev/null +++ b/challenge-002/james-smith/README.md @@ -0,0 +1,20 @@ +Solution by James Smith + +## Perl6 solution - currying + +Just learning Perl 6 - so thought I'd look at some of the functionality. +This challenge just asked for the use of **currying** as base conversion (at least <= 36 is similar for all bases....) + +> **Currying** is a technique to reduce the number of argument of a function by creating a wrapper function that substitutes some predefined values of the original function. + +In this case we construct a function which convert functions which convert from decimal to an arbitrary base (<=36) and visa versa. In the code these are `to_base` and `from_base`. Which take two numbers the base (or radix) and the number/string to convert. To *golf* this challenge the code is written recursively. + +To create the specific base35 functions (`to_35` and `fr_35`) we use `assuming` to curry these functions to the ones we want.... + +I've also included an arbitrary base conversion function which again can use currying (twice) to generate a specific converter - here I've created a `hx2b35` function. + +### Notes + +To be honest this is not the most efficient way of completing this challenge the performance of the curried functions is not that good in comparison to a hard coded version of to_base/from_base... + + diff --git a/challenge-002/james-smith/perl5/ch-2.pl b/challenge-002/james-smith/perl5/ch-2.pl index e807b1731a..a7922d205f 100644 --- a/challenge-002/james-smith/perl5/ch-2.pl +++ b/challenge-002/james-smith/perl5/ch-2.pl @@ -1,13 +1,11 @@ use strict; +use v5.26; -sub base35 { - my $o = ''; - for( shift; $_; ) { - $_ = ( $_ - (my $t = $_%35) )/ 35; - $o .= chr $t+($t<10?48:55); - } - return scalar reverse $o; -} +sub r35 {$_[0]?r35(substr $_[0],0,-1)*35+z(substr $_[0],-1):0} +sub b35 {$_[0]?b35(int$_[0]/35).x($_[0]%35):''} +sub x {chr$_[0]+($_[0]<10?48:55)} +sub z {(ord$_[0])-($_[0]=~/\d/?48:55)} -print $_,"\t", base35( $_ ),"\n" foreach @ARGV; +say b35 $_ for @ARGV; +say r35 b35 $_ for @ARGV; diff --git a/challenge-002/james-smith/perl6/ch-2.p6 b/challenge-002/james-smith/perl6/ch-2.p6 index 80d37995c2..9b9308b025 100644 --- a/challenge-002/james-smith/perl6/ch-2.p6 +++ b/challenge-002/james-smith/perl6/ch-2.p6 @@ -1,3 +1,12 @@ sub mp($n) {chr $n+($n < 10??48!!55)} -sub b35($n) {$n??b35(floor $n/35)~mp($n%35)!!''} -say b35 $_ for @*ARGS; +sub mb($n) {(ord $n)-($n ~~ /\d/ ?? 48 !! 55)} +sub to_base($r,$n){$n??to_base($r,floor $n/$r)~mp($n%$r)!!''} +sub from_base($r,$n){$n??from_base($r,substr $n,0,*-1)*$r+mb(substr $n,*-1)!!0} +sub convert($from,$to,$n){to_base($to,from_base($from,$n))} +my &to_35 = &to_base.assuming(35); +my &fr_35 = &from_base.assuming(35); +my &hx2b35 = &convert.assuming(16).assuming(35); + +say to_35 $_ for @*ARGS; +say fr_35 to_35 $_ for @*ARGS; +say hx2b35 $_.fmt('%X') for @*ARGS; diff --git a/challenge-002/ohmycloud/README b/challenge-002/ohmycloud/README new file mode 100644 index 0000000000..e993fafea7 --- /dev/null +++ b/challenge-002/ohmycloud/README @@ -0,0 +1 @@ +Solution by Ohmycloud diff --git a/challenge-002/ohmycloud/perl6/ch-1.p6 b/challenge-002/ohmycloud/perl6/ch-1.p6 new file mode 100644 index 0000000000..6cd4ddd03c --- /dev/null +++ b/challenge-002/ohmycloud/perl6/ch-1.p6 @@ -0,0 +1,3 @@ +for <00040 03.5 00.002 .02 .3 0.03> -> $n { + say ($n ~~ /^ 0* %% <?> /).postmatch; +} diff --git a/challenge-002/ohmycloud/perl6/ch-2.p6 b/challenge-002/ohmycloud/perl6/ch-2.p6 new file mode 100644 index 0000000000..fb61967308 --- /dev/null +++ b/challenge-002/ohmycloud/perl6/ch-2.p6 @@ -0,0 +1,13 @@ +# convert from base35 to base10 +sub decimal-to-base35(Str $n) { + :35($n.Str) +} + +say decimal-to-base35("OOM"); + +# convert from base10 to base35 +sub base35-to-decimal(Int $n) { + $n.base(35) +} + +say base35-to-decimal(30262);
\ No newline at end of file diff --git a/challenge-002/ozzy/README b/challenge-002/ozzy/README index 40d33b4f43..44e39371ef 100644 --- a/challenge-002/ozzy/README +++ b/challenge-002/ozzy/README @@ -1,36 +1 @@ Solution by Ozzy - ------------- -Challenge 1: ------------- -The simplest solution I could think of was conversion of a numeric (integer) string through -the use of the built-in Int method: - - my $a = "004" # Example string representing positive integer with leading zeros - my $b = $a.Int # Convert string to Int using built-in method Int, and so strip zeros - say $b # Print the Int; OUTPUT: 4 - -In a Perl6/Bash one-liner, this would look something like this: - - perl6 -pe '$_=.Int' <<< 004 - -but this, in itself, isn't very practical since Bash can do it with even less fuzz: - - a="004" - echo ${a##+(0)} - ------------- -Challenge 2: ------------- -The obvious way to go is probably the use of Perl6' .base and .parse-base methods: - -loop { - - my Str $a = prompt("\nPlease, give me a decimal (base-10) number : "); - say("$a in decimal notation is { $a.Int.base(35) } in base-35 notation."); - - $a = prompt("\nNow give me a base-35 number [A-Y0-9]: "); - say("$a in base-35 notation is { $a.parse-base(35) } in base-10 notation.") - -} - diff --git a/challenge-002/ozzy/perl6/ch-1.p6 b/challenge-002/ozzy/perl6/ch-1.p6 new file mode 100644 index 0000000000..cd98d990b9 --- /dev/null +++ b/challenge-002/ozzy/perl6/ch-1.p6 @@ -0,0 +1,16 @@ +#!/usr/bin/env perl6 +# The simplest solution I could think of was conversion of a numeric (integer) string through +# the use of the built-in Int method. Provide a commandline argument like "004", and the script +# will output: 4. +# +# In a Perl6/Bash one-liner, this would look something like this: +# perl6 -pe '$_=.Int' <<< "004" +# but this, in itself, isn't very practical since Bash can do it with even less fuzz: +# a="004" +# echo ${a##+(0)} + +sub MAIN (Str $numeric_string) { + + say $numeric_string.Int; +} + diff --git a/challenge-002/ozzy/perl6/ch-2.p6 b/challenge-002/ozzy/perl6/ch-2.p6 new file mode 100644 index 0000000000..bda3f4c372 --- /dev/null +++ b/challenge-002/ozzy/perl6/ch-2.p6 @@ -0,0 +1,12 @@ +#!/usr/bin/env perl6 +# The obvious way to go is probably the use of Perl6' .base and .parse-base methods: + +loop { + + my Str $a = prompt("\nPlease, give me a decimal (base-10) number : "); + say("$a in decimal notation is { $a.Int.base(35) } in base-35 notation."); + + $a = prompt("\nNow give me a base-35 number [A-Y0-9]: "); + say("$a in base-35 notation is { $a.parse-base(35) } in base-10 notation.") + + } diff --git a/stats/pwc-challenge-001.json b/stats/pwc-challenge-001.json index 79ab5ce073..115d42a9f4 100644 --- a/stats/pwc-challenge-001.json +++ b/stats/pwc-challenge-001.json @@ -1,32 +1,32 @@ { "subtitle" : { - "text" : "[Champions: 51] Last updated at 2019-04-03 20:00:16 GMT" + "text" : "[Champions: 51] Last updated at 2019-04-04 10:10:08 GMT" + }, + "tooltip" : { + "followPointer" : 1, + "pointerFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" }, "title" : { "text" : "Perl Weekly Challenge - CURRENT" }, - "legend" : { - "enabled" : 0 - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "format" : "{point.y}", "enabled" : 1 - } + }, + "borderWidth" : 0 } }, + "xAxis" : { + "type" : "category" + }, "drilldown" : { "series" : [ { - "name" : "Adam Russell", "id" : "Adam Russell", + "name" : "Adam Russell", "data" : [ [ "Perl 5", @@ -36,63 +36,63 @@ }, { "id" : "Alex Daniel", + "name" : "Alex Daniel", "data" : [ [ "Perl 6", 2 ] - ], - "name" : "Alex Daniel" + ] }, { "name" : "Antonio Gamiz", + "id" : "Antonio Gamiz", "data" : [ [ "Perl 6", 2 ] - ], - "id" : "Antonio Gamiz" + ] }, { - "name" : "Arpad Toth", - "id" : "Arpad Toth", "data" : [ [ "Perl 5", 2 ] - ] + ], + "name" : "Arpad Toth", + "id" : "Arpad Toth" }, { - "name" : "Athanasius", "data" : [ [ "Perl 5", 2 ] ], - "id" : "Athanasius" + "id" : "Athanasius", + "name" : "Athanasius" }, { + "id" : "Bob Kleemann", + "name" : "Bob Kleemann", "data" : [ [ "Perl 5", 2 ] - ], - "id" : "Bob Kleemann", - "name" : "Bob Kleemann" + ] }, { - "name" : "Daniel Mantovani", - "id" : "Daniel Mantovani", "data" : [ [ "Perl 5", 1 ] - ] + ], + "name" : "Daniel Mantovani", + "id" : "Daniel Mantovani" }, { "data" : [ @@ -105,64 +105,64 @@ "name" : "Dave Cross" }, { + "name" : "Dave Jacoby", + "id" : "Dave Jacoby", "data" : [ [ "Perl 5", 1 ] - ], - "id" : "Dave Jacoby", - "name" : "Dave Jacoby" + ] }, { "name" : "David Kayal", + "id" : "David Kayal", "data" : [ [ "Perl 5", 2 ] - ], - "id" : "David Kayal" + ] }, { - "id" : "Doug Schrag", "data" : [ [ "Perl 6", 2 ] ], - "name" : "Doug Schrag" + "name" : "Doug Schrag", + "id" : "Doug Schrag" }, { - "name" : "Duncan C. White", - "id" : "Duncan C. White", "data" : [ [ "Perl 5", 2 ] - ] + ], + "name" : "Duncan C. White", + "id" : "Duncan C. White" }, { "name" : "Eddy HS", + "id" : "Eddy HS", "data" : [ [ "Perl 5", 2 ] - ], - "id" : "Eddy HS" + ] }, { "name" : "Finley", + "id" : "Finley", "data" : [ [ "Perl 6", 2 ] - ], - "id" : "Finley" + ] }, { "data" : [ @@ -171,17 +171,17 @@ 1 ] ], - "id" : "Fred Zinn", - "name" : "Fred Zinn" + "name" : "Fred Zinn", + "id" : "Fred Zinn" }, { - "id" : "Freddie B", "data" : [ [ "Perl 5", 2 ] ], + "id" : "Freddie B", "name" : "Freddie B" }, { @@ -196,6 +196,7 @@ }, { "name" : "Jaldhar H. Vyas", + "id" : "Jaldhar H. Vyas", "data" : [ [ "Perl 5", @@ -205,32 +206,35 @@ "Perl 6", 2 ] - ], - "id" : "Jaldhar H. Vyas" + ] }, { "name" : "Dr James A. Smith", + "id" : "Dr James A. Smith", "data" : [ [ "Perl 5", 2 + ], + [ + "Perl 6", + 2 ] - ], - "id" : "Dr James A. Smith" + ] }, { - "name" : "Jeff", "data" : [ [ "Perl 5", 2 ] ], + "name" : "Jeff", "id" : "Jeff" }, { - "name" : "Jeremy Carman", "id" : "Jeremy Carman", + "name" : "Jeremy Carman", "data" : [ [ "Perl 5", @@ -263,7 +267,6 @@ ] }, { - "id" : "Jo Christian Oterhals", "data" : [ [ "Perl 5", @@ -274,7 +277,8 @@ 2 ] ], - "name" : "Jo Christian Oterhals" + "name" : "Jo Christian Oterhals", + "id" : "Jo Christian Oterhals" }, { "data" : [ @@ -292,23 +296,23 @@ }, { "id" : "John Barrett", + "name" : "John Barrett", "data" : [ [ "Perl 5", 1 ] - ], - "name" : "John Barrett" + ] }, { "id" : "Juan Caballero", + "name" : "Juan Caballero", "data" : [ [ "Perl 5", 2 ] - ], - "name" : "Juan Caballero" + ] }, { "data" : [ @@ -317,22 +321,22 @@ 2 ] ], - "id" : "Khalid", - "name" : "Khalid" + "name" : "Khalid", + "id" : "Khalid" }, { - "id" : "Kian-Meng Ang", "data" : [ [ "Perl 5", 2 ] ], - "name" : "Kian-Meng Ang" + "name" : "Kian-Meng Ang", + "id" : "Kian-Meng Ang" }, { - "name" : "Kivanc Yazan", "id" : "Kivanc Yazan", + "name" : "Kivanc Yazan", "data" : [ [ "Perl 5", @@ -369,24 +373,24 @@ "name" : "Laurent Rosenfeld" }, { + "name" : "Mark Senn", "id" : "Mark Senn", "data" : [ [ "Perl 6", 2 ] - ], - "name" : "Mark Senn" + ] }, { + "id" : "Martin Mugeni", + "name" : "Martin Mugeni", "data" : [ [ "Perl 6", 2 ] - ], - "id" : "Martin Mugeni", - "name" : "Martin Mugeni" + ] }, { "data" : [ @@ -399,8 +403,8 @@ "name" : "Neil Bowers" }, { - "name" : "Nick Logan", "id" : "Nick Logan", + "name" : "Nick Logan", "data" : [ [ "Perl 5", @@ -413,44 +417,44 @@ ] }, { - "name" : "Oleskii Tsvitenov", "data" : [ [ "Perl 5", 2 ] ], - "id" : "Oleskii Tsvitenov" + "id" : "Oleskii Tsvitenov", + "name" : "Oleskii Tsvitenov" }, { - "id" : "Ozzy", "data" : [ [ "Perl 6", 2 ] ], + "id" : "Ozzy", "name" : "Ozzy" }, { - "name" : "Pavel Jurca", "data" : [ [ "Perl 5", 2 ] ], - "id" : "Pavel Jurca" + "id" : "Pavel Jurca", + "name" : "Pavel Jurca" }, { "name" : "Pete Houston", + "id" : "Pete Houston", "data" : [ [ "Perl 5", 2 ] - ], - "id" : "Pete Houston" + ] }, { "data" : [ @@ -459,32 +463,30 @@ 2 ] ], - "id" : "Philippe Bruhat", - "name" : "Philippe Bruhat" + "name" : "Philippe Bruhat", + "id" : "Philippe Bruhat" }, { "id" : "Prajith P", + "name" : "Prajith P", "data" : [ [ "Perl 5", 1 ] - ], - "name" : "Prajith P" + ] }, { + "name" : "Sean Meininger", "id" : "Sean Meininger", "data" : [ [ "Perl 6", 2 ] - ], - "name" : "Sean Meininger" + ] }, { - "name" : "Simon Proctor", - "id" : "Simon Proctor", "data" : [ [ "Perl 5", @@ -494,20 +496,21 @@ "Perl 6", 2 ] - ] + ], + "id" : "Simon Proctor", + "name" : "Simon Proctor" }, { "name" : "Simon Reinhardt", + "id" : "Simon Reinhardt", "data" : [ [ "Perl 5", 2 ] - ], - "id" : "Simon Reinhardt" + ] }, { - "id" : "Steve Rogerson", "data" : [ [ "Perl 5", @@ -518,27 +521,28 @@ 2 ] ], - "name" : "Steve Rogerson" + "name" : "Steve Rogerson", + "id" : "Steve Rogerson" }, |
