aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2023-06-11 23:36:20 +0200
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2023-06-11 23:36:20 +0200
commitba324e86935cfb84dcefdd3ab6d4bf9ccfd736c9 (patch)
tree919d452c6b5698078ac297389c49ca5060aa4953
parent401be1861472af6d62bbdeb0fe65f6ced1ca8f31 (diff)
downloadperlweeklychallenge-club-ba324e86935cfb84dcefdd3ab6d4bf9ccfd736c9.tar.gz
perlweeklychallenge-club-ba324e86935cfb84dcefdd3ab6d4bf9ccfd736c9.tar.bz2
perlweeklychallenge-club-ba324e86935cfb84dcefdd3ab6d4bf9ccfd736c9.zip
feat: add solutions for challenge 220 from BarrOff
-rw-r--r--challenge-220/barroff/perl/ch-1.pl28
-rw-r--r--challenge-220/barroff/raku/ch-1.raku24
-rw-r--r--challenge-220/barroff/raku/ch-2.raku33
3 files changed, 85 insertions, 0 deletions
diff --git a/challenge-220/barroff/perl/ch-1.pl b/challenge-220/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..9ce4442af4
--- /dev/null
+++ b/challenge-220/barroff/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use v5.36;
+use strict;
+use warnings;
+
+use List::Util qw( uniq );
+
+sub common_characters (@words) {
+ my %characters;
+ my @letters = map( { uniq split( //, lc($_) ) } @words );
+ map( { $characters{$_}++ } @letters );
+ my @cc = sort grep( { $characters{$_} == @words } keys %characters );
+ return \@cc;
+}
+
+#| Run test cases
+sub MAIN() {
+ use Test2::V0 qw( is plan );
+ plan 2;
+
+ is common_characters( "Perl", "Rust", "Raku" ), ['r'],
+ 'works for ("Perl", "Rust", "Raku")';
+ is common_characters( "love", "live", "leave" ), [ 'e', 'l', 'v' ],
+ 'works for ("love", "live", "leave")';
+}
+
+MAIN();
diff --git a/challenge-220/barroff/raku/ch-1.raku b/challenge-220/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..cbfdf3096d
--- /dev/null
+++ b/challenge-220/barroff/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub common-characters(Str:D @words --> List) {
+ List(sort(keys([∩] map({ $_.lc.comb.Set }, @words))));
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is common-characters(Array[Str:D].new(("Perl", "Rust", "Raku"))),
+ ('r'), 'works for ("Perl", "Rust", "Raku")';
+ is common-characters(Array[Str:D].new(("love", "live", "leave"))),
+ ('e', 'l', 'v'), 'works for ("love", "live", "leave")';
+}
+
+#| Take user provided list like Perl Rust Raku
+multi sub MAIN(*@words where @words.elems ≥ 1) {
+ my Str @str-words = @words;
+ say common-characters(@str-words);
+}
diff --git a/challenge-220/barroff/raku/ch-2.raku b/challenge-220/barroff/raku/ch-2.raku
new file mode 100644
index 0000000000..f73bb8c137
--- /dev/null
+++ b/challenge-220/barroff/raku/ch-2.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub is-perfect-square(Int $number --> Bool) {
+ return sqrt($number).narrow.^name eq 'Int';
+}
+
+sub check-permutation(@perm --> Bool) {
+ return so all(map({ is-perfect-square(@perm[$_] + @perm[$_ + 1] ) }, 0 .. @perm.elems - 2));
+}
+
+sub squareful(Int:D @numbers --> List) {
+ my @perms = unique(permutations(@numbers), with => &[~~]);
+ List( grep( &check-permutation, @perms ) );
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is squareful(Array[Int:D].new(1, 17, 8)), [ (1, 8, 17), (17, 8, 1) ],
+ 'works for (1, 17, 8)';
+ is squareful(Array[Int:D].new(2, 2, 2)), [(2, 2, 2)],
+ 'works for (2, 2, 2)';
+}
+
+#| Take user provided list like 1 17 8
+multi sub MAIN(*@numbers where @numbers.elems ≥ 1) {
+ my Int @int-nums = @numbers;
+ say squareful(@int-nums);
+}