From 7ff5675fc0fca0e29f86bba3f6a788d15db7efc1 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Sat, 8 May 2021 15:06:46 -0500 Subject: Challenge 111 - Raku --- challenge-111/aaronreidsmith/blog.txt | 1 + challenge-111/aaronreidsmith/raku/ch-1.raku | 47 +++++++++++++++++++++++++++++ challenge-111/aaronreidsmith/raku/ch-2.raku | 40 ++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 challenge-111/aaronreidsmith/blog.txt create mode 100644 challenge-111/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-111/aaronreidsmith/raku/ch-2.raku diff --git a/challenge-111/aaronreidsmith/blog.txt b/challenge-111/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..ac97767574 --- /dev/null +++ b/challenge-111/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-111/ diff --git a/challenge-111/aaronreidsmith/raku/ch-1.raku b/challenge-111/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..d6344858c7 --- /dev/null +++ b/challenge-111/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku + +sub challenge(@list-of-lists, Int $N) returns Int { + my $output = 0; + for (0..^@list-of-lists.end) Z (0^..@list-of-lists.end) -> ($i, $j) { + if @list-of-lists[$i].head == $N || @list-of-lists[$j].head == $N { + $output = 1; + last; + } elsif @list-of-lists[$i].head < $N < @list-of-lists[$j].head { + $output = (any(@list-of-lists[$i][1..*]) ~~ $N).Int; + last; + } elsif $j == @list-of-lists.end && $N > @list-of-lists[$j].head { + $output = (any(@list-of-lists[$j][1..*]) ~~ $N).Int; + last; + } + } + $output; +} + +multi sub MAIN(Int $N) { + my @list-of-lists = ( + ( 1, 2, 3, 5, 7), + ( 9, 11, 15, 19, 20), + (23, 24, 25, 29, 31), + (32, 33, 39, 40, 42), + (45, 47, 48, 49, 50) + ); + say challenge(@list-of-lists, $N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @list-of-lists = ( + ( 1, 2, 3, 5, 7), + ( 9, 11, 15, 19, 20), + (23, 24, 25, 29, 31), + (32, 33, 39, 40, 42), + (45, 47, 48, 49, 50) + ); + + is(challenge(@list-of-lists, 35), 0); + is(challenge(@list-of-lists, 39), 1); + is(challenge(@list-of-lists, 47), 1); + + done-testing; +} diff --git a/challenge-111/aaronreidsmith/raku/ch-2.raku b/challenge-111/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..7b07b9ea31 --- /dev/null +++ b/challenge-111/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +use LibCurl::HTTP :subs; # Imports jget + +sub get-english-words { + my %words = jget('https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json'); + %words.keys; +} + +sub challenge(Int $threshold) { + my @words = get-english-words; + my @matching-words = gather for @words.race -> $word { + my @chars = $word.comb; + if @chars.elems >= $threshold && @chars.sort.join eq $word { + take $word; + } + } + @matching-words.sort; +} + +multi sub MAIN(Int $threshold = 7) { + for challenge($threshold) -> $word { + say $word; + } +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (7, ('adelops', 'aegilops', 'alloquy', 'beefily', 'begorry', 'belloot', 'billowy', 'deglory', 'egilops')), + (8, ('aegilops',)) + ); + + for @tests -> ($threshold, @expected) { + is(challenge($threshold), @expected); + } + + done-testing; +} -- cgit