diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-30 05:06:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-30 05:06:04 +0000 |
| commit | 7e6fbc0404eff58fe9670f95120c4d46f113570b (patch) | |
| tree | 38ea6f5390d7cb59c1fde3957be72f40c5459023 /challenge-201 | |
| parent | cc865a4fbaf889f6304aa3003a08283a19803467 (diff) | |
| parent | a578486e0716f438e76a1c7fa8eea993d9c3da5c (diff) | |
| download | perlweeklychallenge-club-7e6fbc0404eff58fe9670f95120c4d46f113570b.tar.gz perlweeklychallenge-club-7e6fbc0404eff58fe9670f95120c4d46f113570b.tar.bz2 perlweeklychallenge-club-7e6fbc0404eff58fe9670f95120c4d46f113570b.zip | |
Merge pull request #7497 from jaldhar/challenge-201
Challenge 201 by Jaldhar H. Vyas.
Diffstat (limited to 'challenge-201')
| -rw-r--r-- | challenge-201/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-201/jaldhar-h-vyas/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-201/jaldhar-h-vyas/perl/ch-2.pl | 42 | ||||
| -rwxr-xr-x | challenge-201/jaldhar-h-vyas/raku/ch-1.raku | 10 | ||||
| -rwxr-xr-x | challenge-201/jaldhar-h-vyas/raku/ch-2.raku | 41 |
5 files changed, 107 insertions, 0 deletions
diff --git a/challenge-201/jaldhar-h-vyas/blog.txt b/challenge-201/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..8771085f6c --- /dev/null +++ b/challenge-201/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/01/perl_weekly_challenge_week_201.html
\ No newline at end of file diff --git a/challenge-201/jaldhar-h-vyas/perl/ch-1.pl b/challenge-201/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..d76ca85cb4 --- /dev/null +++ b/challenge-201/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @array = @ARGV; +my @need = 0 .. scalar @array; + +my %count; +for my $elem (@array, @need) { + $count{$elem}++; +} + +say join(q{, }), grep { $count{$_} < 2; } keys %count; diff --git a/challenge-201/jaldhar-h-vyas/perl/ch-2.pl b/challenge-201/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..b0c99ecc73 --- /dev/null +++ b/challenge-201/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub partitions { + my ($n) = @_; + my @combos; + my @piles; + my $i = 0; + $piles[$i] = $n; + + while (1) { + push @combos, [@piles]; + + my $remainder = 0; + while ($i >= 0 && $piles[$i] == 1) { + $remainder += $piles[$i]; + $i--; + } + + if ($i < 0) { + last; + } + + $piles[$i]--; + $remainder++; + + while ($remainder > $piles[$i]) { + $piles[$i + 1] = $piles[$i]; + $remainder = $remainder - $piles[$i]; + $i++; + } + $piles[$i + 1] = $remainder; + $i++; + } + + return @combos; +} + +my $n = shift; + +say scalar partitions(5);
\ No newline at end of file diff --git a/challenge-201/jaldhar-h-vyas/raku/ch-1.raku b/challenge-201/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..aa241f872f --- /dev/null +++ b/challenge-201/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/raku + +sub MAIN( + *@array +) { + my @need = 0 .. @array.elems; + my Set $missing = @need ∖ @array.map({ $_.Int }); + + $missing.keys.join(q{, }).say; +}
\ No newline at end of file diff --git a/challenge-201/jaldhar-h-vyas/raku/ch-2.raku b/challenge-201/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..aab2244791 --- /dev/null +++ b/challenge-201/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/raku + +sub partitions(Int $n) { + my @combos; + my @piles; + my $i = 0; + @piles[$i] = $n; + + loop { + @combos.push([@piles]); + + my $remainder = 0; + while ($i >= 0 && @piles[$i] == 1) { + $remainder += @piles[$i]; + $i--; + } + + if ($i < 0) { + last; + } + + @piles[$i]--; + $remainder++; + + while ($remainder > @piles[$i]) { + @piles[$i + 1] = @piles[$i]; + $remainder = $remainder - @piles[$i]; + $i++; + } + @piles[$i + 1] = $remainder; + $i++; + } + + return @combos; +} + +sub MAIN( + Int $n where { $n > 0; } #= an integer greater than zero +) { + say partitions($n).elems; +}
\ No newline at end of file |
