From ca4a69f929246b55ca5390723123b874cd1d3efa Mon Sep 17 00:00:00 2001 From: John Barrett Date: Mon, 1 Apr 2019 14:39:23 +0100 Subject: Add gist to challenge-001 answer --- challenge-001/john-barrett/perl5/README.md | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 challenge-001/john-barrett/perl5/README.md diff --git a/challenge-001/john-barrett/perl5/README.md b/challenge-001/john-barrett/perl5/README.md new file mode 100644 index 0000000000..65a2fb6106 --- /dev/null +++ b/challenge-001/john-barrett/perl5/README.md @@ -0,0 +1,75 @@ +As published on https://perlweeklychallenge.org/blog/a-new-week-a-new-challenge/
+Submissions demonstrated here using [Reply](https://metacpan.org/pod/Reply). + +> 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’ is found in the string. + +I didn't write a script so I fail this challenge already. + +``` +0> my $foo = 'Perl Weekly Challenge' +$res[0] = "Perl Weekly Challenge" +1> $foo =~ s/e/E/g; +$res[1] = 5 +2> $foo +$res[2] = "PErl WEEkly ChallEngE" +3> +``` + +> 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’. + +I don't remember where I saw this, but it is my favourite Perl5 FizzBuzz: + +``` +4> { no strict; no warnings; map { (fizz)[$_%3].(buzz)[$_%5]||$_ } 1..20 } +$res[4] = [ + [0] 1, + [1] 2, + [2] "fizz", + [3] 4, + [4] "buzz", + [5] "fizz", + [6] 7, + [7] 8, + [8] "fizz", + [9] "buzz", + [10] 11, + [11] "fizz", + [12] 13, + [13] 14, + [14] "fizzbuzz", + [15] 16, + [16] 17, + [17] "fizz", + [18] 19, + [19] "buzz" +] +``` + +Since I didn't write that myself I also fail the second part of the challenge. Not going well. Let's see... + +``` +$ perl -MAcme::FizzBuzz -e '1' | head -n 20 | tr '[:upper:]' '[:lower:]' +1 +2 +fizz +4 +buzz +fizz +7 +8 +fizz +buzz +11 +fizz +13 +14 +fizzbuzz +16 +17 +fizz +19 +buzz +``` + + There! + -- cgit From 0910c851c89e3da165ad33a3454f1f330ec9f1c5 Mon Sep 17 00:00:00 2001 From: John Barrett Date: Mon, 1 Apr 2019 22:05:39 +0100 Subject: Challenge 2, part 1 --- challenge-002/john-barrett/perl5/ch-1.pl | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 challenge-002/john-barrett/perl5/ch-1.pl diff --git a/challenge-002/john-barrett/perl5/ch-1.pl b/challenge-002/john-barrett/perl5/ch-1.pl new file mode 100755 index 0000000000..1567a4ba3c --- /dev/null +++ b/challenge-002/john-barrett/perl5/ch-1.pl @@ -0,0 +1,9 @@ +#!/usr/bin/env perl + +# ./ch-1.pl 00123 + +$ARGV[0] > 0 && printf ( + ( $ARGV[0] =~ /\./ + ? "%g\n" + : "%d\n" ), $ARGV[0] +); -- cgit From f572002dfb9a3a01778dbecbfa44a22c5198a325 Mon Sep 17 00:00:00 2001 From: John Barrett Date: Mon, 1 Apr 2019 22:37:41 +0100 Subject: To base35 --- challenge-002/john-barrett/perl5/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 challenge-002/john-barrett/perl5/ch-2.pl diff --git a/challenge-002/john-barrett/perl5/ch-2.pl b/challenge-002/john-barrett/perl5/ch-2.pl new file mode 100755 index 0000000000..3293529824 --- /dev/null +++ b/challenge-002/john-barrett/perl5/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +# Usage, e.g. +# ./ch-2.pl -to-base35 123 +# ./ch-2.pl -from-base35 ABCD + +my %args = @ARGV; +my @charset = ( 0..9, 'A'..'Y' ); +my $base = @charset; + +say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'}; +#say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'}; + +sub to_base35 { + my ( $int ) = @_; + my $sign = ( $int < 0 ) ? '-' : ''; + my @digits; + $int = abs( $int ); + do { + push @digits, $charset[ $int % $base ]; + $int = int( $int / $base ); + say $int; + } while ( $int > 0 ); + $sign . join '', reverse @digits; +} + -- cgit From b6d10413a6a81c95f44ba5df5fbf5284e8ed1f7f Mon Sep 17 00:00:00 2001 From: John Barrett Date: Mon, 1 Apr 2019 23:05:45 +0100 Subject: From base35 --- challenge-002/john-barrett/perl5/ch-2.pl | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/challenge-002/john-barrett/perl5/ch-2.pl b/challenge-002/john-barrett/perl5/ch-2.pl index 3293529824..f18215e4f0 100755 --- a/challenge-002/john-barrett/perl5/ch-2.pl +++ b/challenge-002/john-barrett/perl5/ch-2.pl @@ -2,7 +2,7 @@ use strict; use warnings; -use feature 'say'; +use feature qw/ say /; # Usage, e.g. # ./ch-2.pl -to-base35 123 @@ -13,7 +13,21 @@ my @charset = ( 0..9, 'A'..'Y' ); my $base = @charset; say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'}; -#say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'}; +say from_base35( $args{'-from-base35'} ) if $args{'-from-base35'}; + +sub from_base35 { + my ( $base35 ) = @_; + my $sign = $base35 =~ s/-//g ? '-' : ''; + my @digits = split '', $base35; + my $idx = join '', @charset; + my $pos = 0; + my $val; + while ( my $char = pop @digits ) { + $val += index( $idx, $char ) * ( $base ** $pos ); + $pos++; + } + $sign . $val; +} sub to_base35 { my ( $int ) = @_; @@ -23,7 +37,6 @@ sub to_base35 { do { push @digits, $charset[ $int % $base ]; $int = int( $int / $base ); - say $int; } while ( $int > 0 ); $sign . join '', reverse @digits; } -- cgit