From 9a2b0d4d705046ff50f9ec0a3e953fd5ed62518f Mon Sep 17 00:00:00 2001 From: bagheera-sands Date: Wed, 3 Apr 2019 23:08:31 +0100 Subject: fixes to do 2nd half of problem and to use currying in perl6 --- challenge-002/james-smith/perl5/ch-2.pl | 16 +++++++--------- challenge-002/james-smith/perl6/ch-2.p6 | 13 +++++++++++-- 2 files changed, 18 insertions(+), 11 deletions(-) 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; -- cgit From 0e56fa880e077bf8a63a82ecd67758de35061e08 Mon Sep 17 00:00:00 2001 From: bagheera-sands Date: Thu, 4 Apr 2019 09:52:19 +0100 Subject: adding challenge 1 perl solutions and updating readme for challenge2 --- challenge-001/james-smith/perl6/ch-1.sh | 1 + challenge-001/james-smith/perl6/ch-2.sh | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-001/james-smith/perl6/ch-1.sh create mode 100644 challenge-001/james-smith/perl6/ch-2.sh 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;' -- cgit From a632216a087e7a7a82cc505992259f0e260c81df Mon Sep 17 00:00:00 2001 From: bagheera-sands Date: Thu, 4 Apr 2019 10:20:46 +0100 Subject: added readme. --- challenge-002/james-smith/README | 1 - challenge-002/james-smith/README.md | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) delete mode 100644 challenge-002/james-smith/README create mode 100644 challenge-002/james-smith/README.md 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... + + -- cgit