From 58d6182e0fa2f922a19fc040ab7f8d4babe0f5f5 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Fri, 28 May 2021 09:52:03 -0500 Subject: Challenge 114 - Raku --- challenge-114/aaronreidsmith/blog.txt | 1 + challenge-114/aaronreidsmith/raku/ch-1.raku | 24 +++++++++++++++++++++++ challenge-114/aaronreidsmith/raku/ch-2.raku | 30 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 challenge-114/aaronreidsmith/blog.txt create mode 100644 challenge-114/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-114/aaronreidsmith/raku/ch-2.raku diff --git a/challenge-114/aaronreidsmith/blog.txt b/challenge-114/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..8bb2dc2414 --- /dev/null +++ b/challenge-114/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-114/ diff --git a/challenge-114/aaronreidsmith/raku/ch-1.raku b/challenge-114/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..5e67985a5f --- /dev/null +++ b/challenge-114/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +sub challenge(Int $N) returns Int { + ($N^..Inf).first(-> $num { $num == $num.flip }, :v); +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1234, 1331), + (999, 1001) + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + done-testing; +} diff --git a/challenge-114/aaronreidsmith/raku/ch-2.raku b/challenge-114/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..20d191d790 --- /dev/null +++ b/challenge-114/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +sub bits(Int $base-ten) returns Int { + $base-ten.base(2).comb.grep(* eq '1').elems; +} + +sub challenge(Int $N where $N > 0) returns Int { + my $bits = bits($N); + ($N^..Inf).first(-> $num { bits($num) == $bits }, :v); +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1, 2), + (3, 5), + (12, 17) + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + done-testing; +} -- cgit