From db43188976684322245b7fd17aea7c0970cc77dd Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Mon, 21 Sep 2020 02:12:17 -0400 Subject: Challenge 78 by Jaldhar H. Vyas --- challenge-078/jaldhar-h-vyas/blog.txt | 1 + challenge-078/jaldhar-h-vyas/perl/ch-1.pl | 19 ++++++++++++++++ challenge-078/jaldhar-h-vyas/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++++ challenge-078/jaldhar-h-vyas/raku/ch-1.sh | 1 + challenge-078/jaldhar-h-vyas/raku/ch-2.p6 | 12 ++++++++++ 5 files changed, 71 insertions(+) create mode 100644 challenge-078/jaldhar-h-vyas/blog.txt create mode 100755 challenge-078/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-078/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-078/jaldhar-h-vyas/raku/ch-1.sh create mode 100755 challenge-078/jaldhar-h-vyas/raku/ch-2.p6 diff --git a/challenge-078/jaldhar-h-vyas/blog.txt b/challenge-078/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..603d51f44e --- /dev/null +++ b/challenge-078/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2020/09/perl_weekly_challenge_week_78.html diff --git a/challenge-078/jaldhar-h-vyas/perl/ch-1.pl b/challenge-078/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..ddeff64952 --- /dev/null +++ b/challenge-078/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +sub isLeader { + my ($n, @arr) = @_; + + for my $elem (@arr) { + if ($n <= $elem) { + return undef; + } + } + + return 1; +} + +my $l = scalar @ARGV - 1; +say join q{, }, + @ARGV[ grep { isLeader($ARGV[$_], @ARGV[($_ + 1) .. $l]) } 0 .. $l ]; \ No newline at end of file diff --git a/challenge-078/jaldhar-h-vyas/perl/ch-2.pl b/challenge-078/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..6868903206 --- /dev/null +++ b/challenge-078/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; +use Getopt::Std; + +sub usage { + + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME [-A=<" ... ">] [-B=<" ... ">] + + -A=<" ... "> A string consisting of integers separated by spaces. + -B=<" ... "> A string consisting of integers separated by spaces. +-USAGE- + + exit 0; +} + +sub rotate { + my ($n, $arr) = @_; + my @arr = @{$arr}; + + for (1 .. $n) { + push @arr, shift @arr; + } + + return @arr; +} + +our ($opt_A, $opt_B); + +getopts('A:B:'); + +my @A = split /\s+/, $opt_A // usage(); +my @B = split /\s+/, $opt_B // usage(); + +map { say q{[}, (join q{ }, rotate($_, \@A)), q{]}; } @B; diff --git a/challenge-078/jaldhar-h-vyas/raku/ch-1.sh b/challenge-078/jaldhar-h-vyas/raku/ch-1.sh new file mode 100755 index 0000000000..e752ce70e6 --- /dev/null +++ b/challenge-078/jaldhar-h-vyas/raku/ch-1.sh @@ -0,0 +1 @@ +perl6 -e '@*ARGS[(0 ..^ @*ARGS.elems).grep({ @*ARGS[$_] > all @*ARGS[$_ + 1, ]:v; })].join(q{, }).say;' $@ \ No newline at end of file diff --git a/challenge-078/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-078/jaldhar-h-vyas/raku/ch-2.p6 new file mode 100755 index 0000000000..8f052d0aac --- /dev/null +++ b/challenge-078/jaldhar-h-vyas/raku/ch-2.p6 @@ -0,0 +1,12 @@ +#!/usr/bin/perl6 + +sub MAIN( + :$A where {$_.defined}, #= A string consisting of integers separated by spaces. + :$B where {$_.defined} #= A string consisting of integers separated by spaces. +) { + my @A = $A.split(/\s+/); + my @B = $B.split(/\s+/); + + + @B.map({ @A.rotate($_).say; }); +} \ No newline at end of file -- cgit From c7c05e77d7d8a112b8338eb8b1fb58b81e0f20e3 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 21 Sep 2020 01:24:40 -0600 Subject: Challenge 79 Solutions (Raku) --- challenge-079/mark-anderson/raku/ch-1.raku | 3 +++ challenge-079/mark-anderson/raku/ch-2.raku | 37 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 challenge-079/mark-anderson/raku/ch-1.raku create mode 100644 challenge-079/mark-anderson/raku/ch-2.raku diff --git a/challenge-079/mark-anderson/raku/ch-1.raku b/challenge-079/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..99e060c88f --- /dev/null +++ b/challenge-079/mark-anderson/raku/ch-1.raku @@ -0,0 +1,3 @@ +unit sub MAIN($N where * > 0); + +say (1..$N).map(*.base(2).indices(1)).sum mod 1000000007; diff --git a/challenge-079/mark-anderson/raku/ch-2.raku b/challenge-079/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..d3ee9a7606 --- /dev/null +++ b/challenge-079/mark-anderson/raku/ch-2.raku @@ -0,0 +1,37 @@ +=begin usage + +Usage: raku ch-2.raku 9 3 1 1 4 2 3 2 5 1 5 8 5 5 4 3 1 7 8 9 9 1 9 5 2 2 6 5 + +Output: + +9 # # # # +8 # # # # # # +7 # # # # # # # +6 # # # # # # # # +5 # # # # # # # # # # # # # # +4 # # # # # # # # # # # # # # # # +3 # # # # # # # # # # # # # # # # # # # +2 # # # # # # # # # # # # # # # # # # # # # # # +1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +111 + +=end usage + +#| MAIN only takes arguments from 1 to 9 +unit sub MAIN(*@ints where (.elems > 0 and .all ~~ 1..9)); + +my $pos = 0; +my $sum = 0; + +for @ints.max...1 -> $n { + say "$n @ints.map({ $_ >= $n ?? "#" !! " " })"; +} + +while @ints.join ~~ m:c($pos)/ (\d)(\d+)(\d) $1.comb.all < $2 }> / { + $sum += (($0, $2).min <<->> $1.comb).sum; + + $pos = $/.to - 1; +} + +say "\n", $sum; -- cgit From 035364d2ca9ca93bc276349c47fbd1fc4fb1e683 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 09:21:22 +0100 Subject: Challenge 1 --- challenge-079/simon-proctor/raku/ch-1.raku | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-079/simon-proctor/raku/ch-1.raku diff --git a/challenge-079/simon-proctor/raku/ch-1.raku b/challenge-079/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..4555876047 --- /dev/null +++ b/challenge-079/simon-proctor/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +use v6; + +#| Count the total numbrer of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007 +sub MAIN ( + UInt $N #= Max number to count up to +) { + say ( [+] (1..$N).race.map( *.base(2).comb ).flat ) % 1000000007 +} -- cgit From 646831c810b88ebca1ae9100232da81ad1e3fe2c Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 11:30:33 +0200 Subject: initial --- challenge-079/markus-holzer/raku/ch-1.raku | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 challenge-079/markus-holzer/raku/ch-1.raku diff --git a/challenge-079/markus-holzer/raku/ch-1.raku b/challenge-079/markus-holzer/raku/ch-1.raku new file mode 100644 index 0000000000..8bf2ac453b --- /dev/null +++ b/challenge-079/markus-holzer/raku/ch-1.raku @@ -0,0 +1,8 @@ +use experimental :cached; + +unit sub MAIN( Int $N ); + +sub bits( $n ) is cached { + $n !%% 2 + bits( $n div 2 ) if $n > 0 || 0 } + +say ($N...1).map( &bits ).sum % 1000000007; \ No newline at end of file -- cgit From 6c3470c8dab91bd9c3bfdc64c509e701ab798eb1 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 11:15:23 +0100 Subject: Challenge 2 done --- challenge-079/simon-proctor/raku/ch-2.raku | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 challenge-079/simon-proctor/raku/ch-2.raku diff --git a/challenge-079/simon-proctor/raku/ch-2.raku b/challenge-079/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..9824854e3a --- /dev/null +++ b/challenge-079/simon-proctor/raku/ch-2.raku @@ -0,0 +1,63 @@ +#!/usr/bin/env raku + +use v6; + +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| Given a list of height find the area of the largest single rectangle +sub MAIN ( + *@heights where { $_.all ~~ UInt && $_.elems >= 1 } , #= List of height +) { + my @rain-levels = calculate-rain-levels( @heights ); + draw-heights( @heights, @rain-levels ); + say [+] @rain-levels.map(*.area); +} + +class RainArea { + has Range $.range; + has UInt $.height; + has UInt $.area; +} + +sub draw-heights( @heights, @rain ) { + my $max = @heights.max; + my $width = $max.codes; + my $bar = '#' x $width; + my $spc = ' ' x $width; + my $dsh = '-' x $width; + my $wtr = '~' x $width; + my &spr = -> $x { sprintf( "%{$width}d", $x ) } + my &prt = -> $h { + -> $k, $v { + my @found = @rain.grep( -> $r { $k ~~ $r.range } ); + if (@found && $h <= @found[0].height) { + $v >= $h ?? $bar !! $wtr; + } else { + $v >= $h ?? $bar !! $spc; + } + }; + }; + + for $max...1 -> $h { + my &fnc = &prt($h); + ( &spr( $h ), |@heights.kv.map( &fnc ) ).join(" ").say; + } + ( $dsh xx @heights.elems + 1 ).join(" ").say; + ( $spc, |@heights.map( &spr ) ).join(" ").say; +} + +multi sub calculate-rain-levels( @heights where { any(@heights[0^..^*-1]) > any(@heights[0],@heights[*-1]) }, $offset = 0) { + my $max = max(@heights[0^..^*-1]); + my $mid-idx = @heights[0^..^*-1].kv.map( -> $k, $v { $v == $max ?? $k+1 !! Empty } )[0]; + + return ( |calculate-rain-levels( @heights[0..$mid-idx],$offset ), |calculate-rain-levels( @heights[$mid-idx..*],$mid-idx+$offset ) ) +} + +multi sub calculate-rain-levels( @heights where { @heights.elems > 2 }, $offset=0 ) { + my $height = min( @heights[0], @heights[*-1] ); + my $area = $height * ( @heights.elems - 2 ); + $area -= [+] @heights[0^..^*-1]; + return RainArea.new( range => ( $offset..^($offset+@heights.elems) ), :$area, :$height ); +} + +multi sub calculate-rain-levels(@,$) {} -- cgit From cd4e516ab22103d20e3c4002129a71b7fc9c0dd0 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 11:44:59 +0100 Subject: Small fix --- challenge-079/simon-proctor/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-079/simon-proctor/raku/ch-2.raku b/challenge-079/simon-proctor/raku/ch-2.raku index 9824854e3a..dc65ae171b 100644 --- a/challenge-079/simon-proctor/raku/ch-2.raku +++ b/challenge-079/simon-proctor/raku/ch-2.raku @@ -60,4 +60,4 @@ multi sub calculate-rain-levels( @heights where { @heights.elems > 2 }, $offset= return RainArea.new( range => ( $offset..^($offset+@heights.elems) ), :$area, :$height ); } -multi sub calculate-rain-levels(@,$) {} +multi sub calculate-rain-levels(@,$) { return Empty} -- cgit From 252c4ad7b974d83bc403aa0a0d7437a7847f665f Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Mon, 21 Sep 2020 12:28:57 +0100 Subject: Solutions for challenge 79 --- challenge-079/dave-cross/perl/ch-1.pl | 24 ++++++++++++++++ challenge-079/dave-cross/perl/ch-2.pl | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-079/dave-cross/perl/ch-1.pl create mode 100644 challenge-079/dave-cross/perl/ch-2.pl diff --git a/challenge-079/dave-cross/perl/ch-1.pl b/challenge-079/dave-cross/perl/ch-1.pl new file mode 100644 index 0000000000..c137165bb2 --- /dev/null +++ b/challenge-079/dave-cross/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw[say signatures]; +no warnings 'experimental::signatures'; + +if (!@ARGV or !$ARGV[0] or $ARGV[0] =~ /\D/) { + die "Please give me a positive integer\n"; +} + +my $n = shift; + +my $count; + +for (1 .. $n) { + $count += set_bits_in($_); +} + +say "$count % 1000000007 = $count"; + +sub set_bits_in($decimal) { + sprintf('%b', $decimal) =~ tr/1/1/; +} diff --git a/challenge-079/dave-cross/perl/ch-2.pl b/challenge-079/dave-cross/perl/ch-2.pl new file mode 100644 index 0000000000..dc1fb23122 --- /dev/null +++ b/challenge-079/dave-cross/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw[say signatures]; +no warnings 'experimental::signatures'; + +use List::Util qw[min max]; + +if (!@ARGV or grep { ! $_ or /\D/ } @ARGV) { + die "Give me a list of positive integers"; +} + +say plot_hist(@ARGV); + +say count_water(@ARGV); + +sub plot_hist(@data) { + my $max = max @data; + my $hist = "\n"; + + for (1 .. $max) { + my $level = $max - $_ + 1; + $hist .= $level; + $hist .= +($_ >= $level) ? ' #' : ' ' for @data; + $hist .= "\n"; + } + + $hist .= join ' ', ('-') x (@data + 1); + $hist .= "\n"; + $hist .= join ' ', (' ', @data); + $hist .= "\n"; + + return $hist; +} + +sub count_water(@data) { + my $water; + + for (1 .. $#data - 1) { + $water += get_dip_size($_, @data); + } + + return $water; +} + +sub get_dip_size($idx, @data) { + my $l_max = max(@data[0 .. $idx]) - $data[$idx]; + my $r_max = max(@data[$idx .. $#data]) - $data[$idx]; + + return min($l_max, $r_max); +} -- cgit From 38e0c835523f6017293d39e6ab32ea4c296d0463 Mon Sep 17 00:00:00 2001 From: vinodk89 Date: Mon, 21 Sep 2020 18:27:57 +0530 Subject: Solutions for challenge 79 --- challenge-079/vinod-k/ch-1.pl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 challenge-079/vinod-k/ch-1.pl diff --git a/challenge-079/vinod-k/ch-1.pl b/challenge-079/vinod-k/ch-1.pl new file mode 100644 index 0000000000..5e844a17dc --- /dev/null +++ b/challenge-079/vinod-k/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $N = $ARGV[0] || 4; +my $bit_count; + +foreach my $i( 1..$N ){ + + $bit_count += getSetBit($i); +} + +print "For $N Set Bits are: $bit_count\n"; +print "`$bit_count` as `$bit_count % 1000000007` = ".$bit_count % 1000000007; +print "\n"; + +sub getSetBit { + my $n = shift; + + my $bin = sprintf ("%b", $n); + + my @bits = split(//, $bin); + + my $count = 0; + foreach my $each_n( @bits ){ + $count++ if( $each_n == 1); + } + return $count; +} + -- cgit From 156b11c445341e9d028777bb41ec506f69fec3a4 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 15:31:34 +0200 Subject: way too long --- challenge-079/markus-holzer/raku/ch-2.raku | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-079/markus-holzer/raku/ch-2.raku diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku new file mode 100644 index 0000000000..d64fc1a83a --- /dev/null +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -0,0 +1,10 @@ +unit sub MAIN( *@N where @N.all ~~ Int ); + +say (@N.max...0) + .map( -> $height { + @N.pairs.grep( *.value >= $height ).map: *.key }) + .map( -> $indexes { + $indexes.rotor(2 => -1) }) + .map( -> $index-pairs { + $index-pairs.map({ .[1] - .[0] - 1 if .elems }).sum }) + .sum; \ No newline at end of file -- cgit From da89e65b16be36928445f92d5443dfd6801c76f2 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 15:46:47 +0200 Subject: somewhat clearer --- challenge-079/markus-holzer/raku/ch-2.raku | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku index d64fc1a83a..864734a511 100644 --- a/challenge-079/markus-holzer/raku/ch-2.raku +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -1,10 +1,10 @@ unit sub MAIN( *@N where @N.all ~~ Int ); +sub index-height { @N.pairs.grep( *.value >= $^height ).map: *.key } +sub index-diff { $^indexes.map({ .[1] - .[0] - 1 if .elems }).sum } + say (@N.max...0) - .map( -> $height { - @N.pairs.grep( *.value >= $height ).map: *.key }) - .map( -> $indexes { - $indexes.rotor(2 => -1) }) - .map( -> $index-pairs { - $index-pairs.map({ .[1] - .[0] - 1 if .elems }).sum }) - .sum; \ No newline at end of file + .map( &index-height ) + .map( *.rotor: 2 => -1 ) + .map( &index-diff ) + .sum; \ No newline at end of file -- cgit From bec3ea4022d39b97f0c7e3892cf0b46319746883 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 21 Sep 2020 15:06:16 +0100 Subject: Answers to challenge #79. --- challenge-079/roger-bell-west/perl/ch-1.pl | 24 ++++++++++++++ challenge-079/roger-bell-west/perl/ch-2.pl | 41 ++++++++++++++++++++++++ challenge-079/roger-bell-west/python/ch-1.py | 21 ++++++++++++ challenge-079/roger-bell-west/python/ch-2.py | 48 ++++++++++++++++++++++++++++ challenge-079/roger-bell-west/raku/ch-1.p6 | 22 +++++++++++++ challenge-079/roger-bell-west/raku/ch-2.p6 | 36 +++++++++++++++++++++ 6 files changed, 192 insertions(+) create mode 100755 challenge-079/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-079/roger-bell-west/perl/ch-2.pl create mode 100755 challenge-079/roger-bell-west/python/ch-1.py create mode 100755 challenge-079/roger-bell-west/python/ch-2.py create mode 100755 challenge-079/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-079/roger-bell-west/raku/ch-2.p6 diff --git a/challenge-079/roger-bell-west/perl/ch-1.pl b/challenge-079/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..e20c92f5e2 --- /dev/null +++ b/challenge-079/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Math::GMPz qw( :mpz ); + +use Test::More tests => 2; + +is(csb(4),5,'example 1'); +is(csb(3),4,'example 2'); + +sub csb { + my $tot=shift; + my $n=Math::GMPz->new(1); + my $bits=Math::GMPz->new(0); + my $m=Math::GMPz->new(1000000007); + while ($n <= $tot) { + Rmpz_add_ui($bits,$bits,Rmpz_popcount(Math::GMPz->new($n))); + Rmpz_mod($bits,$bits,$m); + Rmpz_add_ui($n,$n,1); + } + return Rmpz_get_str($bits,10); +} diff --git a/challenge-079/roger-bell-west/perl/ch-2.pl b/challenge-079/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..e5b9e02893 --- /dev/null +++ b/challenge-079/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,41 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use List::Util qw(min max); + +use Test::More tests => 2; + +is(capacity([2,1,4,1,2,5]),6,'example 1'); +is(capacity([3,1,3,1,1,5]),6,'example 2'); + +histo([2,1,4,1,2,5]); +histo([3,1,3,1,1,5]); + +sub capacity { + my @n=@{shift @_}; + my $cap=0; + foreach my $r (min(@n)..max(@n)) { + my @b=grep {$n[$_]>=$r} (0..$#n); + if (scalar @b > 1) { + foreach my $i (0..$#b-1) { + $cap += $b[$i+1]-$b[$i]-1; + } + } + } + return $cap; +} + +sub histo { + my @n=@{shift @_}; + my $mx=max(@n); + my $cw=int(log($mx+1)/log(10)+.9999); + for (my $r=$mx;$r>0;$r--) { + my @row=(sprintf('%'.$cw.'d',$r)); + push @row,map {($n[$_]>=$r?'#' x $cw:' ' x $cw)} (0..$#n); + print join(' ',@row),"\n"; + } + print join(' ',('-' x $cw) x (1+scalar @n)),"\n"; + print join(' ',map {sprintf('%'.$cw.'s',$_)} ('',@n)),"\n"; +} diff --git a/challenge-079/roger-bell-west/python/ch-1.py b/challenge-079/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c3c5aff095 --- /dev/null +++ b/challenge-079/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python + +import unittest + +def csb(tot): + bits=0 + m=1000000007; + for n in range(1,tot+1): + bits += bin(n).count("1") + bits %= m + return bits + +class TestCsb(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(csb(4),5,'example 1') + + def test_ex2(self): + self.assertEqual(csb(3),4,'example 2') + +unittest.main() diff --git a/challenge-079/roger-bell-west/python/ch-2.py b/challenge-079/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..04f30f729d --- /dev/null +++ b/challenge-079/roger-bell-west/python/ch-2.py @@ -0,0 +1,48 @@ +#! /usr/bin/python + +import unittest + +from math import log10 + +def capacity(n): + cap=0 + for r in range(min(n),max(n)+1): + b=[i for i in range(0,len(n)) if n[i] >= r] + if(len(b)>1): + for i in range(0,len(b)-1): + cap += b[i+1]-b[i]-1 + return cap + +def histo(n): + mx=max(n) + cw=int(log10(mx+1)+.9999); + for r in reversed(range(1,mx+1)): + row=list() + row.append(format(r,str(cw))) + for i in range(0,len(n)): + s = ' ' + if(n[i] >= r): + s = '#' + s *= cw + row.append(s) + print(' '.join(row)) + s='-' * cw; + print(' '.join([s for i in range(0,len(n)+1)])) + row=list() + row.append(' ' * cw) + for i in n: + row.append(format(i,str(cw))) + print(' '.join(row)) + +histo((2,1,4,1,2,5)); +histo((3,1,3,1,1,5)); + +class TestCapacity(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(capacity((2,1,4,1,2,5)),6,'example 1') + + def test_ex2(self): + self.assertEqual(capacity((3,1,3,1,1,5)),6,'example 2') + +unittest.main() diff --git a/challenge-079/roger-bell-west/raku/ch-1.p6 b/challenge-079/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..dcf9aad5d3 --- /dev/null +++ b/challenge-079/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(csb(4),5,'example 1'); +is(csb(3),4,'example 2'); + +sub csb($tot) { + my $bits=0; + my $m=1000000007; + for 1..$tot -> $n { + my $k=$n; + while ($k > 0) { + $bits += $k +& 1; + $k +>= 1; + } + $bits %= $m; + } + return $bits; +} diff --git a/challenge-079/roger-bell-west/raku/ch-2.p6 b/challenge-079/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..631e7caf91 --- /dev/null +++ b/challenge-079/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(capacity((2,1,4,1,2,5)),6,'example 1'); +is(capacity((3,1,3,1,1,5)),6,'example 2'); + +histo((2,1,4,1,2,5)); +histo((3,1,3,1,1,5)); + +sub capacity(@n) { + my $cap=0; + for (min(@n)..max(@n)) -> $r { + my @b=grep {@n[$_] >= $r}, (0..@n.end); + if (@b.elems > 1) { + for (0..@b.end-1) -> $i { + $cap += @b[$i+1]-@b[$i]-1; + } + } + } + return $cap; +} + +sub histo(@n) { + my $mx=max(@n); + my $cw=floor(log($mx+1)/log(10)+.9999); + loop (my $r=$mx;$r>0;$r--) { + my @row=(sprintf('%' ~ $cw ~ 'd',$r)); + push @row,map {(@n[$_]>=$r ?? '#' x $cw !! ' ' x $cw)}, (0..@n.end); + say join(' ',@row); + } + say join(' ',('-' x $cw) xx (1+@n.elems)); + say join(' ',map {sprintf('%' ~ $cw ~ 's',$_)}, ('',@n).flat); +} -- cgit From 4968031a8c17661761edfb6d94fef6bd3169722c Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 16:16:39 +0200 Subject: nicer --- challenge-079/markus-holzer/raku/ch-2.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku index 864734a511..532d2505a3 100644 --- a/challenge-079/markus-holzer/raku/ch-2.raku +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -1,10 +1,10 @@ unit sub MAIN( *@N where @N.all ~~ Int ); -sub index-height { @N.pairs.grep( *.value >= $^height ).map: *.key } -sub index-diff { $^indexes.map({ .[1] - .[0] - 1 if .elems }).sum } +sub index-find { @N.pairs.grep( *.value >= $^h ).map: *.key } +sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 } say (@N.max...0) - .map( &index-height ) - .map( *.rotor: 2 => -1 ) + .map( &index-find ) .map( &index-diff ) + .flat .sum; \ No newline at end of file -- cgit From 75843f94f259023800bf1605c1414cfefeab704f Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:51:26 +0200 Subject: Test program for Week 79. --- challenge-079/abigail/test.pl | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 challenge-079/abigail/test.pl diff --git a/challenge-079/abigail/test.pl b/challenge-079/abigail/test.pl new file mode 100755 index 0000000000..578f69cc61 --- /dev/null +++ b/challenge-079/abigail/test.pl @@ -0,0 +1,48 @@ +#!/opt/perl/bin/perl + +# +# Test the solutions. Either call it with the directory name you +# want to test in, or call it as "../test.pl" from within the directory. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +chdir ".." if -f "../test.pl"; + +use experimental 'signatures'; + +use Test::More; + +my %languages = ( + Perl => ["/opt/perl/bin/perl" => 'pl', 'perl'], + JavaScript => ["/usr/local/bin/node" => 'js', 'node'], +); + + +foreach my $challenge (1, 2) { + my @inputs = or next; + subtest "Challenge $challenge" => sub { + foreach my $language (sort keys %languages) { + my ($exe, $ext, $dir) = @{$languages {$language}}; + my $solution = "$dir/ch-$challenge.$ext"; + next unless -r $solution; + subtest $language => sub { + foreach my $input (@inputs) { + my $output_exp = ($input =~ s/input/output/r) . ".exp"; + my $exp = `cat $output_exp`; + my $got = `$exe ./$solution < $input`; + is $got, $exp, $input; + } + } + } + } +} + +done_testing; + + +__END__ -- cgit From dfdbb9d3c54c2f4da70d04186425a39f3193daa4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:51:44 +0200 Subject: Test files for Week 79, challenge 1 --- challenge-079/abigail/input-1-1 | 1 + challenge-079/abigail/input-1-2 | 1 + challenge-079/abigail/input-1-3 | 1 + challenge-079/abigail/output-1-1.exp | 1 + challenge-079/abigail/output-1-2.exp | 1 + challenge-079/abigail/output-1-3.exp | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-079/abigail/input-1-1 create mode 100644 challenge-079/abigail/input-1-2 create mode 100644 challenge-079/abigail/input-1-3 create mode 100644 challenge-079/abigail/output-1-1.exp create mode 100644 challenge-079/abigail/output-1-2.exp create mode 100644 challenge-079/abigail/output-1-3.exp diff --git a/challenge-079/abigail/input-1-1 b/challenge-079/abigail/input-1-1 new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/challenge-079/abigail/input-1-1 @@ -0,0 +1 @@ +4 diff --git a/challenge-079/abigail/input-1-2 b/challenge-079/abigail/input-1-2 new file mode 100644 index 0000000000..00750edc07 --- /dev/null +++ b/challenge-079/abigail/input-1-2 @@ -0,0 +1 @@ +3 diff --git a/challenge-079/abigail/input-1-3 b/challenge-079/abigail/input-1-3 new file mode 100644 index 0000000000..749fce669d --- /dev/null +++ b/challenge-079/abigail/input-1-3 @@ -0,0 +1 @@ +1000000 diff --git a/challenge-079/abigail/output-1-1.exp b/challenge-079/abigail/output-1-1.exp new file mode 100644 index 0000000000..7ed6ff82de --- /dev/null +++ b/challenge-079/abigail/output-1-1.exp @@ -0,0 +1 @@ +5 diff --git a/challenge-079/abigail/output-1-2.exp b/challenge-079/abigail/output-1-2.exp new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/challenge-079/abigail/output-1-2.exp @@ -0,0 +1 @@ +4 diff --git a/challenge-079/abigail/output-1-3.exp b/challenge-079/abigail/output-1-3.exp new file mode 100644 index 0000000000..b489242255 --- /dev/null +++ b/challenge-079/abigail/output-1-3.exp @@ -0,0 +1 @@ +9884999 -- cgit From 1da0d24d7d9480bb239b1a5584d1f14460627f44 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:52:07 +0200 Subject: Perl solution for Week 79. --- challenge-079/abigail/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 challenge-079/abigail/perl/ch-1.pl diff --git a/challenge-079/abigail/perl/ch-1.pl b/challenge-079/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..a2a8d27818 --- /dev/null +++ b/challenge-079/abigail/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +# +# Challenge: +# +# You are given a positive number $N. +# +# Write a script to count the total numbrer of set bits of the binary +# representations of all numbers from 1 to $N and return +# $total_count_set_bit % 1000000007. +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/ +# + +# +# This is A000778 (https://oeis.org/A000788). There's a recursive formala +# for the number of bits in the binary representation of 0 .. $N: +# +# bits (0) = 0 +# bits (2 * N) = bits (N) + bits (N - 1) + N +# bits (2 * N + 1) = 2 * bits (N) + N + 1 +# + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +use integer; + +my $BIG_NUM = 1_000_000_007; + +sub bits ($n) { + state $bits; + $$bits {$n} ||= + $n == 0 ? 0 + : do { + my $half = int ($n / 2); + bits ($half) + $half + ($n % 2 ? bits ($half) + 1 + : bits ($half - 1)) + } +} + +say bits (<>) % $BIG_NUM; + + +__END__ -- cgit From 172977c36c3012b79d1710cff34da2219a0f67a7 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:18:29 +0200 Subject: Ignore trailing whitespace. --- challenge-079/abigail/test.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-079/abigail/test.pl b/challenge-079/abigail/test.pl index 578f69cc61..222475a99b 100755 --- a/challenge-079/abigail/test.pl +++ b/challenge-079/abigail/test.pl @@ -35,6 +35,7 @@ foreach my $challenge (1, 2) { my $output_exp = ($input =~ s/input/output/r) . ".exp"; my $exp = `cat $output_exp`; my $got = `$exe ./$solution < $input`; + s/\h+$//gm for $exp, $got; is $got, $exp, $input; } } -- cgit From e922cd84165fccaf061431d8fdb6c7a36c56b0fb Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:42:59 +0200 Subject: Test input/output for Week 79, part 2. --- challenge-079/abigail/input-2-1 | 1 + challenge-079/abigail/input-2-2 | 1 + challenge-079/abigail/input-2-3 | 1 + challenge-079/abigail/output-2-1.exp | 7 +++++++ challenge-079/abigail/output-2-2.exp | 7 +++++++ challenge-079/abigail/output-2-3.exp | 13 +++++++++++++ 6 files changed, 30 insertions(+) create mode 100644 challenge-079/abigail/input-2-1 create mode 100644 challenge-079/abigail/input-2-2 create mode 100644 challenge-079/abigail/input-2-3 create mode 100644 challenge-079/abigail/output-2-1.exp create mode 100644 challenge-079/abigail/output-2-2.exp create mode 100644 challenge-079/abigail/output-2-3.exp diff --git a/challenge-079/abigail/input-2-1 b/challenge-079/abigail/input-2-1 new file mode 100644 index 0000000000..e7f18e258b --- /dev/null +++ b/challenge-079/abigail/input-2-1 @@ -0,0 +1 @@ +2 1 4 1 2 5 diff --git a/challenge-079/abigail/input-2-2 b/challenge-079/abigail/input-2-2 new file mode 100644 index 0000000000..bc96b17583 --- /dev/null +++ b/challenge-079/abigail/input-2-2 @@ -0,0 +1 @@ +3 1 3 1 1 5 diff --git a/challenge-079/abigail/input-2-3 b/challenge-079/abigail/input-2-3 new file mode 100644 index 0000000000..febba99da5 --- /dev/null +++ b/challenge-079/abigail/input-2-3 @@ -0,0 +1 @@ +3 10 3 1 11 5 diff --git a/challenge-079/abigail/output-2-1.exp b/challenge-079/abigail/output-2-1.exp new file mode 100644 index 0000000000..0ba8a01c66 --- /dev/null +++ b/challenge-079/abigail/output-2-1.exp @@ -0,0 +1,7 @@ +5 # +4 # # +3 # # +2 # # # # +1 # # # # # # +_ _ _ _ _ _ _ + 2 1 4 1 2 5 diff --git a/challenge-079/abigail/output-2-2.exp b/challenge-079/abigail/output-2-2.exp new file mode 100644 index 0000000000..a4f21fe319 --- /dev/null +++ b/challenge-079/abigail/output-2-2.exp @@ -0,0 +1,7 @@ +5 # +4 # +3 # # # +2 # # # +1 # # # # # # +_ _ _ _ _ _ _ + 3 1 3 1 1 5 diff --git a/challenge-079/abigail/output-2-3.exp b/challenge-079/abigail/output-2-3.exp new file mode 100644 index 0000000000..5dfd8fce2f --- /dev/null +++ b/challenge-079/abigail/output-2-3.exp @@ -0,0 +1,13 @@ +11 # +10 # # + 9 # # + 8 # # + 7 # # + 6 # # + 5 # # # + 4 # # # + 3 # # # # # + 2 # # # # # + 1 # # # # # # +__ __ __ __ __ __ __ + 3 10 3 1 11 5 -- cgit From 5b410e796c910a170f80bc40c4a91b0b6f1f2edf Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:43:32 +0200 Subject: Perl solution for Week 79, part 2. --- challenge-079/abigail/perl/ch-2.pl | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 challenge-079/abigail/perl/ch-2.pl diff --git a/challenge-079/abigail/perl/ch-2.pl b/challenge-079/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3fc6b9fd7e --- /dev/null +++ b/challenge-079/abigail/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/opt/perl/bin/perl + +use 5.032; + +# +# Challenge: +# +# You are given an array of positive numbers @N. +# Write a script to represent it as Histogram Chart and find out +# how much water it can trap. +# + +use strict; +use warnings; +no warnings 'syntax'; + +use List::Util qw [max]; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Read the input, extract the numbers, store them in @N, and find the maximum. +# +my $max = max my @N = <> =~ /[0-9]+/g; + +my $w = length $max; # Width of the largest number, so we + # we can align things properly. + +# +# Print histograms. We're counting down from the maximum volume to 1, +# and for each index, if the volume on given index equals, or exceeds +# the current volume, we print a #, else, we print a space. +# +for (my $volume = $max; $volume; $volume --) { + printf "%${w}d" => $volume; + printf " %${w}s" => $N [$_] >= $volume ? "#" : " " for keys @N; + print "\n"; +} + +# +# Print the line with bars. +# +my $bar = "_" x $w; +print $bar, " $bar" x @N, "\n"; + +# +# Print the sums (just the values in @N) +# +print " " x $w; +printf " %${w}d" => $_ for @N; +print "\n"; + + +__END__ -- cgit From fa07704e9e67658e76e89d0b00c9cfeb62d7aa42 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:56:57 +0200 Subject: Javascript solution for week 79, part 1 --- challenge-079/abigail/node/ch-1.js | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-079/abigail/node/ch-1.js diff --git a/challenge-079/abigail/node/ch-1.js b/challenge-079/abigail/node/ch-1.js new file mode 100644 index 0000000000..6c9ca09ace --- /dev/null +++ b/challenge-079/abigail/node/ch-1.js @@ -0,0 +1,53 @@ +// +// Challenge: +// +// You are given a positive number $N. +// +// Write a script to count the total numbrer of set bits of the binary +// representations of all numbers from 1 to $N and return +// $total_count_set_bit % 1000000007. +// +// https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/ +// + +// +// This is A000778 (https://oeis.org/A000788). There's a recursive formala +// for the number of bits in the binary representation of 0 .. $N: +// +// bits (0) = 0 +// bits (2 * N) = bits (N) + bits (N - 1) + N +// bits (2 * N + 1) = 2 * bits (N) + N + 1 +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + console . log (bits (+line)); // Unary + numifies +}); + + +// +// Recursive function to calculate the results (see above). +// +let cache = []; +function bits (n) { + if (n == 0) { + return 0; + } + + if (!cache [n]) { + let half = Math . floor (n / 2); + cache [n] = bits (half) + half + + (n % 2 ? bits (half) + 1 : bits (half - 1)); + } + return cache [n]; +} + -- cgit From 88acc407f4db3e21ac6701f49886728f2dc846b2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 19:30:15 +0200 Subject: Javascript solution for Week 079, part 2. --- challenge-079/abigail/node/ch-2.js | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 challenge-079/abigail/node/ch-2.js diff --git a/challenge-079/abigail/node/ch-2.js b/challenge-079/abigail/node/ch-2.js new file mode 100644 index 0000000000..a3de411e8c --- /dev/null +++ b/challenge-079/abigail/node/ch-2.js @@ -0,0 +1,77 @@ +// +// Challenge: +// +// You are given an array of positive numbers @N. +// Write a script to represent it as Histogram Chart and find out +// how much water it can trap. +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + print_histogram (line); +}); + + +// +// Node doesn't have (s)printf, so we're writing our own formatting function. +// +function ff (n, width, pad = " ") { + let s = n . toString (); + while (s . length < width) { + s = pad + s; + } + return s; +} + +function print_histogram (input) { + // + // Extract N from the input, find the maximum, + // and the width of this maximum. + // + let N = input . split (" ") . map (Number); + let max = Math . max (... N); + let w = max . toString () . length; + + // + // Count down volumes from the maximum, down to 1. + // For each volume, print the volume, and for each entry + // in N, print "#" if the volume is equal or less than + // the entry in N, else, print " ". + // + for (let volume = max; volume; volume --) { + let line = ff (volume, w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (volume <= N [i] ? "#" : " ", w); + } + console . log (line); + } + + // + // Print the bars + // + let line = ""; + line += ff ("_", w, "_"); + for (let i = 0; i < N . length; i ++) { + line += " " + ff ("_", w, "_"); + } + console . log (line); + + // + // Finally, print the totals + // + line = ""; + line += ff (" ", w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (N [i], w); + } + console . log (line); +} -- cgit From 10f8946866d030906f6bea294e1bf90084360279 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 20:00:28 +0200 Subject: improved and benchmarked --- challenge-079/markus-holzer/raku/bench.raku | 32 +++++++++++++++++++++++++++++ challenge-079/markus-holzer/raku/ch-1.raku | 9 ++++---- challenge-079/markus-holzer/raku/ch-2.raku | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 challenge-079/markus-holzer/raku/bench.raku diff --git a/challenge-079/markus-holzer/raku/bench.raku b/challenge-079/markus-holzer/raku/bench.raku new file mode 100644 index 0000000000..77ad224a3b --- /dev/null +++ b/challenge-079/markus-holzer/raku/bench.raku @@ -0,0 +1,32 @@ +use Bench; + +my $n = @*ARGS[0] || 42; + +#say i($n); +#say r($n); +#say f($n); +#say k($n); +#say l($n); + +sub i { + +$^n.base(2).indices(1) } + +sub r { + +($^n.base(2) ~~ m:g/1/) } + +sub f { + $^n == 0 ?? 0 !! $^n !%% 2 + f( $^n div 2 ) } + +sub k($n is copy) { + my $c = 0; while $n != 0 { $n = $n +& ($n-1); $c++ }; $c } + +sub l { + state @b = 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4; + $^n == 0 ?? 0 !! @b[ $^n +& 0xf ] + l($^n +> 4) } + +Bench.new.timethese( 100000, { + base2-with-indices => { i(42) }, + base2-with-regex => { r(42) }, + div2-recursive => { f(42) }, + kernighan => { k(42) }, + lookup-recursive => { l(42) }}) \ No newline at end of file diff --git a/challenge-079/markus-holzer/raku/ch-1.raku b/challenge-079/markus-holzer/raku/ch-1.raku index 8bf2ac453b..592d814445 100644 --- a/challenge-079/markus-holzer/raku/ch-1.raku +++ b/challenge-079/markus-holzer/raku/ch-1.raku @@ -1,8 +1,7 @@ -use experimental :cached; - unit sub MAIN( Int $N ); -sub bits( $n ) is cached { - $n !%% 2 + bits( $n div 2 ) if $n > 0 || 0 } +# This is not only the simplest, but also a quite fast solution +# It only loses (sometimes) to the kernighan algorithm +# See bench.raku in this directory -say ($N...1).map( &bits ).sum % 1000000007; \ No newline at end of file +say ($N...1).map( + *.base(2).indices(1) ).sum % 1000000007 \ No newline at end of file diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku index 532d2505a3..d674dca8af 100644 --- a/challenge-079/markus-holzer/raku/ch-2.raku +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -1,7 +1,7 @@ unit sub MAIN( *@N where @N.all ~~ Int ); sub index-find { @N.pairs.grep( *.value >= $^h ).map: *.key } -sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 } +sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 } say (@N.max...0) .map( &index-find ) -- cgit From 0eaf9a519d188731cefdb41e66be775ed2392633 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 20:04:52 +0200 Subject: actually look at the argument --- challenge-079/markus-holzer/raku/bench.raku | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-079/markus-holzer/raku/bench.raku b/challenge-079/markus-holzer/raku/bench.raku index 77ad224a3b..f435d3a028 100644 --- a/challenge-079/markus-holzer/raku/bench.raku +++ b/challenge-079/markus-holzer/raku/bench.raku @@ -1,6 +1,6 @@ use Bench; -my $n = @*ARGS[0] || 42; +my $N = @*ARGS[0] || 42; #say i($n); #say r($n); @@ -25,8 +25,8 @@ sub l { $^n == 0 ?? 0 !! @b[ $^n +& 0xf ] + l($^n +> 4) } Bench.new.timethese( 100000, { - base2-with-indices => { i(42) }, - base2-with-regex => { r(42) }, - div2-recursive => { f(42) }, - kernighan => { k(42) }, - lookup-recursive => { l(42) }}) \ No newline at end of file + base2-with-indices => { i($N) }, + base2-with-regex => { r($N) }, + div2-recursive => { f($N) }, + kernighan => { k($N) }, + lookup-recursive => { l($N) }}) \ No newline at end of file -- cgit From d6d15ced633043d544d0deed31eaba48fbc61a59 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 21 Sep 2020 20:05:29 +0200 Subject: actually look at the argument --- challenge-079/markus-holzer/raku/bench.raku | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-079/markus-holzer/raku/bench.raku b/challenge-079/markus-holzer/raku/bench.raku index f435d3a028..972836ee07 100644 --- a/challenge-079/markus-holzer/raku/bench.raku +++ b/challenge-079/markus-holzer/raku/bench.raku @@ -2,11 +2,11 @@ use Bench; my $N = @*ARGS[0] || 42; -#say i($n); -#say r($n); -#say f($n); -#say k($n); -#say l($n); +#say i($N); +#say r($N); +#say f($N); +#say k($N); +#say l($N); sub i { +$^n.base(2).indices(1) } -- cgit From dd7bef349a1197e46bdf1c12a44bcea3107485fd Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 21 Sep 2020 12:50:48 -0600 Subject: Challenge 79 Solutions (Raku) --- challenge-079/mark-anderson/raku/ch-1.raku | 2 +- challenge-079/mark-anderson/raku/ch-2.raku | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/challenge-079/mark-anderson/raku/ch-1.raku b/challenge-079/mark-anderson/raku/ch-1.raku index 99e060c88f..0c6bceb3b3 100644 --- a/challenge-079/mark-anderson/raku/ch-1.raku +++ b/challenge-079/mark-anderson/raku/ch-1.raku @@ -1,3 +1,3 @@ unit sub MAIN($N where * > 0); -say (1..$N).map(*.base(2).indices(1)).sum mod 1000000007; +say (1..$N).map(*.base(2)).comb.sum mod 1000000007; diff --git a/challenge-079/mark-anderson/raku/ch-2.raku b/challenge-079/mark-anderson/raku/ch-2.raku index d3ee9a7606..5cdb57761c 100644 --- a/challenge-079/mark-anderson/raku/ch-2.raku +++ b/challenge-079/mark-anderson/raku/ch-2.raku @@ -13,6 +13,8 @@ Output: 3 # # # # # # # # # # # # # # # # # # # 2 # # # # # # # # # # # # # # # # # # # # # # # 1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + 9 3 1 1 4 2 3 2 5 1 5 8 5 5 4 3 1 7 8 9 9 1 9 5 2 2 6 5 111 @@ -28,6 +30,8 @@ for @ints.max...1 -> $n { say "$n @ints.map({ $_ >= $n ?? "#" !! " " })"; } +say " ", " -" x @ints, "\n ", @ints.join(" "); + while @ints.join ~~ m:c($pos)/ (\d)(\d+)(\d) $1.comb.all < $2 }> / { $sum += (($0, $2).min <<->> $1.comb).sum; -- cgit From 778b4ee84076081c2df48d50dd133d80726e8472 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 21 Sep 2020 12:57:18 -0600 Subject: Challenge 79 Solutions (Raku) --- challenge-079/mark-anderson/raku/ch-2.raku | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-079/mark-anderson/raku/ch-2.raku b/challenge-079/mark-anderson/raku/ch-2.raku index 5cdb57761c..328f3ed5bf 100644 --- a/challenge-079/mark-anderson/raku/ch-2.raku +++ b/challenge-079/mark-anderson/raku/ch-2.raku @@ -34,7 +34,6 @@ say " ", " -" x @ints, "\n ", @ints.join(" "); while @ints.join ~~ m:c($pos)/ (\d)(\d+)(\d) $1.comb.all < $2 }> / { $sum += (($0, $2).min <<->> $1.comb).sum; - $pos = $/.to - 1; } -- cgit From 7445a3719578b7fd43eb82cc45b6ce45264c6d3d Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 21 Sep 2020 13:15:56 -0600 Subject: Challenge 79 Solutions (Raku) --- challenge-079/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-079/mark-anderson/raku/ch-1.raku b/challenge-079/mark-anderson/raku/ch-1.raku index 0c6bceb3b3..9d690421e5 100644 --- a/challenge-079/mark-anderson/raku/ch-1.raku +++ b/challenge-079/mark-anderson/raku/ch-1.raku @@ -1,3 +1,3 @@ unit sub MAIN($N where * > 0); -say (1..$N).map(*.base(2)).comb.sum mod 1000000007; +say (1..$N).map(*.base(2)).comb(/1/).elems mod 1000000007; -- cgit From efe627bc1ff8398b695786dc8871aacedf270f68 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 21 Sep 2020 21:20:11 +0100 Subject: - Added solutions by Jaldhar H. Vyas. --- stats/pwc-current.json | 629 ++++++++++++++++-------------- stats/pwc-language-breakdown-summary.json | 62 +-- stats/pwc-language-breakdown.json | 528 ++++++++++++------------- stats/pwc-leaders.json | 340 ++++++++-------- stats/pwc-summary-1-30.json | 36 +- stats/pwc-summary-121-150.json | 34 +- stats/pwc-summary-151-180.json | 122 +++--- stats/pwc-summary-181-210.json | 40 +- stats/pwc-summary-31-60.json | 30 +- stats/pwc-summary-61-90.json | 100 ++--- stats/pwc-summary-91-120.json | 100 ++--- stats/pwc-summary.json | 48 +-- 12 files changed, 1046 insertions(+), 1023 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 18d89156ec..4d4453c9c1 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,17 +1,262 @@ { + "subtitle" : { + "text" : "[Champions: 42] Last updated at 2020-09-21 20:20:00 GMT" + }, + "series" : [ + { + "name" : "Perl Weekly Challenge - 078", + "data" : [ + { + "drilldown" : "Abigail", + "y" : 2, + "name" : "Abigail" + }, + { + "name" : "Adam Russell", + "y" : 3, + "drilldown" : "Adam Russell" + }, + { + "drilldown" : "Alexander Pankoff", + "y" : 2, + "name" : "Alexander Pankoff" + }, + { + "y" : 4, + "drilldown" : "Andinus", + "name" : "Andinus" + }, + { + "drilldown" : "Andrew Shitov", + "y" : 3, + "name" : "Andrew Shitov" + }, + { + "name" : "Anton Fedotov", + "y" : 2, + "drilldown" : "Anton Fedotov" + }, + { + "drilldown" : "Arne Sommer", + "y" : 5, + "name" : "Arne Sommer" + }, + { + "y" : 4, + "drilldown" : "Athanasius", + "name" : "Athanasius" + }, + { + "drilldown" : "Bob Lied", + "y" : 2, + "name" : "Bob Lied" + }, + { + "y" : 2, + "drilldown" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" + }, + { + "name" : "Colin Crain", + "y" : 5, + "drilldown" : "Colin Crain" + }, + { + "name" : "Dave Cross", + "drilldown" : "Dave Cross", + "y" : 2 + }, + { + "name" : "Dave Jacoby", + "y" : 2, + "drilldown" : "Dave Jacoby" + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "drilldown" : "Flavio Poletti", + "y" : 4, + "name" : "Flavio Poletti" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "y" : 5, + "name" : "Jaldhar H. Vyas" + }, + { + "y" : 2, + "drilldown" : "James Smith", + "name" : "James Smith" + }, + { + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek", + "y" : 2 + }, + { + "drilldown" : "Jason Messer", + "y" : 2, + "name" : "Jason Messer" + }, + { + "name" : "Jorg Sommrey", + "y" : 2, + "drilldown" : "Jorg Sommrey" + }, + { + "drilldown" : "Julio de Castro", + "y" : 4, + "name" : "Julio de Castro" + }, + { + "name" : "Kang-min Liu", + "y" : 2, + "drilldown" : "Kang-min Liu" + }, + { + "drilldown" : "Laurent Rosenfeld", + "y" : 5, + "name" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Lubos Kolouch", + "y" : 2, + "name" : "Lubos Kolouch" + }, + { + "name" : "Mark Anderson", + "y" : 2, + "drilldown" : "Mark Anderson" + }, + { + "name" : "Markus Holzer", + "y" : 4, + "drilldown" : "Markus Holzer" + }, + { + "y" : 7, + "drilldown" : "Mohammad S Anwar", + "name" : "Mohammad S Anwar" + }, + { + "name" : "Myoungjin Jeon", + "y" : 4, + "drilldown" : "Myoungjin Jeon" + }, + { + "drilldown" : "Niels van Dijke", + "y" : 2, + "name" : "Niels van Dijke" + }, + { + "name" : "Nuno Vieira", + "y" : 2, + "drilldown" : "Nuno Vieira" + }, + { + "drilldown" : "Pete Houston", + "y" : 2, + "name" : "Pete Houston" + }, + { + "name" : "Richard Park", + "y" : 2, + "drilldown" : "Richard Park" + }, + { + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West", + "y" : 5 + }, + { + "drilldown" : "Shahed Nooshmand", + "y" : 3, + "name" : "Shahed Nooshmand" + }, + { + "name" : "Shawn Wagner", + "drilldown" : "Shawn Wagner", + "y" : 2 + }, + { + "name" : "Simon Green", + "drilldown" : "Simon Green", + "y" : 3 + }, + { + "name" : "Simon Proctor", + "y" : 2, + "drilldown" : "Simon Proctor" + }, + { + "y" : 1, + "drilldown" : "Steven Wilson", + "name" : "Steven Wilson" + }, + { + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke", + "y" : 4 + }, + { + "name" : "Vinod Kumar K", + "drilldown" : "Vinod Kumar K", + "y" : 1 + }, + { + "name" : "Walt Mankowski", + "y" : 3, + "drilldown" : "Walt Mankowski" + }, + { + "name" : "Wanderdoc", + "drilldown" : "Wanderdoc", + "y" : 2 + } + ], + "colorByPoint" : 1 + } + ], + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" + }, + "title" : { + "text" : "Perl Weekly Challenge - 078" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
" + }, "drilldown" : { "series" : [ { "id" : "Abigail", + "name" : "Abigail", "data" : [ [ "Perl", 2 ] - ], - "name" : "Abigail" + ] }, { + "name" : "Adam Russell", "data" : [ [ "Perl", @@ -22,22 +267,21 @@ 1 ] ], - "id" : "Adam Russell", - "name" : "Adam Russell" + "id" : "Adam Russell" }, { + "id" : "Alexander Pankoff", "name" : "Alexander Pankoff", "data" : [ [ "Perl", 2 ] - ], - "id" : "Alexander Pankoff" + ] }, { - "name" : "Andinus", "id" : "Andinus", + "name" : "Andinus", "data" : [ [ "Perl", @@ -50,7 +294,6 @@ ] }, { - "id" : "Andrew Shitov", "data" : [ [ "Raku", @@ -61,19 +304,21 @@ 1 ] ], - "name" : "Andrew Shitov" + "name" : "Andrew Shitov", + "id" : "Andrew Shitov" }, { + "name" : "Anton Fedotov", "data" : [ [ "Perl", 2 ] ], - "id" : "Anton Fedotov", - "name" : "Anton Fedotov" + "id" : "Anton Fedotov" }, { + "name" : "Arne Sommer", "data" : [ [ "Perl", @@ -88,8 +333,7 @@ 1 ] ], - "id" : "Arne Sommer", - "name" : "Arne Sommer" + "id" : "Arne Sommer" }, { "id" : "Athanasius", @@ -106,8 +350,8 @@ "name" : "Athanasius" }, { - "name" : "Bob Lied", "id" : "Bob Lied", + "name" : "Bob Lied", "data" : [ [ "Perl", @@ -116,14 +360,14 @@ ] }, { - "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "name" : "Cheok-Yin Fung" + "id" : "Cheok-Yin Fung" }, { "id" : "Colin Crain", @@ -154,28 +398,27 @@ "name" : "Dave Cross" }, { - "name" : "Dave Jacoby", "id" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Dave Jacoby" }, { + "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], - "id" : "E. Choroba", - "name" : "E. Choroba" + "id" : "E. Choroba" }, { "name" : "Flavio Poletti", - "id" : "Flavio Poletti", "data" : [ [ "Perl", @@ -185,27 +428,46 @@ "Blog", 2 ] - ] + ], + "id" : "Flavio Poletti" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Jaldhar H. Vyas", + "id" : "Jaldhar H. Vyas" }, { - "id" : "James Smith", "data" : [ [ "Perl", 2 ] ], - "name" : "James Smith" + "name" : "James Smith", + "id" : "James Smith" }, { "id" : "Jan Krnavek", + "name" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ], - "name" : "Jan Krnavek" + ] }, { "name" : "Jason Messer", @@ -229,7 +491,6 @@ }, { "name" : "Julio de Castro", - "id" : "Julio de Castro", "data" : [ [ "Perl", @@ -239,7 +500,8 @@ "Raku", 2 ] - ] + ], + "id" : "Julio de Castro" }, { "name" : "Kang-min Liu", @@ -270,27 +532,26 @@ "name" : "Laurent Rosenfeld" }, { + "id" : "Lubos Kolouch", "name" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] - ], - "id" : "Lubos Kolouch" + ] }, { - "name" : "Mark Anderson", + "id" : "Mark Anderson", "data" : [ [ "Raku", 2 ] ], - "id" : "Mark Anderson" + "name" : "Mark Anderson" }, { - "name" : "Markus Holzer", "data" : [ [ "Perl", @@ -301,10 +562,10 @@ 2 ] ], + "name" : "Markus Holzer", "id" : "Markus Holzer" }, { - "name" : "Mohammad S Anwar", "data" : [ [ "Perl", @@ -319,11 +580,12 @@ 3 ] ], + "name" : "Mohammad S Anwar", "id" : "Mohammad S Anwar" }, { - "name" : "Myoungjin Jeon", "id" : "Myoungjin Jeon", + "name" : "Myoungjin Jeon", "data" : [ [ "Perl", @@ -336,36 +598,37 @@ ] }, { + "id" : "Niels van Dijke", "name" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] - ], - "id" : "Niels van Dijke" + ] }, { - "name" : "Nuno Vieira", "id" : "Nuno Vieira", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Nuno Vieira" }, { + "name" : "Pete Houston", "data" : [ [ "Perl", 2 ] ], - "id" : "Pete Houston", - "name" : "Pete Houston" + "id" : "Pete Houston" }, { + "id" : "Richard Park", "data" : [ [ "Raku", @@ -376,12 +639,9 @@ 1 ] ], - "id" : "Richard Park", "name" : "Richard Park" }, { - "name" : "Roger Bell_West", - "id" : "Roger Bell_West", "data" : [ [ "Perl", @@ -395,11 +655,11 @@ "Blog", 1 ] - ] + ], + "name" : "Roger Bell_West", + "id" : "Roger Bell_West" }, { - "name" : "Shahed Nooshmand", - "id" : "Shahed Nooshmand", "data" : [ [ "Raku", @@ -409,21 +669,23 @@ "Blog", 1 ] - ] + ], + "name" : "Shahed Nooshmand", + "id" : "Shahed Nooshmand" }, { - "name" : "Shawn Wagner", "id" : "Shawn Wagner", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Shawn Wagner" }, { - "name" : "Simon Green", "id" : "Simon Green", + "name" : "Simon Green", "data" : [ [ "Perl", @@ -446,17 +708,16 @@ "id" : "Simon Proctor" }, { - "id" : "Steven Wilson", + "name" : "Steven Wilson", "data" : [ [ "Perl", 1 ] ], - "name" : "Steven Wilson" + "id" : "Steven Wilson" }, { - "name" : "Ulrich Rieke", "id" : "Ulrich Rieke", "data" : [ [ @@ -467,7 +728,8 @@ "Raku", 2 ] - ] + ], + "name" : "Ulrich Rieke" }, { "data" : [ @@ -476,11 +738,11 @@ 1 ] ], - "id" : "Vinod Kumar K", - "name" : "Vinod Kumar K" + "name" : "Vinod Kumar K", + "id" : "Vinod Kumar K" }, { - "name" : "Walt Mankowski", + "id" : "Walt Mankowski", "data" : [ [ "Perl", @@ -491,265 +753,26 @@ 1 ] ], - "id" : "Walt Mankowski" + "name"