diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-11 03:24:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-11 03:24:27 +0100 |
| commit | 16cf84758036d12780a57acf2f2fd5f35421394b (patch) | |
| tree | d0a04839df974008fbc31593a00dbcf3eb02b427 | |
| parent | 4b9f5613915352541b2de8da593c6289cfa51b11 (diff) | |
| parent | 0c3d710a62f0c993a49cc0f3fc5a17998a747518 (diff) | |
| download | perlweeklychallenge-club-16cf84758036d12780a57acf2f2fd5f35421394b.tar.gz perlweeklychallenge-club-16cf84758036d12780a57acf2f2fd5f35421394b.tar.bz2 perlweeklychallenge-club-16cf84758036d12780a57acf2f2fd5f35421394b.zip | |
Merge pull request #6420 from jaldhar/challenge-172
Challenge 172 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-172/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-172/jaldhar-h-vyas/perl/ch-1.pl | 87 | ||||
| -rwxr-xr-x | challenge-172/jaldhar-h-vyas/perl/ch-2.pl | 12 | ||||
| -rwxr-xr-x | challenge-172/jaldhar-h-vyas/raku/ch-1.raku | 20 | ||||
| -rwxr-xr-x | challenge-172/jaldhar-h-vyas/raku/ch-2.raku | 14 |
5 files changed, 134 insertions, 0 deletions
diff --git a/challenge-172/jaldhar-h-vyas/blog.txt b/challenge-172/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..e26a67b76a --- /dev/null +++ b/challenge-172/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2022/07/perl_weekly_challenge_week_172.html
\ No newline at end of file diff --git a/challenge-172/jaldhar-h-vyas/perl/ch-1.pl b/challenge-172/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..58c6eb17ef --- /dev/null +++ b/challenge-172/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use English qw/ -no_match_vars /; + +sub combinations { + my @list = @{$_[0]}; + my $length = $_[1]; + + if ($length <= 1) { + return map [$_], @list; + } + + my @combos; + + for (my $i = 0; $i + $length <= scalar @list; $i++) { + my $val = $list[$i]; + my @rest = @list[$i + 1 .. $#list]; + for my $c (combinations(\@rest, $length - 1)) { + push @combos, [$val, @{$c}] ; + } + } + + return @combos; +} + +sub isPrime { + my ($n) = @_; + + if ($n < 2) { + return undef; + } + + if ($n == 2) { + return 1; + } + + for my $i (2 .. sqrt($n)) { + if ($n % $i == 0) { + return undef; + } + } + + return 1; +} + +sub sum { + my ($arr) = @_; + my $total = 0; + + for my $elem (@{$arr}) { + $total += $elem; + } + + return $total; +} + +sub usage { +print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <m> <n> + + <m> an integer to partition + <n> number of elements in partition +-USAGE- + exit(0); +} + +if (scalar @ARGV != 2) { + usage(); +} + +my ($m, $n) = @ARGV; + +my @primes = grep { isPrime($_) } 1 .. $m; +my $count = 0; + +for my $combo (combinations(\@primes, $n)) { + if (sum($combo) == $m) { + say join q{, }, @{$combo}; + $count++; + } +} + +unless ($count) { + say "No such prime partition."; +} diff --git a/challenge-172/jaldhar-h-vyas/perl/ch-2.pl b/challenge-172/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..a46fba6b46 --- /dev/null +++ b/challenge-172/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @nums = sort { $a <=> $b } @ARGV; +my $e = scalar @nums; + +say "minimum: ", $nums[0]; +say "lower quartile: ", $e % 2 == 0 ? ($nums[$e / 4 - 1] + $nums[$e / 4]) / 2 : $nums[$e / 4]; +say "median: ", $e % 2 == 0 ? ($nums[$e / 2 - 1] + $nums[$e / 2]) / 2 : $nums[$e / 2]; +say "upper quartile: ", $e % 2 == 0 ? ($nums[$e / 4 * 3 - 1] + $nums[$e / 4 * 3]) / 2 : $nums[$e / 4 * 3]; +say "maximum: ", $nums[-1]; diff --git a/challenge-172/jaldhar-h-vyas/raku/ch-1.raku b/challenge-172/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..a39cba0720 --- /dev/null +++ b/challenge-172/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/raku + +sub MAIN( + Int $m, #= an integer to partition + Int $n #= number of elements in partition +) { + my @primes = (1 .. $m).grep({ .is-prime }); + my $count = 0; + + for @primes.combinations($n) -> @combo { + if ([+] @combo) == $m { + @combo.join(q{, }).say; + $count++; + } + } + + unless $count { + say "No such prime partition."; + } +}
\ No newline at end of file diff --git a/challenge-172/jaldhar-h-vyas/raku/ch-2.raku b/challenge-172/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..10efa1f01e --- /dev/null +++ b/challenge-172/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,14 @@ +#!/usr/bin/raku + +sub MAIN( + *@args #= a series of numbers +) { + my @nums = @args.sort; + my $e = @nums.elems; + + say "minimum: ", @nums.min; + say "lower quartile: ", $e %% 2 ?? (@nums[$e / 4 - 1] + @nums[$e / 4]) / 2 !! @nums[$e / 4]; + say "median: ", $e %% 2 ?? (@nums[$e / 2 - 1] + @nums[$e / 2]) / 2 !! @nums[$e / 2]; + say "upper quartile: ", $e %% 2 ?? (@nums[$e / 4 * 3 - 1] + @nums[$e / 4 * 3]) / 2 !! @nums[$e / 4 * 3]; + say "maximum: ", @nums.max; +}
\ No newline at end of file |
