diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-05-07 22:39:55 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-05-07 22:39:55 +0200 |
| commit | acfe905a2a468ebf5d406b65c10657ec3164c3d7 (patch) | |
| tree | b84be34137f22fe610df80d8142fb79dc175594a | |
| parent | fca710e33f9f55af0a163c95ad060a4d1c6e7be5 (diff) | |
| download | perlweeklychallenge-club-acfe905a2a468ebf5d406b65c10657ec3164c3d7.tar.gz perlweeklychallenge-club-acfe905a2a468ebf5d406b65c10657ec3164c3d7.tar.bz2 perlweeklychallenge-club-acfe905a2a468ebf5d406b65c10657ec3164c3d7.zip | |
feat: add solutions for challenge 215 from BarrOff
| -rw-r--r-- | challenge-215/barroff/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-215/barroff/perl/ch-2.pl | 32 | ||||
| -rw-r--r-- | challenge-215/barroff/raku/ch-1.raku | 28 | ||||
| -rw-r--r-- | challenge-215/barroff/raku/ch-2.raku | 30 |
4 files changed, 116 insertions, 0 deletions
diff --git a/challenge-215/barroff/perl/ch-1.pl b/challenge-215/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..b0629a8899 --- /dev/null +++ b/challenge-215/barroff/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use v5.36; +use strict; +use warnings; + +sub is_alphabetic ($s) { + return $s eq join( '', sort( split( //, $s ) ) ); +} + +sub odd_one_out (@words) { + my @sorted_words = grep is_alphabetic($_), @words; + return $#words - $#sorted_words; +} + +#| Run test cases +sub MAIN() { + use Test2::V0 qw(is plan); + plan 3; + + is odd_one_out( 'abc', 'xyz', 'tsu' ), 1, "works for ('abc', 'xyz', 'tsu')"; + is odd_one_out( 'rat', 'cab', 'dad' ), 3, "works for ('rat', 'cab', 'dad')"; + is odd_one_out( 'x', 'y', 'z' ), 0, "works for ('x', 'y', 'z')"; +} + +MAIN(); diff --git a/challenge-215/barroff/perl/ch-2.pl b/challenge-215/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..9a4d15cc9e --- /dev/null +++ b/challenge-215/barroff/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use v5.36; +use strict; +use warnings; + +sub count_places ($zeros) { + return int( ( length($zeros) - 1 ) / 2 ); +} + +sub number_placement ( $count, @numbers ) { + my $joined_numbers = join '', @numbers; + my @captures = $joined_numbers =~ m/ (0+) /gx; + return List::Util::sum( map( &count_places($_), @captures ) ) >= $count + ? 1 + : 0; +} + +#| Run test cases +sub MAIN() { + use Test2::V0 qw( is plan ); + plan 4; + + is number_placement( 1, ( 1, 0, 0, 0, 1 ) ), 1, 'works for 1, (1,0,0,0,1)'; + is number_placement( 2, ( 1, 0, 0, 0, 1 ) ), 0, 'works for 2, (1,0,0,0,1)'; + is number_placement( 3, ( 1, 0, 0, 0, 0, 0, 0, 0, 1 ) ), 1, + 'works for 3, (1,0,0,0,0,0,0,0,1)'; + is number_placement( 4, ( 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 ) ), 1, + 'works for 4, (1,0,0,0,0,0,0,0,1,0,0,0,1)'; +} + +MAIN(); diff --git a/challenge-215/barroff/raku/ch-1.raku b/challenge-215/barroff/raku/ch-1.raku new file mode 100644 index 0000000000..74c7e63898 --- /dev/null +++ b/challenge-215/barroff/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub is-alphabetic(Str $s --> Bool) { + $s eq $s.comb.sort.join; +} + +sub odd-one-out(Str @words where @words[0].chars == (map(&chars, @words)).all --> Int) { + my @sorted-words = @words.grep: &is-alphabetic; + return @words.elems - @sorted-words.elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is odd-one-out(Array[Str].new('abc', 'xyz', 'tsu')), 1, "works for ('abc', 'xyz', 'tsu')"; + is odd-one-out(Array[Str].new('rat', 'cab', 'dad')), 3, "works for ('rat', 'cab', 'dad')"; + is odd-one-out(Array[Str].new('x', 'y', 'z')), 0, "works for ('x', 'y', 'z')"; +} + +#| Take user provided list like x y z +multi sub MAIN(*@words where @words.elems ≥ 1 && @words[0].chars == (map(&chars, @words)).all) { + my Str @strings = @words; + say odd-one-out(@strings); +} diff --git a/challenge-215/barroff/raku/ch-2.raku b/challenge-215/barroff/raku/ch-2.raku new file mode 100644 index 0000000000..bc3c130a59 --- /dev/null +++ b/challenge-215/barroff/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +use v6.d; + +sub count-places(Match $zeros --> Int) { + return ( $zeros.chars - 1 ) ÷ 2; +} + +sub number-placement(Int @numbers where 2 > @numbers.all, UInt $count --> UInt) { + my Str $joined-numbers = @numbers.join; + $joined-numbers ~~ m:g/ (0+) /; + return sum(map( &count-places, $/.list)) ≥ $count ?? 1 !! 0; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 4; + + is number-placement(Array[Int].new(1,0,0,0,1), 1), 1, 'works for (1,0,0,0,1), 1'; + is number-placement(Array[Int].new(1,0,0,0,1), 2), 0, 'works for (1,0,0,0,1), 2'; + is number-placement(Array[Int].new(1,0,0,0,0,0,0,0,1), 3), 1, 'works for (1,0,0,0,0,0,0,0,1), 3'; + is number-placement(Array[Int].new(1,0,0,0,0,0,0,0,1,0,0,0,1), 4), 1, 'works for (1,0,0,0,0,0,0,0,1,0,0,0,1), 4'; +} + +#| Take user provided list like x y z +multi sub MAIN(UInt $count, *@numbers where 2 > @numbers.all --> UInt) { + my Int @int-numbers = @numbers; + say number-placement(@int-numbers, $count); +} |
