aboutsummaryrefslogtreecommitdiff
path: root/challenge-104
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-22 00:57:22 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-22 00:57:22 +0000
commit5cefa4cf260e515aa6994f0d083319b05cbc3446 (patch)
tree90fc902e84b81ea66d07c9660870dbdf5b8b9180 /challenge-104
parent816a93d813adbad0616374a025065e09996c426f (diff)
parent4bb887378718b1281f0d675ce5816f74f1e2672a (diff)
downloadperlweeklychallenge-club-5cefa4cf260e515aa6994f0d083319b05cbc3446.tar.gz
perlweeklychallenge-club-5cefa4cf260e515aa6994f0d083319b05cbc3446.tar.bz2
perlweeklychallenge-club-5cefa4cf260e515aa6994f0d083319b05cbc3446.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-104')
-rw-r--r--challenge-104/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-104/jaldhar-h-vyas/perl/ch-1.pl22
-rwxr-xr-xchallenge-104/jaldhar-h-vyas/perl/ch-2.pl39
-rwxr-xr-xchallenge-104/jaldhar-h-vyas/raku/ch-1.raku34
-rwxr-xr-xchallenge-104/jaldhar-h-vyas/raku/ch-2.raku38
5 files changed, 134 insertions, 0 deletions
diff --git a/challenge-104/jaldhar-h-vyas/blog.txt b/challenge-104/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..ca9f93aea2
--- /dev/null
+++ b/challenge-104/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/03/perl_weekly_challenge_week_104.html
diff --git a/challenge-104/jaldhar-h-vyas/perl/ch-1.pl b/challenge-104/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..1bb9215160
--- /dev/null
+++ b/challenge-104/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub fusc {
+ my ($n) = @_;
+
+ if ($n < 2) {
+ return $n;
+ }
+
+ if ($n % 2 == 0) {
+ return fusc($n / 2);
+ } else {
+ return fusc(($n - 1) / 2) + fusc(($n + 1) / 2);
+ }
+}
+
+for my $n (0 .. 49) {
+ print fusc($n), ' ';
+}
+print "\n";
diff --git a/challenge-104/jaldhar-h-vyas/perl/ch-2.pl b/challenge-104/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..64a1c4857d
--- /dev/null
+++ b/challenge-104/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub plural {
+ my ($word, $count) = @_;
+ return $word . ($count == 1 ? q{} : 's');
+}
+
+my $tokens = 12;
+
+while ($tokens) {
+ say "There are $tokens ", plural("token", $tokens), ".";
+ my $playerChoice = 0;
+
+ while (1) {
+ print "How many tokens will you pick [1, 2 or 3]?";
+ my $answer = <>;
+ if ($answer =~ /\A \s* (1|2|3) \s* \z/msx) {
+ $playerChoice = $1;
+ last;
+ }
+ }
+
+ $tokens -= $playerChoice;
+ if ($tokens < 1) {
+ say "You win!";
+ last;
+ }
+
+ my $computerChoice = 4 - $playerChoice;
+ say "The computer picks $computerChoice ", plural("token", $computerChoice);
+ $tokens -= $computerChoice;
+
+ if ($tokens < 1) {
+ say "The computer wins.";
+ last;
+ }
+}
diff --git a/challenge-104/jaldhar-h-vyas/raku/ch-1.raku b/challenge-104/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..ce26e40bf8
--- /dev/null
+++ b/challenge-104/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/raku
+
+multi sub fusc(
+ Int $n where { $n == 0 }
+) {
+ return 0;
+}
+
+multi sub fusc(
+ Int $n where { $n == 1 }
+) {
+ return 1;
+}
+
+multi sub fusc(
+ Int $n where { ($n > 1) && ($n % 2 == 0); }
+) {
+ return fusc($n div 2);
+}
+
+multi sub fusc(
+ Int $n where { ($n > 1) && ($n % 2 == 1); }
+) {
+ return fusc(($n - 1) div 2) + fusc(($n + 1) div 2);
+}
+
+sub MAIN () {
+ for ^50 -> $n {
+ print fusc($n), ' ';
+ }
+
+ print "\n";
+}
+
diff --git a/challenge-104/jaldhar-h-vyas/raku/ch-2.raku b/challenge-104/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..f698e2a84d
--- /dev/null
+++ b/challenge-104/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/raku
+
+sub plural(Str $word, Int $count) {
+ return $word ~ ($count == 1 ?? q{} !! 's');
+}
+
+sub MAIN () {
+ my $tokens = 12;
+
+ while ($tokens) {
+ say "There are $tokens ", plural("token", $tokens), ".";
+ my $playerChoice = 0;
+
+ loop {
+ my $answer = prompt("How many tokens will you pick [1, 2 or 3]?");
+ if $answer ~~ /^ \s* (1|2|3) \s* $/ {
+ $playerChoice = $0;
+ last;
+ }
+ }
+
+ $tokens -= $playerChoice;
+ if $tokens < 1 {
+ say "You win!";
+ last;
+ }
+
+ my $computerChoice = 4 - $playerChoice;
+ say "The computer picks $computerChoice ",
+ plural("token", $computerChoice);
+ $tokens -= $computerChoice;
+
+ if ($tokens < 1) {
+ say "The computer wins.";
+ last;
+ }
+ }
+} \ No newline at end of file