From 0691e05a6750e20c750d46bb4488a574bb147d8a Mon Sep 17 00:00:00 2001 From: drbaggy Date: Wed, 21 Apr 2021 13:00:56 +0100 Subject: change the n-1 to n/2 or (rather n>>1) ~ 50% faster - approx 80K calcs per second for the for loop version --- challenge-109/james-smith/perl/ch-1.pl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-109/james-smith/perl/ch-1.pl b/challenge-109/james-smith/perl/ch-1.pl index 1b89226a6a..c7820fb3d2 100644 --- a/challenge-109/james-smith/perl/ch-1.pl +++ b/challenge-109/james-smith/perl/ch-1.pl @@ -19,7 +19,7 @@ is( chowla_for($_), $answer[ $_ ] ) foreach 1..20; done_testing(); ## We will quickly run benchmarking... -## This suggests the for loop to be approximately 40-50% +## This suggests the for loop to be approximately 40% ## faster than the map solution... ## It is also 9 characters shorter... @@ -30,15 +30,15 @@ cmpthese(1_000_000, { ## ## Rate Map For -## Map 38670/s -- -33% -## For 57670/s 49% -- +## Map 59524/s -- -26% +## For 79936/s 34% -- ## sub chowla_map { my ($t,$n) = (0,@_); ## First attempt - the one-liner is to write this as a map, ## we add $t at the end which is the value returned - ( map { (($n%$_) || ($t+=$_)) && () } 2..$n-1 ), $t; + ( map { (($n%$_) || ($t+=$_)) && () } 2..$n>>1 ), $t; } sub chowla_for { @@ -56,7 +56,7 @@ sub chowla_for { ## ($condition)||($fun()) ## * in perl `foreach` and `for` are synonymous - so we can shorten - ($n%$_)||($t+=$_) for 2..$n-1; + ($n%$_)||($t+=$_) for 2..$n>>1; ## Now a quick "shortening" - if there is no specific return ## statement - we can just omit the return in the last statement... -- cgit From c6f8f5b8fc12980a57f4db78f1da65523ccce32f Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Mon, 26 Apr 2021 10:58:27 +0800 Subject: Remove extra line --- challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java index ad507def6e..3ae566171b 100644 --- a/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java +++ b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java @@ -122,8 +122,6 @@ public class NBoxesJavaFX extends Application group.getChildren().add(shape[i]); } - Rectangle shape1 = new Rectangle(); - Text[] text = new Text[M]; Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15); for (int j=0; j Date: Mon, 26 Apr 2021 00:56:32 -0600 Subject: initial --- challenge-110/mark-anderson/raku/ch-1.raku | 6 ++++++ challenge-110/mark-anderson/raku/ch-1.txt | 5 +++++ challenge-110/mark-anderson/raku/ch-2.raku | 5 +++++ challenge-110/mark-anderson/raku/ch-2.txt | 5 +++++ 4 files changed, 21 insertions(+) create mode 100644 challenge-110/mark-anderson/raku/ch-1.raku create mode 100644 challenge-110/mark-anderson/raku/ch-1.txt create mode 100644 challenge-110/mark-anderson/raku/ch-2.raku create mode 100644 challenge-110/mark-anderson/raku/ch-2.txt diff --git a/challenge-110/mark-anderson/raku/ch-1.raku b/challenge-110/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..c0fc86e49e --- /dev/null +++ b/challenge-110/mark-anderson/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku + +.say for "ch-1.txt".IO.lines + +.grep(/ ^ [ \s\+\d\d | \(\d\d\) | \d\d\d\d ] \s \d**10 $ /); + diff --git a/challenge-110/mark-anderson/raku/ch-1.txt b/challenge-110/mark-anderson/raku/ch-1.txt new file mode 100644 index 0000000000..48d6254741 --- /dev/null +++ b/challenge-110/mark-anderson/raku/ch-1.txt @@ -0,0 +1,5 @@ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 diff --git a/challenge-110/mark-anderson/raku/ch-2.raku b/challenge-110/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..e2cd9534d7 --- /dev/null +++ b/challenge-110/mark-anderson/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku + +say .map(*.join(",")).join("\n") given + +[Z] "ch-2.txt".IO.lines.map(*.split(",")); diff --git a/challenge-110/mark-anderson/raku/ch-2.txt b/challenge-110/mark-anderson/raku/ch-2.txt new file mode 100644 index 0000000000..716ebdce75 --- /dev/null +++ b/challenge-110/mark-anderson/raku/ch-2.txt @@ -0,0 +1,5 @@ +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f -- cgit From 653e1766c721fd92f84d36ecca8a811b727dacce Mon Sep 17 00:00:00 2001 From: Mark A Date: Mon, 26 Apr 2021 00:59:07 -0600 Subject: initial --- challenge-110/mark-anderson/raku/ch-1.raku | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-110/mark-anderson/raku/ch-1.raku b/challenge-110/mark-anderson/raku/ch-1.raku index c0fc86e49e..aaeb60658b 100644 --- a/challenge-110/mark-anderson/raku/ch-1.raku +++ b/challenge-110/mark-anderson/raku/ch-1.raku @@ -3,4 +3,3 @@ .say for "ch-1.txt".IO.lines .grep(/ ^ [ \s\+\d\d | \(\d\d\) | \d\d\d\d ] \s \d**10 $ /); - -- cgit From 4349ac4778e200e3a5865ddd94b3b044deee7f54 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 09:05:09 +0200 Subject: Task 1 done --- challenge-110/luca-ferrari/raku/ch-1.p6 | 10 ++++++++++ challenge-110/luca-ferrari/raku/phone.txt | 5 +++++ 2 files changed, 15 insertions(+) create mode 100644 challenge-110/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-110/luca-ferrari/raku/phone.txt diff --git a/challenge-110/luca-ferrari/raku/ch-1.p6 b/challenge-110/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..2eee9c781f --- /dev/null +++ b/challenge-110/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,10 @@ +#!raku + + +sub MAIN( Str $file-name = 'phone.txt' ) { + my @regexps = rx / ^ \s* <[+]> \d ** 2 \s+ \d ** 10 $ / + , rx / ^ \s* <[(]> \d ** 2 <[)]> \s+ \d ** 10 $ / + , rx / ^ \s* \d ** 4 \s+ \d ** 10 $ /; + + say $_ if $_ ~~ any @regexps for $file-name.IO.lines; +} diff --git a/challenge-110/luca-ferrari/raku/phone.txt b/challenge-110/luca-ferrari/raku/phone.txt new file mode 100644 index 0000000000..48d6254741 --- /dev/null +++ b/challenge-110/luca-ferrari/raku/phone.txt @@ -0,0 +1,5 @@ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 -- cgit From 3954c4d133c65ee56946a2aa4c35ef904818f7db Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 09:20:32 +0200 Subject: Task2 done --- challenge-110/luca-ferrari/raku/ch-2.p6 | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 challenge-110/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-110/luca-ferrari/raku/ch-2.p6 b/challenge-110/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..b388913a7f --- /dev/null +++ b/challenge-110/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,12 @@ +#!raku + +sub MAIN( Str $file-name = 'people.txt' ) { + + my @content; + @content.push: .split( ',' ) for $file-name.IO.lines; + + for 0 ..^ @content[ 0 ].elems -> $column { + my @row.push: @content[ $_ ][ $column ] for 0 ..^ @content.elems; + @row.join( ',' ).say; + } +} -- cgit From 0310f70d5102cb73bf8d677e2641c1c7e0acf764 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 09:51:02 +0200 Subject: Use map to compelte task 2 --- challenge-110/luca-ferrari/raku/ch-2.p6 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/challenge-110/luca-ferrari/raku/ch-2.p6 b/challenge-110/luca-ferrari/raku/ch-2.p6 index b388913a7f..ed01afe516 100644 --- a/challenge-110/luca-ferrari/raku/ch-2.p6 +++ b/challenge-110/luca-ferrari/raku/ch-2.p6 @@ -5,8 +5,12 @@ sub MAIN( Str $file-name = 'people.txt' ) { my @content; @content.push: .split( ',' ) for $file-name.IO.lines; - for 0 ..^ @content[ 0 ].elems -> $column { - my @row.push: @content[ $_ ][ $column ] for 0 ..^ @content.elems; - @row.join( ',' ).say; - } + # for 0 ..^ @content[ 0 ].elems -> $column { + # my @row.push: @content[ $_ ][ $column ] for 0 ..^ @content.elems; + # @row.join( ',' ).say; + # } + + my @transposed.push: @content.map: *[ $_ ] for 0 ..^ @content[ 0 ].elems; + $_.join( ',' ).say for @transposed; + } -- cgit From 5457a92ce0762633f0b0279134ceb2c4e5d9c2f6 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 26 Apr 2021 08:11:59 +0000 Subject: Task 1 & 2 --- challenge-110/perlboy1967/perl/ch-1.pl | 62 +++++++++++++++++++++++++++++++ challenge-110/perlboy1967/perl/ch-2.pl | 48 ++++++++++++++++++++++++ challenge-110/perlboy1967/perl/input1.txt | 5 +++ challenge-110/perlboy1967/perl/input2.txt | 5 +++ 4 files changed, 120 insertions(+) create mode 100755 challenge-110/perlboy1967/perl/ch-1.pl create mode 100755 challenge-110/perlboy1967/perl/ch-2.pl create mode 100644 challenge-110/perlboy1967/perl/input1.txt create mode 100644 challenge-110/perlboy1967/perl/input2.txt diff --git a/challenge-110/perlboy1967/perl/ch-1.pl b/challenge-110/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..b0a192c0b5 --- /dev/null +++ b/challenge-110/perlboy1967/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 110 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK1 +# +# Task 1 - Valid Phone Numbers +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use File::Basename qw(dirname); +use File::Slurp; + +use Test::More; +use Test::Deep; + +# Prototype(s) +sub validatePhoneNumber($); + +# Work relative from script directory +chdir(dirname($0)); + +cmp_deeply ([map { validatePhoneNumber($_) } read_file('input1.txt')], + ['0044 1148820341', + '+44 1148820341', + '(44) 1148820341']); + +done_testing; + + +# Interpreted the phone number rules to: +# 1) +nn nnnnnnnnnn: +# '+' followed 2-4 digits (see: https://countrycode.org/) +# followed by one or more spaces followed 6 or more digits +# 2) nnnn nnnnnnnnnn: +# '00' followed by 2-4 digits followed by one or more spaces +# followed by 6 or more digits +# 3) (nn) nnnnnnnnnn: +# '(' followed by 2-4 digits followed by ')' followed by +# one or more spaces followed by 6 or more digits + +sub validatePhoneNumber($) { + my ($n) = @_; + + # trim input + $n =~ s/^\s*(.+?)\s*$/$1/; + + my ($ccMinLen,$ccMaxLen) = (2,4); + my $pMinLen = 6; + + return $n if ($n =~ m/( + ^ \+ \d{$ccMinLen,$ccMaxLen} \s+ \d{$pMinLen,} $ | + ^ 00 \d{$ccMinLen,$ccMaxLen} \s+ \d{$pMinLen,} $ | + ^ \( \d{$ccMinLen,$ccMaxLen} \) \s+ \d{$pMinLen,} $ + )/x); + + return; +} + diff --git a/challenge-110/perlboy1967/perl/ch-2.pl b/challenge-110/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..656d263fc4 --- /dev/null +++ b/challenge-110/perlboy1967/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 110 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK2 +# +# Task 2 - Transpose File +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use File::Basename qw(dirname); +use File::Slurp; + +use Test::More; +use Test::Deep; + +# Prototype(s) +sub transposeFile($); + +# Work relative from script directory +chdir(dirname($0)); + +cmp_deeply ([transposeFile('input2.txt')], + ['name,Mohammad,Joe,Julie,Cristina', + 'age,45,20,35,10', + 'sex,m,m,f,f']); + +done_testing; + + +sub transposeFile($) { + my ($f) = @_; + + # Read and trim + my @lines = map { s/^\s*(.*?)\s*$/$1/; $_ } read_file($f); + + my @out; + map { + my $i = 0; + map { push(@{$out[$i++]},$_) } split(/,/, $_); + } @lines; + + return map {join(',',@$_)} @out; +} + diff --git a/challenge-110/perlboy1967/perl/input1.txt b/challenge-110/perlboy1967/perl/input1.txt new file mode 100644 index 0000000000..48d6254741 --- /dev/null +++ b/challenge-110/perlboy1967/perl/input1.txt @@ -0,0 +1,5 @@ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 diff --git a/challenge-110/perlboy1967/perl/input2.txt b/challenge-110/perlboy1967/perl/input2.txt new file mode 100644 index 0000000000..716ebdce75 --- /dev/null +++ b/challenge-110/perlboy1967/perl/input2.txt @@ -0,0 +1,5 @@ +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f -- cgit From 46b8aecc9397c6211a1e97a7f0638833726294a2 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 26 Apr 2021 09:14:21 +0100 Subject: tidying up code --- challenge-109/james-smith/perl/ch-1.pl | 14 ++++++-------- challenge-109/james-smith/perl/ch-2.pl | 5 ++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/challenge-109/james-smith/perl/ch-1.pl b/challenge-109/james-smith/perl/ch-1.pl index c7820fb3d2..6fccc59edd 100644 --- a/challenge-109/james-smith/perl/ch-1.pl +++ b/challenge-109/james-smith/perl/ch-1.pl @@ -34,15 +34,13 @@ cmpthese(1_000_000, { ## For 79936/s 34% -- ## -sub chowla_map { - my ($t,$n) = (0,@_); ## First attempt - the one-liner is to write this as a map, ## we add $t at the end which is the value returned +sub chowla_map { + my ($t,$n) = (0,@_); ( map { (($n%$_) || ($t+=$_)) && () } 2..$n>>1 ), $t; } -sub chowla_for { - my($t,$n)=(0,@_); ## This time we won't write this as a nasty map/reduce solution... ## @@ -55,12 +53,12 @@ sub chowla_for { ## can be rewritten as: ## ($condition)||($fun()) ## * in perl `foreach` and `for` are synonymous - so we can shorten - - ($n%$_)||($t+=$_) for 2..$n>>1; - - ## Now a quick "shortening" - if there is no specific return + ## Finally a quick "shortening" - if there is no specific return ## statement - we can just omit the return in the last statement... +sub chowla_for { + my($t,$n)=(0,@_); + ($n%$_)||($t+=$_) for 2..$n>>1; $t; } diff --git a/challenge-109/james-smith/perl/ch-2.pl b/challenge-109/james-smith/perl/ch-2.pl index 436e0fca19..bafd89bff8 100644 --- a/challenge-109/james-smith/perl/ch-2.pl +++ b/challenge-109/james-smith/perl/ch-2.pl @@ -67,8 +67,6 @@ say ''; sub sep { say '------------------------------------------------------------------------'; } sub show { say "@{$_}" foreach @{$_[0]}; } -sub four_square { - ## For a start we make the observation that ## ## $a + 2$b + $c + 2$d + $e + 2$f + $g = $n * 4 where $n is the total of a square @@ -94,6 +92,7 @@ sub four_square { ## ## We push any valid results to the array +sub four_square { my ($t,@n1,@res) = (0,@_); $t+=$_ foreach @n1; foreach my $b ( @n1 ) { @@ -110,7 +109,6 @@ sub four_square { return \@res; } -sub four_square_non_unique { ## Now let us make no assumption about the numbers... ## We choose 3 from the list... ## We then compute n (and check for no remainder) @@ -123,6 +121,7 @@ sub four_square_non_unique { ## will end up with 2 entries in the list ## where you swap the equivalent values... +sub four_square_non_unique { my ($t,$check,@n1,%res) = (0,"@{[sort @_]}",@_); $t+=$_ foreach @n1; foreach my $i ( 0..@n1-1 ) { -- cgit From a86f90bf5060f5cc194f82d4e05eff2954326f6b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 10:48:19 +0200 Subject: Blog references --- challenge-110/luca-ferrari/blog-1.txt | 1 + challenge-110/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-110/luca-ferrari/blog-1.txt create mode 100644 challenge-110/luca-ferrari/blog-2.txt diff --git a/challenge-110/luca-ferrari/blog-1.txt b/challenge-110/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..061a9b3d76 --- /dev/null +++ b/challenge-110/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/04/26/PerlWeeklyChallenge110.html#task1 diff --git a/challenge-110/luca-ferrari/blog-2.txt b/challenge-110/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e45816a13e --- /dev/null +++ b/challenge-110/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/04/26/PerlWeeklyChallenge110.html#task2 -- cgit From a05d653452a1c0c9366c6e62e8a51570068f6af0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 10:56:45 +0200 Subject: Better task 1 --- challenge-110/luca-ferrari/raku/ch-1.p6 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/challenge-110/luca-ferrari/raku/ch-1.p6 b/challenge-110/luca-ferrari/raku/ch-1.p6 index 2eee9c781f..d31e9fa671 100644 --- a/challenge-110/luca-ferrari/raku/ch-1.p6 +++ b/challenge-110/luca-ferrari/raku/ch-1.p6 @@ -2,9 +2,15 @@ sub MAIN( Str $file-name = 'phone.txt' ) { - my @regexps = rx / ^ \s* <[+]> \d ** 2 \s+ \d ** 10 $ / - , rx / ^ \s* <[(]> \d ** 2 <[)]> \s+ \d ** 10 $ / - , rx / ^ \s* \d ** 4 \s+ \d ** 10 $ /; + my $phone-regexp = rx/ \d ** 10 /; + my $prefix-regexp = rx/ + <[+]> \d ** 2 + || <[(]> \d ** 2 <[)]> + || \d ** 4 + /; - say $_ if $_ ~~ any @regexps for $file-name.IO.lines; + + my $phone-rx = rx / ^ \s* $prefix-regexp \s+ $phone-regexp $ /; + + $_.say if $_ ~~ $phone-rx for $file-name.IO.lines; } -- cgit From af2151c9a4306f5d3e4f28b86667e89e051f0f4a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Apr 2021 10:59:19 +0200 Subject: Input text files --- challenge-098/luca-ferrari/raku/input.txt | 1 + challenge-110/luca-ferrari/raku/people.txt | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 challenge-098/luca-ferrari/raku/input.txt create mode 100644 challenge-110/luca-ferrari/raku/people.txt diff --git a/challenge-098/luca-ferrari/raku/input.txt b/challenge-098/luca-ferrari/raku/input.txt new file mode 100644 index 0000000000..a614821304 --- /dev/null +++ b/challenge-098/luca-ferrari/raku/input.txt @@ -0,0 +1 @@ +1234ABCD5678EFGH diff --git a/challenge-110/luca-ferrari/raku/people.txt b/challenge-110/luca-ferrari/raku/people.txt new file mode 100644 index 0000000000..716ebdce75 --- /dev/null +++ b/challenge-110/luca-ferrari/raku/people.txt @@ -0,0 +1,5 @@ +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f -- cgit From 34595087f49d6292a09dfa9452efe1e49959639d Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 26 Apr 2021 08:12:34 -0400 Subject: 1st commit on 110_perl --- challenge-110/stuart-little/perl/ch-1.pl | 32 +++++++++++++++++++++++++++++++ challenge-110/stuart-little/perl/ch-2.pl | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 challenge-110/stuart-little/perl/ch-1.pl create mode 100755 challenge-110/stuart-little/perl/ch-2.pl diff --git a/challenge-110/stuart-little/perl/ch-1.pl b/challenge-110/stuart-little/perl/ch-1.pl new file mode 100755 index 0000000000..27d3e71249 --- /dev/null +++ b/challenge-110/stuart-little/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use warnings; +use v5.12; + +# run