aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-04 11:08:04 +0100
committerGitHub <noreply@github.com>2019-04-04 11:08:04 +0100
commitd585026be11a9d4014d0739deb2ad1b91580c43c (patch)
tree13c4fe0990714ba3d8c4df6a9e5612618cabc082
parentd0ab5891b69bc1383dce9debed25419ac2d57a07 (diff)
parenta632216a087e7a7a82cc505992259f0e260c81df (diff)
downloadperlweeklychallenge-club-d585026be11a9d4014d0739deb2ad1b91580c43c.tar.gz
perlweeklychallenge-club-d585026be11a9d4014d0739deb2ad1b91580c43c.tar.bz2
perlweeklychallenge-club-d585026be11a9d4014d0739deb2ad1b91580c43c.zip
Merge pull request #19 from bagheera-sands/ch1-perl6
challenge 1 + 2 perl 6 solutions and readme...
-rw-r--r--challenge-001/james-smith/perl6/ch-1.sh1
-rw-r--r--challenge-001/james-smith/perl6/ch-2.sh1
-rw-r--r--challenge-002/james-smith/README1
-rw-r--r--challenge-002/james-smith/README.md20
-rw-r--r--challenge-002/james-smith/perl5/ch-2.pl16
-rw-r--r--challenge-002/james-smith/perl6/ch-2.p613
6 files changed, 40 insertions, 12 deletions
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/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;