diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-18 04:11:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-18 04:11:42 +0100 |
| commit | 625cd7e5e395f68a1124667b412fcdb15cbbda22 (patch) | |
| tree | 34e10f9220f4310bf295488f18c62ad880cadbfe | |
| parent | 894009b4ceedd87af9a1f5b93909a3a36a3ae496 (diff) | |
| parent | b34cea10253d96d8c9cea2b8d240c68845b8bb1d (diff) | |
| download | perlweeklychallenge-club-625cd7e5e395f68a1124667b412fcdb15cbbda22.tar.gz perlweeklychallenge-club-625cd7e5e395f68a1124667b412fcdb15cbbda22.tar.bz2 perlweeklychallenge-club-625cd7e5e395f68a1124667b412fcdb15cbbda22.zip | |
Merge pull request #5956 from jaldhar/challenge-160
Challenge 160 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-160/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-160/jaldhar-h-vyas/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-160/jaldhar-h-vyas/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-160/jaldhar-h-vyas/raku/ch-1.raku | 29 | ||||
| -rwxr-xr-x | challenge-160/jaldhar-h-vyas/raku/ch-2.raku | 14 |
5 files changed, 104 insertions, 0 deletions
diff --git a/challenge-160/jaldhar-h-vyas/blog.txt b/challenge-160/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..829340e89b --- /dev/null +++ b/challenge-160/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2022/04/perl_weekly_challenge_week_160.html
\ No newline at end of file diff --git a/challenge-160/jaldhar-h-vyas/perl/ch-1.pl b/challenge-160/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..07b22ef1c9 --- /dev/null +++ b/challenge-160/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my $n = shift // die "Need an integer from 1-9\n"; +if ($n < 1 || $n > 9) { + die "Need an integer from 1-9\n"; +} + +my %words = ( + 1 => 'one', + 2 => 'two', + 3 => 'three', + 4 => 'four', + 5 => 'five', + 6 => 'six', + 7 => 'seven', + 8 => 'eight', + 9 => 'nine', +); + +my $first = $words{$n}; +while(1) { + if ($first eq 'four') { + say "four is magic."; + last; + } else { + my $second = $words{length $first}; + print "$first is $second, "; + $first = $second; + } +} diff --git a/challenge-160/jaldhar-h-vyas/perl/ch-2.pl b/challenge-160/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..354b962b06 --- /dev/null +++ b/challenge-160/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub sum { + my ($arr) = @_; + my $total = 0; + + for my $elem (@{$arr}) { + $total += $elem; + } + + return $total; +} + +my @n = @ARGV; +if (!scalar @n) { + die "need an array of integers\n"; +} + +for my $i (0 .. (scalar @n - 1)) { + if (sum([@n[0 .. $i - 1]]) == sum([@n[$i + 1 .. scalar @n - 1]])) { + say $i; + exit; + } +} + +say -1; diff --git a/challenge-160/jaldhar-h-vyas/raku/ch-1.raku b/challenge-160/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..f9a8d2de04 --- /dev/null +++ b/challenge-160/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/raku + +sub MAIN( + Int $n where { $n > 0 && $n < 10 } #= an integer from 1-9 +) { + my %words = ( + 1 => 'one', + 2 => 'two', + 3 => 'three', + 4 => 'four', + 5 => 'five', + 6 => 'six', + 7 => 'seven', + 8 => 'eight', + 9 => 'nine', + ); + + my $first = %words{$n}; + loop { + if $first eq 'four' { + say "four is magic."; + last; + } else { + my $second = %words{$first.chars}; + print "$first is $second, "; + $first = $second; + } + } +}
\ No newline at end of file diff --git a/challenge-160/jaldhar-h-vyas/raku/ch-2.raku b/challenge-160/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..77f1902772 --- /dev/null +++ b/challenge-160/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,14 @@ +#!/usr/bin/raku + +sub MAIN( + *@n +) { + for 0 ..^ @n.elems -> $i { + if ([+] @n[0 ..^ $i]) == ([+] @n[$i ^..^ @n.elems]) { + say $i; + exit; + } + } + + say -1; +}
\ No newline at end of file |
