diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-29 15:39:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-29 15:39:59 +0100 |
| commit | 06f9873504374d655e2f4be53c4a584fb6bf5208 (patch) | |
| tree | d6dbd3d6de8da730cdd55b71d8fcda18ad8017d1 /challenge-127 | |
| parent | 7bf36bcccc1fc15b40557109b1cca94ccab332ff (diff) | |
| parent | 39c784f7c4ea4cb554929a722857121f12fd12b3 (diff) | |
| download | perlweeklychallenge-club-06f9873504374d655e2f4be53c4a584fb6bf5208.tar.gz perlweeklychallenge-club-06f9873504374d655e2f4be53c4a584fb6bf5208.tar.bz2 perlweeklychallenge-club-06f9873504374d655e2f4be53c4a584fb6bf5208.zip | |
Merge pull request #4800 from mimosinnet/branch-for-challenge-127
Solutions for challenge 127
Diffstat (limited to 'challenge-127')
| -rw-r--r-- | challenge-127/mimosinnet/raku/ch-1.raku | 36 | ||||
| -rw-r--r-- | challenge-127/mimosinnet/raku/ch-2.raku | 48 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-127/mimosinnet/raku/ch-1.raku b/challenge-127/mimosinnet/raku/ch-1.raku new file mode 100644 index 0000000000..a4227c42c9 --- /dev/null +++ b/challenge-127/mimosinnet/raku/ch-1.raku @@ -0,0 +1,36 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-127 + +# Simplified the expresion using Ash solution +sub challenge( @s1, @s2 ) { + return +!(@s1 ∩ @s2 ); +} + +multi sub MAIN( @s1, @s2 ) { + say 'Input: @S1 = ',@s1; + say ' @S2 = ',@s2; + my $result = challenge( @s1, @s2); + print 'Output: ',challenge( @s1, @s2),' as the given two sets '; + ($result ?? 'do not have common member.' !! 'have at least a common member.' ).say; +} + +multi sub MAIN( 'challenge' ) { + MAIN( (1, 2, 5, 3, 4), (4, 6, 7, 8, 9) ); + MAIN( (1, 3, 5, 7, 9), (0, 2, 4, 6, 8) ); +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @test = ( + (1, 2, 5, 3, 4), (4, 6, 7, 8, 9), 0, + (1, 3, 5, 7, 9), (0, 2, 4, 6, 8), 1, + (1, 2, 5, 3, 4, 6), (4, 6, 7, 8, 9), 0 + ); + + for @test -> $a, $b, $c { + is challenge($a,$b), $c; + } + + done-testing; + +} diff --git a/challenge-127/mimosinnet/raku/ch-2.raku b/challenge-127/mimosinnet/raku/ch-2.raku new file mode 100644 index 0000000000..8be6ca748f --- /dev/null +++ b/challenge-127/mimosinnet/raku/ch-2.raku @@ -0,0 +1,48 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-127 + +#| Get the last interval and check conflict with previous intervals +sub challenge( @intervals ) { + + my @result; + + while @intervals { + last if @intervals.elems == 1; # skip when no previous intervals + my @last = @intervals.pop.flat; # get last interval @last(a,b) + for @intervals -> @item { # check for conflict with previos intervals @item(x,y) + next if @last[0] > @item[1]; # next if a > y, no conflict + next if @last[1] < @item[0]; # next if b < x, no conflict + @result.push: @last; # push @last as it conflicts with previous intervals + } + } + return @result.unique.sort; # get unique results +} + +multi sub MAIN( @intervals ) { + say 'Input: @Intervals = ',@intervals; + say 'Output: ',challenge( @intervals ),"\n"; +} + +multi sub MAIN( 'challenge' ) { + + MAIN( [ (1,4), (3,5), (6,8), (12, 13), ( 3,20) ] ); + MAIN( [ (3,4), (5,7), (6,9), (10, 12), (13,15) ] ); + +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @test = ( + [ (1,4), (3,5), (6,8), (12,13), ( 3,20) ], [ (3,5), ( 3,20) ], + [ (3,4), (5,7), (6,9), (10,12), (13,15) ], [ (6,9) ], + [ (3,4), (5,7), (6,9), (10,14), (13,15) ], [ (6,9), (13,15) ], + [ (3,4), (8,9), (5,7), (12,13), ( 6, 7) ], [ (6,7) ] + ); + + for @test -> $a, $b { + is challenge($a), $b; + } + + done-testing; + +} |
