diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-29 11:26:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-29 11:26:20 +0000 |
| commit | a5ca1106dadbd6b0e7b91671ac692013852d73d3 (patch) | |
| tree | 81d93877a6c09476121942826d367c8b290393ba | |
| parent | 71d0df1c25084d211592d7e4ca9eb73267023a58 (diff) | |
| parent | 5e0f44f9e405f253382a03735e7caed4ccde10fa (diff) | |
| download | perlweeklychallenge-club-a5ca1106dadbd6b0e7b91671ac692013852d73d3.tar.gz perlweeklychallenge-club-a5ca1106dadbd6b0e7b91671ac692013852d73d3.tar.bz2 perlweeklychallenge-club-a5ca1106dadbd6b0e7b91671ac692013852d73d3.zip | |
Merge pull request #7478 from pip/branch-for-challenge-201
Pip Stuart's submission for challenge-201.
| -rw-r--r-- | challenge-201/pip/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-201/pip/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-201/pip/raku/ch-1.raku | 22 | ||||
| -rw-r--r-- | challenge-201/pip/raku/ch-2.raku | 35 |
4 files changed, 114 insertions, 0 deletions
diff --git a/challenge-201/pip/perl/ch-1.pl b/challenge-201/pip/perl/ch-1.pl new file mode 100644 index 0000000000..808319b6e4 --- /dev/null +++ b/challenge-201/pip/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #1 of Week #201 - Pip Stuart +# Missing Numbers: You are given an array of unique numbers. Write a script to find out all missing numbers in the range 0..$n where $n is the array size. +# Example1: +# In-put: @array = (0,1,3) +# Output: 2 The array size i.e. total element count is 3, so the range is 0..3. The missing number is 2 in the given array. +# Example2: +# In-put: @array = (0,1) +# Output: 2 The array size is 2, therefore the range is 0..2. The missing number is 2. +use strict;use warnings;use utf8;use v5.10;my $d8VS='N1OLH3eX'; +sub MNum {my @aray=@_;my @mnmz=();my %hash=(); + print '(' . sprintf("%-5s",join(',',@aray)) . ') => (';$hash{$_}=1 for(@aray); + for (0..@aray){push(@mnmz,$_) unless(exists($hash{$_}));} + say join(',',@mnmz) . ');'; + return( @mnmz); +} +if (@ARGV) { + MNum(@ARGV); +} else { + MNum(0,1,3); # => 2 + MNum(0,1 ); # => 2 +} diff --git a/challenge-201/pip/perl/ch-2.pl b/challenge-201/pip/perl/ch-2.pl new file mode 100644 index 0000000000..0e14d5756c --- /dev/null +++ b/challenge-201/pip/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #2 of Week #201 - Pip Stuart +# Penny Piles: You are given an integer, $n > 0. +# Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. +# Example1: +# In-put: $n = 5 +# Output: 7 Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +# 1 1 1 1 1 +# 1 1 1 2 +# 1 2 2 +# 1 1 3 +# 2 3 +# 1 4 +# 5 +# Last date to submit the solution 23:59 (UK Time) Sunday 29th January 2023. +use strict;use warnings;use utf8;use v5.10;my $d8VS='N1PLC2pr';my @nszs=({}, {'1'=>1}); # made global to preserve for multiple calls +sub PPil {my $numb=shift(@_);my $pilz=0; # I originally tried just merging piles with complex loops throughout the pennies but this was ultimately limited; + for my $size (@nszs..$numb){ # fill NumberSiZeS array of hashes up to desired $numb; # Merged piles skipped lots but had many 1,2,4,8,16 hits; + for my $pway (sort keys %{$nszs[$size-1]}){ # loop through all previous size pile-ways + $nszs[$size]{"1,$pway"}=1;my @prvp=split(/,/,$pway); # prepend new 1 penny to current size + for my $pndx (0..$#prvp){ # distribute new size layer's 1 over all the piles of the old ways + $prvp[$pndx-1]-- if ($pndx); + $prvp[$pndx ]++; + $nszs[$size]{join(',',sort {$a <=> $b} @prvp)}=1; } } + } say join("\n",sort keys %{$nszs[$numb]}); + $pilz=scalar keys %{$nszs[$numb]} ; + say "$numb => $pilz;"; + return( $pilz ); +} +if (@ARGV) { + PPil(@ARGV); +} else { # 1,2,3=>1,2,3; 4=>5, 5=>7 ,6=>11, 7 => 15, 8 => 22, 9 => 30, 10 => 42; + PPil( 5); # => 7; + PPil($_) for(1..10); +} diff --git a/challenge-201/pip/raku/ch-1.raku b/challenge-201/pip/raku/ch-1.raku new file mode 100644 index 0000000000..19524a0eb9 --- /dev/null +++ b/challenge-201/pip/raku/ch-1.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #1 of Week #201 - Pip Stuart +# Missing Numbers: You are given an array of unique numbers. Write a script to find out all missing numbers in the range 0..$n where $n is the array size. +# Example1: +# In-put: @array = (0,1,3) +# Output: 2 The array size i.e. total element count is 3, so the range is 0..3. The missing number is 2 in the given array. +# Example2: +# In-put: @array = (0,1) +# Output: 2 The array size is 2, therefore the range is 0..2. The missing number is 2. +use v6;my $d8VS='N1PLFupi'; +sub MNum {my @aray=@_;my @mnmz=();my %hash=(); + print '(' ~ sprintf("%-5s",join(',',@aray)) ~ ') => (';%hash{$_}=1 for @aray; + for 0..@aray.elems {@mnmz.push($_) unless %hash{$_}:exists;} + say join(',',@mnmz) ~ ');'; + return @mnmz; +} +if (@*ARGS) { + MNum(@*ARGS); +} else { + MNum(0,1,3); # => 2 + MNum(0,1 ); # => 2 +} diff --git a/challenge-201/pip/raku/ch-2.raku b/challenge-201/pip/raku/ch-2.raku new file mode 100644 index 0000000000..f7e80103cf --- /dev/null +++ b/challenge-201/pip/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #2 of Week #201 - Pip Stuart +# Penny Piles: You are given an integer, $n > 0. +# Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. +# Example1: +# In-put: $n = 5 +# Output: 7 Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +# 1 1 1 1 1 +# 1 1 1 2 +# 1 2 2 +# 1 1 3 +# 2 3 +# 1 4 +# 5 +# Last date to submit the solution 23:59 (UK Time) Sunday 29th January 2023. +use v6; my $d8VS='N1PLGHuD';my @nszs=({}, {'1'=>1}); # made global to preserve for multiple calls +sub PPil {my $numb=@_.shift;my $pilz=0; + for @nszs.elems..$numb -> $size { # fill NumberSiZeS array of hashes up to desired $numb + for @nszs[$size-1].keys.sort -> $pway { # loop through all previous size pile-ways + @nszs[$size]{"1,$pway"}=1;my @prvp=$pway.split(','); # prepend new 1 penny to current size + for 0..@prvp.elems-1 -> $pndx { # distribute new size layer's 1 over all the piles of the old ways + @prvp[$pndx-1]-- if ($pndx); + @prvp[$pndx ]++; + @nszs[$size]{@prvp.sort.join(',')}=1; } } + }; @nszs[$numb ].keys.sort.join("\n").say; + $pilz=@nszs[$numb ].keys.elems; + say "$numb => $pilz;"; + return $pilz ; +} +if (@*ARGS) { + PPil(@*ARGS);if (@*ARGS[0] >= 32) {my $nowi = now;say "now-INIT:" ~ $nowi - INIT now;} # 32=>9630 takes over 3 seconds to run on my machine +} else { # 1,2,3=>1,2,3; 4=>5, 5=>7 ,6=>11, 7 => 15, 8 => 22, 9 => 30, 10 => 42; + PPil( 5); # => 7; + PPil($_) for 1..10 ; +} |
