diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-26 08:45:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-26 08:45:56 +0100 |
| commit | 751fc7b9e8c09eea769183bddcae4ea4c97f336a (patch) | |
| tree | c1914856f9540254ece5d78dad99625e5d09b9c8 | |
| parent | 31011089fc9fd0202e6b99f557b311f7ac840495 (diff) | |
| parent | 10b0539795b7f0c84b0484d061d164a4219cb26e (diff) | |
| download | perlweeklychallenge-club-751fc7b9e8c09eea769183bddcae4ea4c97f336a.tar.gz perlweeklychallenge-club-751fc7b9e8c09eea769183bddcae4ea4c97f336a.tar.bz2 perlweeklychallenge-club-751fc7b9e8c09eea769183bddcae4ea4c97f336a.zip | |
Merge pull request #4599 from jaldhar/challenge-122
Challenge 122 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-122/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-122/jaldhar-h-vyas/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-122/jaldhar-h-vyas/perl/ch-2.pl | 57 | ||||
| -rwxr-xr-x | challenge-122/jaldhar-h-vyas/raku/ch-1.raku | 14 | ||||
| -rwxr-xr-x | challenge-122/jaldhar-h-vyas/raku/ch-2.raku | 39 |
5 files changed, 125 insertions, 0 deletions
diff --git a/challenge-122/jaldhar-h-vyas/blog.txt b/challenge-122/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..f4e4dabb76 --- /dev/null +++ b/challenge-122/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2021/07/perl_weekly_challenge_week_122.html diff --git a/challenge-122/jaldhar-h-vyas/perl/ch-1.pl b/challenge-122/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..06a6fdfe09 --- /dev/null +++ b/challenge-122/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +my $total = 0; +my $count = 1; +my @averages; + +for my $arg (@ARGV) { + $total += $arg; + push @averages, $total / $count++; +} + +say join q{ }, @averages; diff --git a/challenge-122/jaldhar-h-vyas/perl/ch-2.pl b/challenge-122/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..e176a2e430 --- /dev/null +++ b/challenge-122/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +sub arrange { + my ($ones, $twos, $threes) = @_; + my @arranged; + + if ($ones) { + push @arranged, (split //, ('1' x $ones)); + } + + if ($twos) { + push @arranged, (split //, ('2' x $twos)); + } + + if ($threes) { + push @arranged, (split //, ('3' x $threes)); + } + + return @arranged; +} + +sub permute (&@) { + my $code = shift; + my @idx = 0..$#_; + while ( $code->(@_[@idx]) ) { + my $p = $#idx; + --$p while $idx[$p-1] > $idx[$p]; + my $q = $p or return; + push @idx, reverse splice @idx, $p; + ++$q while $idx[$p-1] > $idx[$q]; + @idx[$p-1,$q]=@idx[$q,$p-1]; + } +} + +my $S = shift // die "Need score as an integer.\n"; +my %scores; + +for my $threes (0 .. $S / 3) { + my $remainder = $S - $threes * 3; + for my $twos (0 .. $remainder / 2) { + my $ones = $remainder - $twos * 2; + + my @permutations; + permute { push @permutations, \@_; } + arrange($ones, $twos, $threes); + + for my $perm (@permutations) { + $scores{join q{}, @{$perm}}++; + } + } +} + +for my $score (sort keys %scores) { + say join q{ }, (split //, $score); +} diff --git a/challenge-122/jaldhar-h-vyas/raku/ch-1.raku b/challenge-122/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..90d75a53de --- /dev/null +++ b/challenge-122/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/raku + +sub MAIN(*@ARGS) { + my $total = 0; + my $count = 1; + my @averages; + + for @ARGS -> $arg { + $total += $arg; + @averages.push($total / $count++); + } + + @averages.join(q{ }).say; +} diff --git a/challenge-122/jaldhar-h-vyas/raku/ch-2.raku b/challenge-122/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..86c4337f7c --- /dev/null +++ b/challenge-122/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,39 @@ +#!/usr/bin/raku + +sub arrangement(Int $ones, Int $twos, Int $threes) { + my @arranged; + + if $ones { + @arranged.push(| ('1' xx $ones)); + } + + if $twos { + @arranged.push(| ('2' xx $twos)); + } + + if $threes { + @arranged.push(| ('3' xx $threes)); + } + + return @arranged; +} + +sub MAIN( + Int $S #= score +) { + my %scores; + + for 0 .. $S div 3 -> $threes { + my $remainder = $S - $threes * 3; + for 0 .. $remainder div 2 -> $twos { + my $ones = $remainder - $twos * 2; + for arrangement($ones, $twos, $threes).permutations -> $perm { + %scores{$perm}++; + } + } + } + + for %scores.keys.sort -> $score { + say $score; + } +} |
