diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-24 22:45:38 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-24 22:45:38 +1000 |
| commit | 811f5ea123eb6c84b2856d99a9983d083789e8d5 (patch) | |
| tree | 80070eadd63e441da049e6d908aeace65bd67f6c /challenge-079 | |
| parent | 94471ad8790745216a12f45436db34113aa55bbb (diff) | |
| parent | b7e37689a4d9f7a80b263a58ae289c56afe3aa34 (diff) | |
| download | perlweeklychallenge-club-811f5ea123eb6c84b2856d99a9983d083789e8d5.tar.gz perlweeklychallenge-club-811f5ea123eb6c84b2856d99a9983d083789e8d5.tar.bz2 perlweeklychallenge-club-811f5ea123eb6c84b2856d99a9983d083789e8d5.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-079')
| -rwxr-xr-x | challenge-079/cristian-heredia/perl/ch-1.pl | 59 | ||||
| -rwxr-xr-x | challenge-079/dave-jacoby/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-079/dave-jacoby/perl/ch-2.pl | 70 | ||||
| -rw-r--r-- | challenge-079/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-079/laurent-rosenfeld/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-079/laurent-rosenfeld/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-079/laurent-rosenfeld/raku/ch-1.sh | 1 | ||||
| -rw-r--r-- | challenge-079/laurent-rosenfeld/raku/ch-2.raku | 32 | ||||
| -rw-r--r-- | challenge-079/markus-holzer/raku/ch-2.raku | 19 |
9 files changed, 271 insertions, 1 deletions
diff --git a/challenge-079/cristian-heredia/perl/ch-1.pl b/challenge-079/cristian-heredia/perl/ch-1.pl new file mode 100755 index 0000000000..30e2136aa5 --- /dev/null +++ b/challenge-079/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,59 @@ +#You are given a positive number $N. +#Write a script to count the total number of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. + + #Input: $N = 3 + + #Explanation: First find out the set bit counts of all numbers i.e. 1, 2 and 3. + + # Decimal: 1 + # Binary: 01 + # Set Bit Count: 1 + + # Decimal: 2 + # Binary: 10 + # Set Bit Count: 1 + + # Decimal: 3 + # Binary: 11 + # Set Bit Count: 2 + + # Total set bit count: 1 + 1 + 2 = 4 + + #Output: Your script should print `4` as `4 % 1000000007 = 4`. + +use strict; +use warnings; +use Data::Dumper; + +#variables +my $N = 3; +my $bin; +my @array; +my $count = 0; + + +convertBinary(); +sub convertBinary { + + foreach(my $i = 1; $i <= $N; $i++) { + $bin = sprintf ("%b", $i); + @array = (); + @array = split //, $bin; + countBit(); + } + + print "output: $count\n"; +} + +sub countBit { + + foreach(my $j = 0; $j < @array; $j++) { + if ($array[$j] == 1) { + $count++; + } + } +} + + + + diff --git a/challenge-079/dave-jacoby/perl/ch-1.pl b/challenge-079/dave-jacoby/perl/ch-1.pl new file mode 100755 index 0000000000..b1e4bff87e --- /dev/null +++ b/challenge-079/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use Carp; +use List::Util qw{ sum }; +use Getopt::Long; + +my $n = 4; +GetOptions( 'n=i' => \$n, ); +croak 'Non-positive number' if $n < 0; + +my $total = count_set_bits($n); +say $total; + +sub count_set_bits( $n ) { + my $total = 0; + my $t = 0; + for my $i ( 0 .. $n ) { + my $b = sprintf '%b', $i; + my $c = sum split m{|}, $b; + $total += $c; + $t = $total % 1000000007; + } + + return $t; +} diff --git a/challenge-079/dave-jacoby/perl/ch-2.pl b/challenge-079/dave-jacoby/perl/ch-2.pl new file mode 100755 index 0000000000..b2b8265e48 --- /dev/null +++ b/challenge-079/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use Getopt::Long; +use List::Util qw{ max sum0 }; + +my @n = grep { $_ >= 1 } @ARGV; +@n = ( 2, 1, 4, 1, 2, 5 ) unless scalar @n; + +# make_histogram(@n); +my $trapped = trap_rain_water(@n); + +my $units = $trapped == 1 ? 'unit' : 'units'; +say qq{$trapped $units trapped}; + +sub make_histogram ( @n ) { + my $max = max @n; + say ''; + for my $i ( reverse 1 .. $max ) { + my @h = map { $i <= $_ ? '#' : ' ' } @n; + say join ' ', $i, @h; + } + say join '-', ' ', map { '-' } @n; + say join ' ', ' ', @n; + say ''; +} + +sub trap_rain_water ( @n ) { + my $max = max @n; + my $s = scalar @n; + my $c = 0; + + my @hist; + + for my $i ( reverse 1 .. $max ) { + my $z = sum0 map { $i <= $_ ? 1 : 0 } @n; + my @h; + my $hh = []; + push $hh->@*, $i, ''; + + for my $j ( 0 .. $s - 1 ) { + my $e = $n[$j]; # equals + my $p = $e >= $i ? 1 : 0; # is peak + + my @lt = @n[ 0 .. $j - 1 ]; + my @gt = @n[ $j + 1 .. $s - 1 ]; + my $lt = scalar grep { $_ >= $i } @lt; # is peak to left + my $gt = scalar grep { $_ >= $i } @gt; # is peak to right + my $t = $p != 1 && $lt > 0 && $gt > 0 ? 1 : 0; # has trapped + $c += $t; + + push @h, $e >= $i ? '#' : $t; + my $v = ' '; + $v = '#' if $p; + $v = '.' if $t; + push $hh->@*, $v; + } + + push @hist, $hh; + } + + say join "\n", map { join ' ', $_->@* } @hist, [], [ ' ', '', @n ]; + say ''; + + return $c; +} diff --git a/challenge-079/laurent-rosenfeld/blog.txt b/challenge-079/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..15f7c0b465 --- /dev/null +++ b/challenge-079/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2020/09/perl-weekly-challenge-79-count-set-bits-and-trapped-rain-water.html diff --git a/challenge-079/laurent-rosenfeld/perl/ch-1.pl b/challenge-079/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..4033707433 --- /dev/null +++ b/challenge-079/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,10 @@ +use strict; +use warnings; +use feature "say"; + +my $n = shift; +my $sum; +for my $num (1..$n) { + $sum += $_ for split '', sprintf "%b", $num; +} +say $sum % 1000000007; diff --git a/challenge-079/laurent-rosenfeld/perl/ch-2.pl b/challenge-079/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..33f6f1cc2e --- /dev/null +++ b/challenge-079/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,50 @@ +use strict; +use warnings; +use feature "say"; + +my @a = @ARGV > 1 ? @ARGV : ( 2, 1, 4, 5, 3, 7); +draw_histo(@a); +say "Rain capacity is: ", capacity(@a); + +sub max { + my @vals = @_; + my $max = shift @vals; + for my $val (@vals) { + $max = $val if $val > $max; + } + return $max; +} + +sub min2 { + $_[0] < $_[1] ? $_[0] : $_[1]; +} + +sub draw_histo { + my @in = @_; + my $max_val = max @in; + say ""; + for my $ordinate (reverse 1..$max_val) { + print $ordinate; + for my $i (0..$#in) { + print $in[$i] >= $ordinate ? " # " : " "; + } + say ""; + } + say " =" x scalar @in; + say " ", join " ", @in; + say ""; +} + +sub capacity { + my @in = @_; + my $left_max = $in[0]; + my $total = 0; + for my $i (1..$#in-1) { + $left_max = $in[$i] and next if $in[$i] > $left_max; + my $right_max = max @in[$i+1..$#in]; + my $col = min2($left_max, $right_max) - $in[$i]; + next if $col < 0; + $total += $col; + } + return $total +} diff --git a/challenge-079/laurent-rosenfeld/raku/ch-1.sh b/challenge-079/laurent-rosenfeld/raku/ch-1.sh new file mode 100644 index 0000000000..c6f7baba11 --- /dev/null +++ b/challenge-079/laurent-rosenfeld/raku/ch-1.sh @@ -0,0 +1 @@ +raku -e 'say ([+] map { .fmt("%b").comb.sum }, 1..@*ARGS[0]) % 1000000007' diff --git a/challenge-079/laurent-rosenfeld/raku/ch-2.raku b/challenge-079/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..c7d571f9ee --- /dev/null +++ b/challenge-079/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,32 @@ +use v6; + +my @a = @*ARGS.elems > 1 ?? @*ARGS !! (2, 1, 4, 1, 2, 5); +draw-histo(@a); +say "Rain capacity is: ", capacity(@a); + + +sub draw-histo (@in) { + my $max-val = @in.max; + say ""; + for (1..$max-val).reverse -> $ordinate { + print $ordinate; + for 0..@in.end -> $i { + print @in[$i] >= $ordinate ?? " # " !! " "; + } + say ""; + } + say " =" x @in.elems; + say " ", join " ", @in, "\n"; +} + +sub capacity (@in) { + my $left-max = @in[0]; + my $total = 0; + for 1..@in.end-1 -> $i { + $left-max = @in[$i] and next if @in[$i] > $left-max; + my $right-max = max @in[$i+1..@in.end]; + my $col = min($left-max, $right-max) - @in[$i]; + $total += $col if $col > 0; + } + return $total +} diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku index d674dca8af..c3a961e708 100644 --- a/challenge-079/markus-holzer/raku/ch-2.raku +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -1,9 +1,26 @@ unit sub MAIN( *@N where @N.all ~~ Int ); +my $max = @N.max; + +my @histo = reverse [Z] + (' ', '-', |(1..$max) ), + |@N.map: { $_, '-', |('#' xx $_), |(' ' xx ($max - $_)) }; + +my @pretty = @histo.map({ + .join('') + .subst(/ <?after '#'> ( \s+ ) <?before '#'> /, { '~' x .chars }, :g) + .subst('', ' ', :g )}); + + +.say and .indices('~').elems.say with @pretty.join("\n"); + +# This was my first attempt before I realized we're supposed to +# print the histogram too and I can just count the ~ in the output + sub index-find { @N.pairs.grep( *.value >= $^h ).map: *.key } sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 } -say (@N.max...0) +say ($max...0) .map( &index-find ) .map( &index-diff ) .flat |
