diff options
| author | Aaron Smith <asmith@sumologic.com> | 2021-03-21 10:43:25 -0500 |
|---|---|---|
| committer | Aaron Smith <asmith@sumologic.com> | 2021-03-21 10:43:25 -0500 |
| commit | b7a08d6c3b286bf5f2c6d70f57926b88ad4e428a (patch) | |
| tree | 8ff2043fe7c0aea5a68913eb5fa5792aa499eeed /challenge-104 | |
| parent | 65d54d52500028ec5359a7d39619803ade281543 (diff) | |
| download | perlweeklychallenge-club-b7a08d6c3b286bf5f2c6d70f57926b88ad4e428a.tar.gz perlweeklychallenge-club-b7a08d6c3b286bf5f2c6d70f57926b88ad4e428a.tar.bz2 perlweeklychallenge-club-b7a08d6c3b286bf5f2c6d70f57926b88ad4e428a.zip | |
Challenge 104 - Raku
Diffstat (limited to 'challenge-104')
| -rw-r--r-- | challenge-104/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-104/aaronreidsmith/raku/ch-1.raku | 33 | ||||
| -rw-r--r-- | challenge-104/aaronreidsmith/raku/ch-2.raku | 49 |
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-104/aaronreidsmith/blog.txt b/challenge-104/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..36e3e68db1 --- /dev/null +++ b/challenge-104/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-104/ diff --git a/challenge-104/aaronreidsmith/raku/ch-1.raku b/challenge-104/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..c6f02d4387 --- /dev/null +++ b/challenge-104/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +use experimental :cached; + +sub fusc(Int(Rat) $n) is cached returns Int { + given $n { + when 0 { 0 } + when 1 { 1 } + when * %% 2 { fusc($n / 2) } + default { fusc(($n - 1) / 2) + fusc(($n + 1) / 2) } + } +} + +multi sub MAIN(Int $terms = 50) { + say (^$terms).map(&fusc); +} + +multi sub MAIN(Bool :$test) { + use Test; + + # Picked random terms in the sequence + my @tests = ( + (7, 3), + (28, 3), + (50, 7) + ); + + for @tests -> ($n, $expected) { + is(fusc($n), $expected); + } + + done-testing; +} diff --git a/challenge-104/aaronreidsmith/raku/ch-2.raku b/challenge-104/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..a722153ca3 --- /dev/null +++ b/challenge-104/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,49 @@ +#!/usr/bin/env raku + +# Formats a message defined as plural to be singular if $n == 1 +sub format(Str $message, Int $n) returns Str { + $n == 1 ?? $message.trans(['are', 'tokens'] => ['is', 'token']) !! $message; +} + +sub challenge(Int $n) { + my $remaining = $n; + + # Defined within the challenge sub because it references $remaining + sub default-prompt returns Any { + prompt(format("There are $remaining tokens. How many would you like to pick up? (1, 2, 3) ", $remaining)); + } + + my $input = default-prompt; + my $most-recent-move; + while $remaining > 0 { + given $input { + when 1|2|3 { + if $input > $remaining { + $input = prompt("There are only $remaining tokens left. Please enter a valid number ") + } else { + say format("You take $input tokens", $input); + $remaining -= $input; + $most-recent-move = 'You'; + + last if $remaining == 0; + + # If there are only 3 or less tokens, take all of them. Otherwise, take a random number between 1 and 3 + my $bot-move = $remaining ~~ 1|2|3 ?? $remaining !! (1..3).pick; + say format("The computer takes $bot-move tokens", $bot-move); + $remaining -= $bot-move; + $most-recent-move = 'Computer'; + + last if $remaining == 0; + + $input = default-prompt; + } + } + default { $input = prompt('Please enter 1, 2, or 3 ') } + } + } + say $most-recent-move eq 'Computer' ?? 'The computer wins!' !! 'You win!'; +} + +sub MAIN(Int $n where $n > 0 = 12) { + challenge($n); +} |
