From b5467c3a5fe97e006e8946bc99e569199fe9d887 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 13 Dec 2021 04:12:49 +0000 Subject: Update blog.txt --- challenge-139/james-smith/blog.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-139/james-smith/blog.txt b/challenge-139/james-smith/blog.txt index 6a2d58b005..dc41a6e2bd 100644 --- a/challenge-139/james-smith/blog.txt +++ b/challenge-139/james-smith/blog.txt @@ -1 +1 @@ -https://github.com/drbaggy/perlweeklychallenge-club/new/master/challenge-139/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-139/james-smith -- cgit From 442fd31c6c75ce02cd911813531434ce87fe3c18 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Dec 2021 12:24:25 +0100 Subject: Task 2 done --- challenge-143/luca-ferrari/raku/ch-2.p6 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-143/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-143/luca-ferrari/raku/ch-2.p6 b/challenge-143/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..0ef97d3981 --- /dev/null +++ b/challenge-143/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,16 @@ +#!raku + +sub MAIN( Int $n where { $n > 0 } ) { + my @numbers = 1 ^..^ $n; + + # extract all the pairs to get the $n by multiplication + my @pairs = @numbers.grep( $n %% * ).map( { $_, $n / $_, $_ + $n / $_ } ); + + # now extract all the pairs couples that have a difference of one + for 0 ..^ @pairs.elems -> $left { + for $left ^..^ @pairs.elems -> $right { + "$n is stealth by { @pairs[ $left ][ 0..1 ].join( ',' ) } and { @pairs[ $right ][ 0..1 ].join( ',' ) }".say if @pairs[ $left ][ 2 ] - @pairs[ $right ][ 2 ] == any( 1, -1 ); + } + } + +} -- cgit From a9419595b246ec2ba94db259326024b980513dc3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 13 Dec 2021 13:22:04 +0100 Subject: Perl solutions for a boring week 143. --- challenge-143/abigail/README.md | 4 ++++ challenge-143/abigail/perl/ch-1.pl | 1 + challenge-143/abigail/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ challenge-143/abigail/t/ctest.ini | 8 ++++++++ challenge-143/abigail/t/input-1-1 | 2 ++ challenge-143/abigail/t/input-2-1 | 3 +++ challenge-143/abigail/t/output-1-1.exp | 2 ++ challenge-143/abigail/t/output-2-1.exp | 3 +++ 8 files changed, 56 insertions(+) create mode 100644 challenge-143/abigail/perl/ch-1.pl create mode 100644 challenge-143/abigail/perl/ch-2.pl create mode 100644 challenge-143/abigail/t/ctest.ini create mode 100644 challenge-143/abigail/t/input-1-1 create mode 100644 challenge-143/abigail/t/input-2-1 create mode 100644 challenge-143/abigail/t/output-1-1.exp create mode 100644 challenge-143/abigail/t/output-2-1.exp diff --git a/challenge-143/abigail/README.md b/challenge-143/abigail/README.md index d589af7261..aa835b7f1e 100644 --- a/challenge-143/abigail/README.md +++ b/challenge-143/abigail/README.md @@ -3,3 +3,7 @@ ## Part 1 * [Perl](perl/ch-1.pl) + +## Part 2 + +* [Perl](perl/ch-2.pl) diff --git a/challenge-143/abigail/perl/ch-1.pl b/challenge-143/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..8ae1d21ccb --- /dev/null +++ b/challenge-143/abigail/perl/ch-1.pl @@ -0,0 +1 @@ +use 5.01;say eval for <> # Maybe a challenge for language without eval. Not Perl diff --git a/challenge-143/abigail/perl/ch-2.pl b/challenge-143/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..2109647bf2 --- /dev/null +++ b/challenge-143/abigail/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Run as: perl ch-2.pl < input-file +# + +# +# And for the fourth week in succession we're solving need the +# majority of the problem by getting the divisors of a number. +# +# Perhaps it is time to say: +# "I'm completely out of ideas, let's skip this week". +# +# Big Yawn +# + +use Math::Prime::Util qw [divisors]; + +while (my $n = <>) { + # Put sum of divisor pairs in hash. + my %s = map {$_ + $n / $_ => 1} divisors 0 + $n; + # Any difference of 1? + say grep ({$s {$_ - 1}} keys %s) ? 1 : 0; +} diff --git a/challenge-143/abigail/t/ctest.ini b/challenge-143/abigail/t/ctest.ini new file mode 100644 index 0000000000..575704342b --- /dev/null +++ b/challenge-143/abigail/t/ctest.ini @@ -0,0 +1,8 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +2-1 = Given Examples diff --git a/challenge-143/abigail/t/input-1-1 b/challenge-143/abigail/t/input-1-1 new file mode 100644 index 0000000000..01dd3f8720 --- /dev/null +++ b/challenge-143/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +10 + 20 - 5 +(10 + 20 - 5) * 2 diff --git a/challenge-143/abigail/t/input-2-1 b/challenge-143/abigail/t/input-2-1 new file mode 100644 index 0000000000..024f0e0086 --- /dev/null +++ b/challenge-143/abigail/t/input-2-1 @@ -0,0 +1,3 @@ +36 +12 +6 diff --git a/challenge-143/abigail/t/output-1-1.exp b/challenge-143/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..e573c227f4 --- /dev/null +++ b/challenge-143/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +25 +50 diff --git a/challenge-143/abigail/t/output-2-1.exp b/challenge-143/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..2f1465d159 --- /dev/null +++ b/challenge-143/abigail/t/output-2-1.exp @@ -0,0 +1,3 @@ +1 +1 +0 -- cgit From 13d671c38f5bfdcec24b93cfd5bfd2ded929ce2a Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 13 Dec 2021 12:49:40 +0000 Subject: first submit --- challenge-143/james-smith/README.md | 29 ++++++++++++---------------- challenge-143/james-smith/blog.txt | 1 + challenge-143/james-smith/perl/ch-1.pl | 35 ++++++++++++++++++++++++++++++++++ challenge-143/james-smith/perl/ch-2.pl | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 challenge-143/james-smith/blog.txt create mode 100644 challenge-143/james-smith/perl/ch-1.pl create mode 100644 challenge-143/james-smith/perl/ch-2.pl diff --git a/challenge-143/james-smith/README.md b/challenge-143/james-smith/README.md index 90c0a8816c..67a6742064 100644 --- a/challenge-143/james-smith/README.md +++ b/challenge-143/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #142 +# Perl Weekly Challenge #143 You can find more information about this weeks, and previous weeks challenges at: @@ -10,32 +10,27 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-142/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith/perl -# Challenge 1 - Divisor Last Digit +# Challenge 1 - Calculator -***You are given positive integers, `$m` and `$n`. Write a script to find total count of divisors of `$m` having last digit `$n`.*** +***You are given a string, `$s`, containing mathematical expression. Write a script to print the result of the mathematical expression. To keep it simple, please only accept `+ - * ()`.***` ## The solution ```perl -sub divisor_last_digit { - my($m,$n)=@_; - ($n==1?1:0)+grep{$_%10==$n} - map{$m%$_?():$m==$_*$_?($_):($_,$m/$_)} - 2..sqrt$m; +sub evaluate { + my $str = shift; + 1 while $str =~ s/\(\s*([^()]*?)\s*\)/ evaluate($1) /e; + 1 while $str =~ s/(-?\d+)\s*\*\s*(-?\d+)/ $1 * $2 /e; + 1 while $str =~ s/(-?\d+)\s*([-+])\s*(-?\d+)/$2 eq '+' ? $1+$3 : $1-$3/e; + return $str; } ``` - * First we find all the factors - by looping over all values between `2` and the square root of `$m`. If the value is a factor, so is `$m/$_`. - * We have a special case when `$m` is a square to avoid including the square root twice. - * We then `grep` to obtain those which have the correct last digit. - * There is one extra special case if `$n` is `1` we have to add `1` as `1` is a factor which we miss out in our calculations (so we don't - equally get `$m` as a factor). +# Challenge 2 - Stealthy Number -# Challenge 2 - Sleep sort - -***Another joke sort similar to JortSort suggested by champion Adam Russell. You are given a list of numbers. Write a script to implement Sleep Sort.*** +***You are given a positive number, `$n`. Write a script to find out if the given number is Stealthy Number. A positive integer `N` is stealthy, if there exist positive integers `a`, `b`, `c`, `d` such that `a * b = c * d = N` and `a + b = c + d + 1`.*** To perform a sleep sort - we loop through the list of numbers, sleeping for `$value` seconds and updating the list of results with `$value` diff --git a/challenge-143/james-smith/blog.txt b/challenge-143/james-smith/blog.txt new file mode 100644 index 0000000000..efff1b655b --- /dev/null +++ b/challenge-143/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/new/master/challenge-143/james-smith diff --git a/challenge-143/james-smith/perl/ch-1.pl b/challenge-143/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..a4abfdeaab --- /dev/null +++ b/challenge-143/james-smith/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ '10 + 20 - 5', 25 ], + [ '(10 + 20 - 5) * 2', 50 ], + [ '(5 - 10) * -5', 25 ], + [ '10 * 10 - 6 * 6 - 8 * 8', 0 ], + [ '4 + 4 - 6 - 2', 0 ], + [ '(10 + 10) * (10 - 10) * (10 + 10) * (20 - (((20))))', 0 ], +); + +is( evaluate($_->[0]), $_->[1] ) foreach @TESTS; +is( eval( $_->[0]), $_->[1] ) foreach @TESTS; +cmpthese( 20000, { + 'evaluate' => sub { evaluate($_->[0]) for @TESTS }, + 'eval' => sub { eval( $_->[0]) for @TESTS }, +}); +done_testing(); + +sub evaluate { + my $str = shift; + 1 while $str =~ s/\(\s*([^()]*?)\s*\)/ evaluate($1) /e; + 1 while $str =~ s/(-?\d+)\s*\*\s*(-?\d+)/ $1 * $2 /e; + 1 while $str =~ s/(-?\d+)\s*([-+])\s*(-?\d+)/$2 eq '+' ? $1+$3 : $1-$3/e; + return $str; +} + diff --git a/challenge-143/james-smith/perl/ch-2.pl b/challenge-143/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..ddfe30d376 --- /dev/null +++ b/challenge-143/james-smith/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @ST = (4,12,24,36,40,60,72,84,112,120,144,180,220,240, + 252,264,312,336,360,364,400,420,432,480,504,540, + 544,600,612,660,672,684,760,792,840,864,900,924, + 936,1012,1080,1092,1104,1120,1200,1260,1300,1320); +my @NST = map { $_ + 15 } @ST; +my @TESTS = ( + [ 36, 1 ], + [ 12, 1 ], + [ 6, 0 ], + ( map { [$_ => 1] } @ST ), + ( map { [$_ => 0] } @NST ), +); + +is( stealthy_number($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub stealthy_number { + my($n,%c) = shift; + $n%$_||($c{$n/$_+$_ }++,$c{$n/$_+$_+1}++) for 1..sqrt$n; + (grep { $_ > 1 } values %c) ? 1 : 0; +} + -- cgit From 07b1884f7284cfcd9bd72829b5de4c4751b90100 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 13 Dec 2021 12:59:40 +0000 Subject: first submit --- challenge-143/james-smith/README.md | 43 ++++++------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/challenge-143/james-smith/README.md b/challenge-143/james-smith/README.md index 67a6742064..d7d0f3e04a 100644 --- a/challenge-143/james-smith/README.md +++ b/challenge-143/james-smith/README.md @@ -10,7 +10,7 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith # Challenge 1 - Calculator @@ -32,44 +32,13 @@ sub evaluate { ***You are given a positive number, `$n`. Write a script to find out if the given number is Stealthy Number. A positive integer `N` is stealthy, if there exist positive integers `a`, `b`, `c`, `d` such that `a * b = c * d = N` and `a + b = c + d + 1`.*** -To perform a sleep sort - we loop through the list of numbers, sleeping for `$value` seconds and updating the list of results with `$value` - ## The solution -We need to parallelise this process - -There are different ways of doing this `fork`, `threads`, `Promises`. - -We will go for the `threads` approach as it easier to implement that `Promises` but doesn't eat at memory by forking lots of times. - ```perl -use threads; -use threads::shared; -use Time::HiRes qw(sleep); - -my @res :shared; -my @list=map{0.001*int rand 3000}1..20; - -say "@list"; - -sub sleeper {sleep$_[0];push@res,$_[0]} - -threads->new( \&sleeper, $_ ) for @list; - -$_->join for threads->list; - -say for @res; +sub stealthy_number { + my($n,%c) = shift; + $n%$_||($c{$n/$_+$_ }++,$c{$n/$_+$_+1}++) for 1..sqrt$n; + (grep { $_ > 1 } values %c) ? 1 : 0; +} ``` -## Notes - - * We create a test set of 20 values between `0` and `3`. - * We fire off all the threads (`threads->new`) - * Wait for them to finish `$_->join for threads->list` - * Return the results. - * As well as `use threads`, we also `use threads::shared`. This lets us declare the results array `@res` shareable across all processes, which we need to collect the values. - -## Caveat - -Not all threads start at the same time so sometimes results don't quite come back in the same order - especially if values are close together. - -- cgit From 13ebea25187e04a08fb804ce8496c67dc373000b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 13 Dec 2021 13:38:44 +0100 Subject: Eval solutions for week 143, part 1 --- challenge-143/abigail/README.md | 8 ++++++++ challenge-143/abigail/bash/ch-1.sh | 1 + challenge-143/abigail/bc/ch-1.bc | 1 + challenge-143/abigail/lua/ch-1.lua | 13 +++++++++++++ challenge-143/abigail/node/ch-1.js | 10 ++++++++++ challenge-143/abigail/python/ch-1.py | 13 +++++++++++++ challenge-143/abigail/r/ch-1.r | 16 ++++++++++++++++ challenge-143/abigail/ruby/ch-1.rb | 7 +++++++ challenge-143/abigail/tcl/ch-1.tcl | 5 +++++ 9 files changed, 74 insertions(+) create mode 100644 challenge-143/abigail/bash/ch-1.sh create mode 100644 challenge-143/abigail/bc/ch-1.bc create mode 100644 challenge-143/abigail/lua/ch-1.lua create mode 100644 challenge-143/abigail/node/ch-1.js create mode 100644 challenge-143/abigail/python/ch-1.py create mode 100644 challenge-143/abigail/r/ch-1.r create mode 100644 challenge-143/abigail/ruby/ch-1.rb create mode 100644 challenge-143/abigail/tcl/ch-1.tcl diff --git a/challenge-143/abigail/README.md b/challenge-143/abigail/README.md index aa835b7f1e..48a5093c46 100644 --- a/challenge-143/abigail/README.md +++ b/challenge-143/abigail/README.md @@ -2,7 +2,15 @@ ## Part 1 +* [Bash](bash/ch-1.sh) +* [Bc](bc/ch-1.bc) +* [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) +* [R](r/ch-1.r) +* [Ruby](ruby/ch-1.rb) +* [Tcl](tcl/ch-1.tcl) ## Part 2 diff --git a/challenge-143/abigail/bash/ch-1.sh b/challenge-143/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..db6e8363a1 --- /dev/null +++ b/challenge-143/abigail/bash/ch-1.sh @@ -0,0 +1 @@ +while read l;do echo $(($l));done diff --git a/challenge-143/abigail/bc/ch-1.bc b/challenge-143/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..dc63c2a8f4 --- /dev/null +++ b/challenge-143/abigail/bc/ch-1.bc @@ -0,0 +1 @@ +bc < input-file diff --git a/challenge-143/abigail/lua/ch-1.lua b/challenge-143/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..524751df03 --- /dev/null +++ b/challenge-143/abigail/lua/ch-1.lua @@ -0,0 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + (load ("print (" .. line .. ")")) () +end diff --git a/challenge-143/abigail/node/ch-1.js b/challenge-143/abigail/node/ch-1.js new file mode 100644 index 0000000000..33be42296f --- /dev/null +++ b/challenge-143/abigail/node/ch-1.js @@ -0,0 +1,10 @@ +#!/usr/local/bin/node + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => {console . log (eval (line))}) + diff --git a/challenge-143/abigail/python/ch-1.py b/challenge-143/abigail/python/ch-1.py new file mode 100644 index 0000000000..3b67604548 --- /dev/null +++ b/challenge-143/abigail/python/ch-1.py @@ -0,0 +1,13 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): print (eval (line)) diff --git a/challenge-143/abigail/r/ch-1.r b/challenge-143/abigail/r/ch-1.r new file mode 100644 index 0000000000..238fc25f08 --- /dev/null +++ b/challenge-143/abigail/r/ch-1.r @@ -0,0 +1,16 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + n <- readLines (stdin, n = 1) + if (length (n) == 0) { + break + } + print (eval (parse (text = n))) +} diff --git a/challenge-143/abigail/ruby/ch-1.rb b/challenge-143/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..314b69797c --- /dev/null +++ b/challenge-143/abigail/ruby/ch-1.rb @@ -0,0 +1,7 @@ +#!/usr/bin/ruby + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| puts (eval _) end diff --git a/challenge-143/abigail/tcl/ch-1.tcl b/challenge-143/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..3796f3ae1c --- /dev/null +++ b/challenge-143/abigail/tcl/ch-1.tcl @@ -0,0 +1,5 @@ +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin line] >= 0} {puts [expr $line]} -- cgit From 1c507d143478172efe90141f51097e28cadc96c6 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Dec 2021 16:09:21 +0100 Subject: Task 1 done --- challenge-143/luca-ferrari/raku/ch-1.p6 | 116 ++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 challenge-143/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-143/luca-ferrari/raku/ch-1.p6 b/challenge-143/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..f3823bd145 --- /dev/null +++ b/challenge-143/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,116 @@ +#!raku + + +my Str $OPERATOR_ADD = '+'; +my Str $OPERATOR_MINUS = '-'; +my Str $OPERATOR_MULTIPLY = '*'; +my Str $OPERATOR_DIVIDE = '/'; + + + + +grammar Calculator { + rule TOP { + ^ $ + } + + rule expression { + | + %% $=([$OPERATOR_ADD|$OPERATOR_MINUS]) + | + } + rule operation { + + %% $=([$OPERATOR_MULTIPLY|$OPERATOR_DIVIDE]) + } + + rule operand { + | + | + } + + rule parenthesized-expression { + '(' ')' + } + + token number { \d+ } +} + + +class CalculatorActions { + method TOP($/) { + $/.make: $.made + } + + + method parenthesized-expression($/) { + $/.make: $.made + } + + method number($/) { + $/.make: +$/ + } + + + method operand($/) { + $/.make: $ ?? $.Int !! $.made; + } + + + method do-compute( $left-operand, $operator, $right-operand ) { + given $operator { + when $OPERATOR_ADD { $left-operand + $right-operand } + when $OPERATOR_MINUS { $left-operand - $right-operand } + when $OPERATOR_MULTIPLY { $left-operand * $right-operand } + when $OPERATOR_DIVIDE { $left-operand / $right-operand } + } + } + + + method do-compute-all( $left-operand is rw, @operators, @operands ) { + while ( @operators.elems > 0 ) { + $left-operand = self.do-compute( $left-operand, + @operators.pop, + @operands.pop ); + } + } + + method operation($/) { + # left part + my $result = $[ 0 ].made; + + # if there is a right part ... + if $ { + my @operators = $.map( *.Str ); + my @operands = $[ 1..* ].map( *.made ); + + self.do-compute-all( $result, @operators, @operands ); + } + + $/.make: $result; + } + + + + method expression($/) { + if $ { + $/.make: $.made + } + else { + my $result = $[ 0 ].made; + + if $ { + my @operators = $.map( *.Str ); + my @operands = $[ 1..* ].map( *.made ); + + self.do-compute-all( $result, @operators, @operands ); + + } + + $/.make: $result; + } + } +} + +sub MAIN( Str $expr ) { + my $calculator = Calculator.parse( $expr, :actions( CalculatorActions ) ); + "{ $expr } = { $calculator.made }".say; +} -- cgit From a7ce07821aad2cde7884ec1c6e2e9ea145787107 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 13 Dec 2021 16:18:04 +0000 Subject: Add Perl solution to challenge 27 --- challenge-027/paulo-custodio/Makefile | 2 ++ challenge-027/paulo-custodio/README | 1 + challenge-027/paulo-custodio/perl/ch-1.pl | 23 ++++++++++++++++++ challenge-027/paulo-custodio/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++ challenge-027/paulo-custodio/t/test-1.yaml | 5 ++++ challenge-027/paulo-custodio/t/test-2.yaml | 5 ++++ 6 files changed, 74 insertions(+) create mode 100644 challenge-027/paulo-custodio/Makefile create mode 100644 challenge-027/paulo-custodio/README create mode 100644 challenge-027/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-027/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-027/paulo-custodio/t/test-1.yaml create mode 100644 challenge-027/paulo-custodio/t/test-2.yaml diff --git a/challenge-027/paulo-custodio/Makefile b/challenge-027/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-027/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-027/paulo-custodio/README b/challenge-027/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-027/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-027/paulo-custodio/perl/ch-1.pl b/challenge-027/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e4b4feca3e --- /dev/null +++ b/challenge-027/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# Challenge 027 +# +# Task #1 +# Write a script to find the intersection of two straight lines. The +# co-ordinates of the two lines should be provided as command line parameter. +# For example: +# +# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d). +# +# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s). +# +# The script should print the co-ordinates of point of intersection of the +# above two lines. + +use Modern::Perl; + +my($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4) = @ARGV; +my $D = ($x1-$x2)*($y3-$y4)-($y1-$y2)*($x3-$x4); +my $x = (($x1*$y2-$y1*$x2)*($x3-$x4)-($x1-$x2)*($x3*$y4-$y3*$x4))/$D; +my $y = (($x1*$y2-$y1*$x2)*($y3-$y4)-($y1-$y2)*($x3*$y4-$y3*$x4))/$D; +say sprintf("%.1f %.1f", $x, $y); diff --git a/challenge-027/paulo-custodio/perl/ch-2.pl b/challenge-027/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8091076b58 --- /dev/null +++ b/challenge-027/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Challenge 027 +# +# Task #2 +# Write a script that allows you to capture/display historical data. It could +# be an object or a scalar. For example +# +# my $x = 10; $x = 20; $x -= 5; +# +# After the above operations, it should list $x historical value in order. + +use Modern::Perl; + +{ + package LoggingScalar; + sub TIESCALAR { + my($class, $value) = @_; + return bless [$value], $class; + } + sub FETCH { + my($self) = @_; + return $self->[-1]; + } + sub STORE { + my($self, $value) = @_; + push @$self, $value; + } + sub show_hist { + my($self) = @_; + say join(" ", @$self); + } +} + +tie my $x, 'LoggingScalar', 10; +$x = 20; +$x -= 5; +tied($x)->show_hist(); diff --git a/challenge-027/paulo-custodio/t/test-1.yaml b/challenge-027/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..31ebb25d9b --- /dev/null +++ b/challenge-027/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 15 10 49 25 29 5 32 32 + input: + output: 30.3 16.8 diff --git a/challenge-027/paulo-custodio/t/test-2.yaml b/challenge-027/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..78447f751a --- /dev/null +++ b/challenge-027/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 10 20 15 -- cgit From 8d4cd3c68f9bf9ec693fedf996ac00bb499e22e4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 13 Dec 2021 16:19:32 +0000 Subject: whitespace --- challenge-143/paulo-custodio/perl/ch-1.pl | 98 ++++++++++++++--------------- challenge-143/paulo-custodio/perl/ch-2.pl | 42 ++++++------- challenge-143/paulo-custodio/python/ch-1.py | 6 +- challenge-143/paulo-custodio/python/ch-2.py | 30 ++++----- 4 files changed, 88 insertions(+), 88 deletions(-) diff --git a/challenge-143/paulo-custodio/perl/ch-1.pl b/challenge-143/paulo-custodio/perl/ch-1.pl index 90b6fe6506..73a806191b 100644 --- a/challenge-143/paulo-custodio/perl/ch-1.pl +++ b/challenge-143/paulo-custodio/perl/ch-1.pl @@ -3,10 +3,10 @@ # TASK #1 > Calculator # Submitted by: Mohammad S Anwar # You are given a string, $s, containing mathematical expression. -# +# # Write a script to print the result of the mathematical expression. To keep # it simple, please only accept + - * (). -# +# # Example 1: # Input: $s = "10 + 20 - 5" # Output: 25 @@ -20,61 +20,61 @@ use Modern::Perl; # ($input, $value) = expr($input) sub expr { - my($input) = @_; - ($input, my $value) = factor($input); - while (1) { - if ($input =~ s/^\s*\*//) { - ($input, my $b) = factor($input); - $value *= $b; - } - elsif ($input =~ s/^\s*\///) { - ($input, my $b) = factor($input); - $value /= $b; - } - elsif ($input =~ /^\s*(?:\)|$)/) { - return ($input, $value); - } - else { - die "expected / or * at: $input\n"; - } - } + my($input) = @_; + ($input, my $value) = factor($input); + while (1) { + if ($input =~ s/^\s*\*//) { + ($input, my $b) = factor($input); + $value *= $b; + } + elsif ($input =~ s/^\s*\///) { + ($input, my $b) = factor($input); + $value /= $b; + } + elsif ($input =~ /^\s*(?:\)|$)/) { + return ($input, $value); + } + else { + die "expected / or * at: $input\n"; + } + } } # ($input, $value) = factor($input) sub factor { - my($input) = @_; - ($input, my $value) = term($input); - while (1) { - if ($input =~ s/^\s*\+//) { - ($input, my $b) = term($input); - $value += $b; - } - elsif ($input =~ s/^\s*\-//) { - ($input, my $b) = term($input); - $value -= $b; - } - else { - return ($input, $value); - } - } + my($input) = @_; + ($input, my $value) = term($input); + while (1) { + if ($input =~ s/^\s*\+//) { + ($input, my $b) = term($input); + $value += $b; + } + elsif ($input =~ s/^\s*\-//) { + ($input, my $b) = term($input); + $value -= $b; + } + else { + return ($input, $value); + } + } } # ($input, $value) = term($input) sub term { - my($input) = @_; - while (1) { - if ($input =~ s/^\s*([-+]?\d+)//) { - return ($input, $1); - } - elsif ($input =~ s/^\s*\(//) { - ($input, my $value) = expr($input); - $input =~ s/^\s*\)// or die "expected ) at: $input\n"; - return ($input, $value); - } - else { - die "expected ( or number at: $input\n"; - } - } + my($input) = @_; + while (1) { + if ($input =~ s/^\s*([-+]?\d+)//) { + return ($input, $1); + } + elsif ($input =~ s/^\s*\(//) { + ($input, my $value) = expr($input); + $input =~ s/^\s*\)// or die "expected ) at: $input\n"; + return ($input, $value); + } + else { + die "expected ( or number at: $input\n"; + } + } } my $input = "@ARGV"; diff --git a/challenge-143/paulo-custodio/perl/ch-2.pl b/challenge-143/paulo-custodio/perl/ch-2.pl index a805a8180b..9433cd520d 100644 --- a/challenge-143/paulo-custodio/perl/ch-2.pl +++ b/challenge-143/paulo-custodio/perl/ch-2.pl @@ -3,46 +3,46 @@ # TASK #2 > Stealthy Number # Submitted by: Mohammad S Anwar # You are given a positive number, $n. -# +# # Write a script to find out if the given number is Stealthy Number. -# +# # A positive integer N is stealthy, if there exist positive integers a, b, c, d # such that a * b = c * d = N and a + b = c + d + 1. -# +# # Example 1 # Input: $n = 36 # Output: 1 -# +# # Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. # Example 2 # Input: $n = 12 # Output: 1 -# +# # Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 # Example 3 # Input: $n = 6 # Output: 0 -# +# # Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 use Modern::Perl; sub is_stealthy { - my($n) = @_; - for my $a (1..$n) { - if ($n % $a == 0) { - my $b = $n / $a; # a*b=n - for my $c (1..$n) { - if ($n % $c == 0) { - my $d = $n / $c; # c*d=n - if ($a+$b==$c+$d+1) { - return 1; - } - } - } - } - } - return 0; + my($n) = @_; + for my $a (1..$n) { + if ($n % $a == 0) { + my $b = $n / $a; # a*b=n + for my $c (1..$n) { + if ($n % $c == 0) { + my $d = $n / $c; # c*d=n + if ($a+$b==$c+$d+1) { + return 1; + } + } + } + } + } + return 0; } my $n = shift//1; diff --git a/challenge-143/paulo-custodio/python/ch-1.py b/challenge-143/paulo-custodio/python/ch-1.py index a81c52fd0e..7705a4eafe 100644 --- a/challenge-143/paulo-custodio/python/ch-1.py +++ b/challenge-143/paulo-custodio/python/ch-1.py @@ -3,10 +3,10 @@ # TASK #1 > Calculator # Submitted by: Mohammad S Anwar # You are given a string, $s, containing mathematical expression. -# +# # Write a script to print the result of the mathematical expression. To keep # it simple, please only accept + - * (). -# +# # Example 1: # Input: $s = "10 + 20 - 5" # Output: 25 @@ -46,7 +46,7 @@ def factor(input): value -= b else: return input, value - + def term(input): input = input.strip()+" " match = re.match(r"[-+]?\d+", input) diff --git a/challenge-143/paulo-custodio/python/ch-2.py b/challenge-143/paulo-custodio/python/ch-2.py index 3d1976d3f0..850ebfac33 100644 --- a/challenge-143/paulo-custodio/python/ch-2.py +++ b/challenge-143/paulo-custodio/python/ch-2.py @@ -3,40 +3,40 @@ # TASK #2 > Stealthy Number # Submitted by: Mohammad S Anwar # You are given a positive number, $n. -# +# # Write a script to find out if the given number is Stealthy Number. -# +# # A positive integer N is stealthy, if there exist positive integers a, b, c, d # such that a * b = c * d = N and a + b = c + d + 1. -# +# # Example 1 # Input: $n = 36 # Output: 1 -# +# # Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. # Example 2 # Input: $n = 12 # Output: 1 -# +# # Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 # Example 3 # Input: $n = 6 # Output: 0 -# +# # Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 import sys def is_stealthy(n): - for a in range(1, n+1): - if n%a==0: - b = n/a # a*b=n - for c in range(1, n+1): - if n%c==0: - d = n/c # c*d=n - if a+b==c+d+1: - return 1 - return 0 + for a in range(1, n+1): + if n%a==0: + b = n/a # a*b=n + for c in range(1, n+1): + if n%c==0: + d = n/c # c*d=n + if a+b==c+d+1: + return 1 + return 0 n = int(sys.argv[1]) print(is_stealthy(n)) -- cgit From 8c1fdda84743187534ae4615a0dfcb41283293e4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 13 Dec 2021 16:28:04 +0000 Subject: Add Python solution to challenge 27 --- challenge-027/paulo-custodio/python/ch-1.py | 23 +++++++++++++++++++++++ challenge-027/paulo-custodio/python/ch-2.py | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 challenge-027/paulo-custodio/python/ch-1.py create mode 100644 challenge-027/paulo-custodio/python/ch-2.py diff --git a/challenge-027/paulo-custodio/python/ch-1.py b/challenge-027/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..8f3e6c9731 --- /dev/null +++ b/challenge-027/paulo-custodio/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +# Challenge 027 +# +# Task #1 +# Write a script to find the intersection of two straight lines. The +# co-ordinates of the two lines should be provided as command line parameter. +# For example: +# +# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d). +# +# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s). +# +# The script should print the co-ordinates of point of intersection of the +# above two lines. + +import sys + +x1,y1,x2,y2,x3,y3,x4,y4 = [int(x) for x in sys.argv[1:9]] +D = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) +x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/D +y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/D +print("{:.1f} {:.1f}".format(x, y)) diff --git a/challenge-027/paulo-custodio/python/ch-2.py b/challenge-027/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..99fd57554b --- /dev/null +++ b/challenge-027/paulo-custodio/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +# Challenge 027 +# +# Task #2 +# Write a script that allows you to capture/display historical data. It could +# be an object or a scalar. For example +# +# my $x = 10; $x = 20; $x -= 5; +# +# After the above operations, it should list $x historical value in order. + +class LoggingScalar: + def __init__(self, value): + self.values = [value] + def set(self, value): + self.values.append(value) + def get(self): + return self.values[-1] + def show_hist(self): + print(" ".join([str(x) for x in self.values])) + +x = LoggingScalar(10) +x.set(20) +x.set(x.get()-5) +x.show_hist() -- cgit From bd6bd5ad69f26703e2baa26b8bf9e1bcaeb16e22 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Dec 2021 17:28:21 +0100 Subject: Task 2 done in PostgreSQL. Raku tasks improved --- challenge-143/luca-ferrari/postgresql/ch-2.sql | 32 ++++++++++++++++++++++++++ challenge-143/luca-ferrari/raku/ch-1.p6 | 8 +++++++ challenge-143/luca-ferrari/raku/ch-2.p6 | 11 +++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 challenge-143/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-143/luca-ferrari/postgresql/ch-2.sql b/challenge-143/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..4f67625b37 --- /dev/null +++ b/challenge-143/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,32 @@ +CREATE OR REPLACE FUNCTION f_stealth( needle int ) + RETURNS int +AS $CODE$ + WITH numbers AS ( SELECT generate_series( 2, needle ) as n ) + , pairs AS ( + SELECT needle as n + , n as divisor + , needle / n as divisor_2 + , case needle % n + when 0 then n + needle / n + else null + end as summix + from numbers + ) + , stealth as ( + select * + , abs( summix - lag( summix, 1, summix ) over ( ORDER BY summix DESC ) ) as lag + , abs( summix - lead( summix, 1, summix ) over ( ORDER BY summix DESC ) ) as lead + FROM pairs + where summix is not null + ) + + SELECT CASE count(*) + WHEN 0 THEN 0 + ELSE 1 + END + FROM stealth + WHERE ( lag = 1 or lead = 1 ); + + + $CODE$ + LANGUAGE sql; diff --git a/challenge-143/luca-ferrari/raku/ch-1.p6 b/challenge-143/luca-ferrari/raku/ch-1.p6 index f3823bd145..3ffbf3107b 100644 --- a/challenge-143/luca-ferrari/raku/ch-1.p6 +++ b/challenge-143/luca-ferrari/raku/ch-1.p6 @@ -55,6 +55,8 @@ class CalculatorActions { } + # Computes a single operation in the form + # a + b method do-compute( $left-operand, $operator, $right-operand ) { given $operator { when $OPERATOR_ADD { $left-operand + $right-operand } @@ -65,6 +67,12 @@ class CalculatorActions { } + # Computes all the operation given the first operand, the set of operators + # and the other operands. + # For example: + # 1 + 2 * 3 + # becomes + # do-compute-all( 1, [+,*], [2,3]) method do-compute-all( $left-operand is rw, @operators, @operands ) { while ( @operators.elems > 0 ) { $left-operand = self.do-compute( $left-operand, diff --git a/challenge-143/luca-ferrari/raku/ch-2.p6 b/challenge-143/luca-ferrari/raku/ch-2.p6 index 0ef97d3981..b0be43e59c 100644 --- a/challenge-143/luca-ferrari/raku/ch-2.p6 +++ b/challenge-143/luca-ferrari/raku/ch-2.p6 @@ -1,16 +1,23 @@ #!raku -sub MAIN( Int $n where { $n > 0 } ) { +sub MAIN( Int $n where { $n > 0 }, Bool :$verbose = False ) { my @numbers = 1 ^..^ $n; # extract all the pairs to get the $n by multiplication my @pairs = @numbers.grep( $n %% * ).map( { $_, $n / $_, $_ + $n / $_ } ); # now extract all the pairs couples that have a difference of one + my $found = False; for 0 ..^ @pairs.elems -> $left { for $left ^..^ @pairs.elems -> $right { - "$n is stealth by { @pairs[ $left ][ 0..1 ].join( ',' ) } and { @pairs[ $right ][ 0..1 ].join( ',' ) }".say if @pairs[ $left ][ 2 ] - @pairs[ $right ][ 2 ] == any( 1, -1 ); + if @pairs[ $left ][ 2 ] - @pairs[ $right ][ 2 ] == any( 1, -1 ) { + $found = True; + "$n is stealth by { @pairs[ $left ][ 0..1 ].join( ',' ) } and { @pairs[ $right ][ 0..1 ].join( ',' ) }".say if $verbose; + } } } + "1".say and exit if $found; + "0".say; + } -- cgit From 5706bc204139c7cb84ff7778443d69952716b797 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Dec 2021 17:35:35 +0100 Subject: Blog references --- challenge-143/luca-ferrari/blog-1.txt | 1 + challenge-143/luca-ferrari/blog-2.txt | 1 + challenge-143/luca-ferrari/blog-3.txt | 1 + 3 files changed, 3 insertions(+) create mode 100644 challenge-143/luca-ferrari/blog-1.txt create mode 100644 challenge-143/luca-ferrari/blog-2.txt create mode 100644 challenge-143/luca-ferrari/blog-3.txt diff --git a/challenge-143/luca-ferrari/blog-1.txt b/challenge-143/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..fbfeba6f6c --- /dev/null +++ b/challenge-143/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/13/PerlWeeklyChallenge143.html#task1 diff --git a/challenge-143/luca-ferrari/blog-2.txt b/challenge-143/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..d7ed898652 --- /dev/null +++ b/challenge-143/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/13/PerlWeeklyChallenge143.html#task2 diff --git a/challenge-143/luca-ferrari/blog-3.txt b/challenge-143/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..bd24cc0a68 --- /dev/null +++ b/challenge-143/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/13/PerlWeeklyChallenge143.html#task2pg -- cgit From 526c322c070f34841e5a75a25a1d86bd370d88e8 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 13 Dec 2021 17:22:06 +0000 Subject: some notes --- challenge-143/james-smith/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/challenge-143/james-smith/README.md b/challenge-143/james-smith/README.md index d7d0f3e04a..fc9a9a857d 100644 --- a/challenge-143/james-smith/README.md +++ b/challenge-143/james-smith/README.md @@ -18,6 +18,22 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/ja ## The solution +The simple solution is just to "`eval`" the string, but where is the fun in that... We can either go for a tokenizing parser - where we create an array of elements brackets, symbols, numbers, or we can use regular expressions to reduce the equation. + +All students will remember BODMAS - Brackets, order, Division/Multiplication, Addition/Division. (or BIDMAS, PIDMAS, PEMDAS or whatever you used to remember it). So we have to break our prasing down into: + + * Brackets + * Order - we don't have this in our equations + * Division/Multiplication - we only have the latter + * Addition/Subtraction. + +So our logic becomes: + + * Find brackets without brackets within - for these we evaluate the contents using the same algorithm; + * If there are no brackets left - we then looking for multiplications from left to right and evaluate these. + * If there are no brackets or multiplications we look at the addition/subtraction again left to right. + * This gives the following perl function: + ```perl sub evaluate { my $str = shift; @@ -28,12 +44,20 @@ sub evaluate { } ``` +For small strings - this is about the same speed as `eval`, for larger strings not so, but in both cases it is "safer" as string `eval` of "tainted" input is a real security risk. + # Challenge 2 - Stealthy Number ***You are given a positive number, `$n`. Write a script to find out if the given number is Stealthy Number. A positive integer `N` is stealthy, if there exist positive integers `a`, `b`, `c`, `d` such that `a * b = c * d = N` and `a + b = c + d + 1`.*** ## The solution +First we find all the factors of `N` (well the ones for where `N=a*b` and `a Date: Mon, 13 Dec 2021 19:34:06 +0100 Subject: Add solution for challenge 143 part 1 --- challenge-143/alexander-pankoff/perl/ch-1.pl | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 challenge-143/alexander-pankoff/perl/ch-1.pl diff --git a/challenge-143/alexander-pankoff/perl/ch-1.pl b/challenge-143/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..5e18a29391 --- /dev/null +++ b/challenge-143/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my $expr = $ARGV[0]; +die "invalid expr\n" unless $expr && $expr =~ m/^[\s\d\.\+\-\*\*\(\)]*$/; +print eval($expr) . "\n"; -- cgit From 50e1d3c36fa027ad2aa3ef5f1b3d07c878778d0d Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Mon, 13 Dec 2021 19:34:16 +0100 Subject: Add solution for challenge 143 part 2 --- challenge-143/alexander-pankoff/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 challenge-143/alexander-pankoff/perl/ch-2.pl diff --git a/challenge-143/alexander-pankoff/perl/ch-2.pl b/challenge-143/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..8eba88cc93 --- /dev/null +++ b/challenge-143/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say signatures'; +no warnings qw'experimental::signatures'; + +use List::Util qw'first'; + +use constant DEBUG => $ENV{DEBUG} // 0; + +run() unless caller(); + +sub run() { + my $stealthy = stealthy( $ARGV[0] ); + say $stealthy ? 1 : 0; + explain($stealthy) if DEBUG; +} + +sub stealthy($n) { + my @divisors = find_divisors_pairs($n); + my @pairs = pairs(@divisors); + + my $stealthy = first { + my ( $a, $b, $c, $d ) = flatten($_); + $a * $b == $c * $d && $a + $b == $c + $d + 1; + } + @pairs; + + return $stealthy; +} + +sub explain($stealthy) { + say "Not stealthy" && return if !$stealthy; + my ( $a, $b, $c, $d ) = flatten($stealthy); + say +"Since $a (a) * $b (b) = $c (c) * $d (d) and $a (a) + $b (b) = $c (c) + $d (d) + 1"; +} + +sub pairs(@xs ) { + my @out; + for my $i ( 0 .. $#xs - 1 ) { + push @out, map { [ $xs[$i], $xs[$_] ] } ( $i + 1 .. $#xs ); + } + return @out; +} + +sub flatten($xs) { + map { @$_ } @$xs; +} + +sub find_divisors_pairs($x) { + my @out; + my $max = sqrt($x); + for my $i ( 1 .. $max ) { + if ( $x % $i == 0 ) { + push @out, [ $i, $x / $i ]; + } + } + + return @out; +} -- cgit From 38321bcb275b6e3bb9e55c55e4ff7122438d16b6 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 14 Dec 2021 00:01:29 +0100 Subject: Add solutions to 143: Calculator & Stealthy Number by E. Choroba --- challenge-143/e-choroba/perl/ch-1.pl | 62 ++++++++++++++++++++++++++++++++++++ challenge-143/e-choroba/perl/ch-2.pl | 24 ++++++++++++++ 2 files changed, 86 insertions(+) create mode 100755 challenge-143/e-choroba/perl/ch-1.pl create mode 100755 challenge-143/e-choroba/perl/ch-2.pl diff --git a/challenge-143/e-choroba/perl/ch-1.pl b/challenge-143/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..b1e25bec50 --- /dev/null +++ b/challenge-143/e-choroba/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub calculator { + my ($expression) = @_; + while ($expression =~ m{[ ()]}) { + $expression =~ s/(-?\d+) (\*) (-?\d+)/$1 * $3/e; + $expression =~ s/(-?\d+) ([-+]) (-?\d+)/"$1 $2 $3"/ee; + $expression =~ s/\((-?\d+)\)/$1/g; + } + return $expression +} + +use Marpa::R2; +my $dsl = << '__DSL__'; + + lexeme default = latm => 1 + :default ::= action => ::first + + Expression ::= Number + | ('(') Expression (')') assoc => group + || Expression (ws asterisk ws) Expression action => multiply + || Expression (ws plus ws) Expression action => add + | Expression (ws minus ws) Expression action => subtract + Number ::= Negative | positive + Negative ::= minus positive action => neg + + ws ~ [\s]+ + asterisk ~ '*' + plus ~ '+' + positive ~ [\d]+ + minus ~ '-' + +__DSL__ + +sub add { $_[1] + $_[2] } +sub subtract { $_[1] - $_[2] } +sub multiply { $_[1] * $_[2] } +sub neg { -$_[2] } +sub second { $_[2] } + +my $grammar = 'Marpa::R2::Scanless::G'->new({source => \$dsl}); + +sub calculate { + my ($input) = @_; + return ${ $grammar->parse(\$input, 'main') } +} + +use Test2::V0; +plan 8; + +my %tests = ( + 'Example 1' => ['10 + 20 - 5', 25], + 'Example 2' => ['(10 + 20 - 5) * 2', 50], + Negative => ['(1 + 2) - (1 * 12)', -9], + Precedence => ['((-1 - -2 - -3 * -4 + -5))', -16]); + +for my $t (sort keys %tests) { + is calculator($tests{$t}[0]), $tests{$t}[1], "regex $t"; + is calculate($tests{$t}[0]), $tests{$t}[1], "marpa $t"; +} diff --git a/challenge-143/e-choroba/perl/ch-2.pl b/challenge-143/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..3feae62c6a --- /dev/null +++ b/challenge-143/e-choroba/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +sub stealthy_number { + my ($n) = @_; + my @divisors = (map $n / $_, grep 0 == $n % $_, 1 .. sqrt $n); + for my $A (@divisors) { + for my $C (@divisors) { + my $B = $n / $A; + my $D = $n / $C; + return 1 if $A + $B == $C + $D + 1; + } + } + return 0 +} + +use Test2::V0; +plan 3; + +is stealthy_number(36), 1, 'Example 1'; +is stealthy_number(12), 1, 'Example 2'; +is stealthy_number(6), 0, 'Example 3'; -- cgit From 035bdaeeef58a57a973a144bfe396f0c2c683fe2 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Tue, 14 Dec 2021 00:12:25 -0500 Subject: Done! --- challenge-143/dave-jacoby/blog.txt | 1 + challenge-143/dave-jacoby/perl/ch-1.pl | 62 ++++++++++++++++++++++++++++++++++ challenge-143/dave-jacoby/perl/ch-2.pl | 41 ++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 challenge-143/dave-jacoby/blog.txt create mode 100644 challenge-143/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-143/dave-jacoby/perl/ch-2.pl diff --git a/challenge-143/dave-jacoby/blog.txt b/challenge-143/dave-jacoby/blog.txt new file mode 100644 index 0000000000..c3e15f2677 --- /dev/null +++ b/challenge-143/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/12/14/ninja-numbers-hiding-in-trees-the-weekly-challenge-143.html diff --git a/challenge-143/dave-jacoby/perl/ch-1.pl b/challenge-143/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..0caded391c --- /dev/null +++ b/challenge-143/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @examples; +push @examples, '( 5 * 10 ) - ( 12 * 3 )'; +push @examples, '10 + 20 - 5'; +push @examples, '(10 + 20 - 5) * 2'; +push @examples, '( ( 10 * 20 ) - 5) * 2'; + +for my $i (@examples) { + my $o = calculator($i); + my $o2 = bc($i); + say <<"END"; + Input: \$i = $i + Output: $o + BC: $o2 +END +} + +sub calculator( $s) { + + # parens + while ( $s =~ /\([\s\d\+\-\*]+\)/mix ) { + $s =~ s/\(([\s\d\+\-\*]+\))/calculator( unbracket( $1 ))/e; + } + + # multiplication + + while ( $s =~ / \d+ \s* \* \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \* \s* (\d+) )/ $2 * $3 /emx; + } + + # addition + + while ( $s =~ / \d+ \s* \+ \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \+ \s* (\d+) )/ $2 + $3 /emx; + } + + # subtraction + while ( $s =~ / \d+ \s* \- \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \- \s* (\d+) )/ $2 - $3 /emx; + } + return $s; +} + +sub unbracket( $s ) { + $s =~ s/^\(//; + $s =~ s/\)$//; + return $s; +} + +# This is the easy way, using pre-existing code +sub bc( $s) { + my $cmd = qq{ echo '$s' | bc }; + my $x = qx{$cmd}; + chomp $x; + return $x; +} diff --git a/challenge-143/dave-jacoby/perl/ch-2.pl b/challenge-143/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c712e2014b --- /dev/null +++ b/challenge-143/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +my @examples = qw( 6 12 24 36 ); + +for my $i (@examples) { + my $o = stealthy_numbers($i); + say <<"END"; + Input: \$n = $i + Output: $o +END +} + +sub stealthy_numbers ( $n ) { + my @factors = get_factor_pairs($n); + for my $i ( 0 .. -1 + scalar @factors ) { + my ( $ix, $iy ) = $factors[$i]->@*; + for my $j ( $i + 1 .. -1 + scalar @factors ) { + my ( $jx, $jy ) = $factors[$j]->@*; + my $addi = $ix + $iy; + my $addj = $jx + $jy; + return 1 if abs( $addi - $addj ) == 1; + } + } + return 0; +} + +sub get_factor_pairs( $n ) { + my %hash; + for my $x ( map { int $_ } 1 .. $n ) { + next unless $n % $x == 0; + my $y = $n / $x; + my $xy = join ',', sort { $a <=> $b } $x, $y; + $hash{$xy} = 1; + } + return map { [ split /,/, $_ ] } sort keys %hash; +} -- cgit From ff1256a60687a9903b39c0887e3c64115748b90d Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Tue, 14 Dec 2021 10:48:48 +0100 Subject: Fix formatting --- challenge-143/alexander-pankoff/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-143/alexander-pankoff/perl/ch-2.pl b/challenge-143/alexander-pankoff/perl/ch-2.pl index 8eba88cc93..a952866cde 100755 --- a/challenge-143/alexander-pankoff/perl/ch-2.pl +++ b/challenge-143/alexander-pankoff/perl/ch-2.pl @@ -36,7 +36,7 @@ sub explain($stealthy) { "Since $a (a) * $b (b) = $c (c) * $d (d) and $a (a) + $b (b) = $c (c) + $d (d) + 1"; } -sub pairs(@xs ) { +sub pairs(@xs) { my @out; for my $i ( 0 .. $#xs - 1 ) { push @out, map { [ $xs[$i], $xs[$_] ] } ( $i + 1 .. $#xs ); -- cgit From 7172edadbb0b471cdc3570098fe3f9e3160699e6 Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Tue, 14 Dec 2021 10:49:21 +0100 Subject: Remove duplicate char from match group --- challenge-143/alexander-pankoff/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-143/alexander-pankoff/perl/ch-1.pl b/challenge-143/alexander-pankoff/perl/ch-1.pl index 5e18a29391..fcb9c8edbc 100755 --- a/challenge-143/alexander-pankoff/perl/ch-1.pl +++ b/challenge-143/alexander-pankoff/perl/ch-1.pl @@ -3,5 +3,5 @@ use strict; use warnings; my $expr = $ARGV[0]; -die "invalid expr\n" unless $expr && $expr =~ m/^[\s\d\.\+\-\*\*\(\)]*$/; +die "invalid expr\n" unless $expr && $expr =~ m/^[\s\d\.\+\-\*\(\)]*$/; print eval($expr) . "\n"; -- cgit From 5ef9dca2242f150dae255b6d8fc02d86657440fc Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 14 Dec 2021 11:45:41 +0000 Subject: - Added solution by Robert DiCicco. --- challenge-143/robert-dicicco/perl/ch-1.pl | 18 + stats/pwc-current.json | 134 ++--- stats/pwc-language-breakdown-summary.json | 50 +- stats/pwc-language-breakdown.json | 958 +++++++++++++++--------------- stats/pwc-leaders.json | 770 ++++++++++++------------ stats/pwc-summary-1-30.json | 34 +- stats/pwc-summary-121-150.json | 30 +- stats/pwc-summary-151-180.json | 38 +- stats/pwc-summary-181-210.json | 112 ++-- stats/pwc-summary-211-240.json | 96 +-- stats/pwc-summary-241-270.json | 34 +- stats/pwc-summary-31-60.json | 100 ++-- stats/pwc-summary-61-90.json | 96 +-- stats/pwc-summary-91-120.json | 30 +- stats/pwc-summary.json | 46 +- 15 files changed, 1282 insertions(+), 1264 deletions(-) create mode 100644 challenge-143/robert-dicicco/perl/ch-1.pl diff --git a/challenge-143/robert-dicicco/perl/ch-1.pl b/challenge-143/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..9358ff07f6 --- /dev/null +++ b/challenge-143/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!perl.exe + +use strict; +use warnings; + +### Author: Robert DiCicco +### Date: 13-DEC-2021 +### Challenge #143 Calculator + +use Math::Calc::Parser 'calc'; +use utf8; + +print("Enter a math expression to calculate : "); +my $problem = <>; +chomp($problem); + +my $result = calc $problem; +print("$problem = $result\n"); diff --git a/stats/pwc-current.json b/stats/pwc-current.json index f8cfdf1667..eef5a1791b 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,50 +1,98 @@ { - "legend" : { - "enabled" : 0 + "subtitle" : { + "text" : "[Champions: 5] Last updated at 2021-12-14 11:39:11 GMT" }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "series" : [ + { + "name" : "The Weekly Challenge - 143", + "colorByPoint" : 1, + "data" : [ + { + "name" : "Mohammad S Anwar", + "y" : 1, + "drilldown" : "Mohammad S Anwar" + }, + { + "drilldown" : "Olivier Delouya", + "name" : "Olivier Delouya", + "y" : 1 + }, + { + "y" : 2, + "name" : "Paulo Custodio", + "drilldown" : "Paulo Custodio" + }, + { + "drilldown" : "Robert DiCicco", + "name" : "Robert DiCicco", + "y" : 2 + }, + { + "drilldown" : "Roger Bell_West", + "y" : 4, + "name" : "Roger Bell_West" + } + ] + } + ], "drilldown" : { "series" : [ { + "id" : "Mohammad S Anwar", "data" : [ [ "Perl", 1 ] ], - "name" : "Mohammad S Anwar", - "id" : "Mohammad S Anwar" + "name" : "Mohammad S Anwar" }, { - "name" : "Olivier Delouya", "id" : "Olivier Delouya", "data" : [ [ "Perl", 1 ] - ] + ], + "name" : "Olivier Delouya" }, { - "id" : "Paulo Custodio", - "name" : "Paulo Custodio", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Paulo Custodio", + "id" : "Paulo Custodio" }, { + "name" : "Robert DiCicco", "data" : [ [ "Perl", - 1 + 2 ] ], - "id" : "Robert DiCicco", - "name" : "Robert DiCicco" + "id" : "Robert DiCicco" }, { + "id" : "Roger Bell_West", "data" : [ [ "Perl", @@ -55,73 +103,25 @@ 2 ] ], - "id" : "Roger Bell_West", "name" : "Roger Bell_West" } ] }, - "chart" : { - "type" : "column" - }, "tooltip" : { "followPointer" : 1, - "headerFormat" : "{series.name}
", - "pointFormat" : "{point.name}: {point.y:f}
" - }, - "series" : [ - { - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 143", - "data" : [ - { - "name" : "Mohammad S Anwar", - "y" : 1, - "drilldown" : "Mohammad S Anwar" - }, - { - "y" : 1, - "name" : "Olivier Delouya", - "drilldown" : "Olivier Delouya" - }, - { - "drilldown" : "Paulo Custodio", - "y" : 2, - "name" : "Paulo Custodio" - }, - { - "drilldown" : "Robert DiCicco", - "y" : 1, - "name" : "Robert DiCicco" - }, - { - "y" : 4, - "name" : "Roger Bell_West", - "drilldown" : "Roger Bell_West" - } - ] - } - ], - "plotOptions" : { - "series" : { - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - }, - "borderWidth" : 0 - } + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
" }, "title" : { "text" : "The Weekly Challenge - 143" }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" }, "xAxis" : { "type" : "category" - }, - "subtitle" : { - "text" : "[Champions: 5] Last updated at 2021-12-13 18:05:31 GMT" } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index a183547112..d3f14dfe8a 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,30 +1,31 @@ { - "xAxis" : { - "type" : "category", - "labels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - } - } - }, - "subtitle" : { - "text" : "Last updated at 2021-12-13 18:05:31 GMT" - }, "yAxis" : { "title" : { "text" : null }, "min" : 0 }, - "title" : { - "text" : "The Weekly Challenge Contributions [2019 - 2021]" + "subtitle" : { + "text" : "Last updated at 2021-12-14 11:39:11 GMT" }, "legend" : { "enabled" : "false" }, + "xAxis" : { + "labels" : { + "style" : { + "fontSize" : "13px", + "fontFamily" : "Verdana, sans-serif" + } + }, + "type" : "category" + }, + "chart" : { + "type" : "column" + }, "series" : [ { + "name" : "Contributions", "data" : [ [ "Blog", @@ -32,7 +33,7 @@ ], [ "Perl", - 6859 + 6860 ], [ "Raku", @@ -40,24 +41,23 @@ ] ], "dataLabels" : { + "rotation" : -90, "y" : 10, - "color" : "#FFFFFF", "enabled" : "true", + "align" : "right", + "color" : "#FFFFFF", "format" : "{point.y:.0f}", "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" - }, - "align" : "right", - "rotation" : -90 - }, - "name" : "Contributions" + "fontSize" : "13px", + "fontFamily" : "Verdana, sans-serif" + } + } } ], "tooltip" : { "pointFormat" : "{point.y:.0f}" }, - "chart" : { - "type" : "column" + "title" : { + "text" : "The Weekly Challenge Contributions [2019 - 2021]" } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index 2d5595c2be..28bfbb1e9f 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,4 +1,13 @@ { + "legend" : { + "enabled" : "false" + }, + "xAxis" : { + "type" : "category" + }, + "chart" : { + "type" : "column" + }, "drilldown" : { "series" : [ { @@ -20,6 +29,7 @@ "id" : "001" }, { + "id" : "002", "data" : [ [ "Perl", @@ -34,7 +44,6 @@ 10 ] ], - "id" : "002", "name" : "002" }, { @@ -52,10 +61,11 @@ 9 ] ], - "id" : "003", - "name" : "003" + "name" : "003", + "id" : "003" }, { + "id" : "004", "data" : [ [ "Perl", @@ -70,7 +80,6 @@ 10 ] ], - "id" : "004", "name" : "004" }, { @@ -88,12 +97,10 @@ 12 ] ], -