diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-26 23:11:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-26 23:11:02 +0000 |
| commit | 164cb2c66176378c79e036b63be79867afbb29a6 (patch) | |
| tree | 4ca2994cf42aad49091163bad07cd1b247f06395 | |
| parent | e67f938553ce59fdaad58ae8d08855fa3ce1b55a (diff) | |
| parent | abfa55d38586a8c5b2a9e5a7358bd55052c2bb5a (diff) | |
| download | perlweeklychallenge-club-164cb2c66176378c79e036b63be79867afbb29a6.tar.gz perlweeklychallenge-club-164cb2c66176378c79e036b63be79867afbb29a6.tar.bz2 perlweeklychallenge-club-164cb2c66176378c79e036b63be79867afbb29a6.zip | |
Merge pull request #7639 from BarrOff/barroff-205
feat: add solutions for challenge 205 from BarrOff
| -rw-r--r-- | challenge-205/barroff/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-205/barroff/raku/ch-1.raku | 29 | ||||
| -rw-r--r-- | challenge-205/barroff/raku/ch-2.raku | 25 |
3 files changed, 81 insertions, 0 deletions
diff --git a/challenge-205/barroff/perl/ch-1.pl b/challenge-205/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..bc016eaa30 --- /dev/null +++ b/challenge-205/barroff/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use v5.36; +use strict; +use warnings; + +use List::MoreUtils qw(uniq); + +sub third_highest_sorted (@list) { + scalar @list >= 3 ? $list[-3] : $list[-1]; +} + +sub third_highest (@list) { + my @sorted = sort( uniq(@list) ); + third_highest_sorted @sorted; +} + +sub MAIN() { + use Test::Simple tests => 3; + use Test::More import => [qw( is plan )]; + + is third_highest( 5, 3, 4 ), 3, 'works for (5, 3, 4)'; + is third_highest( 5, 6 ), 6, 'works for (5, 6)'; + is third_highest( 5, 4, 4, 3 ), 3, 'works for (5, 4, 4, 3)'; +} + +MAIN(); diff --git a/challenge-205/barroff/raku/ch-1.raku b/challenge-205/barroff/raku/ch-1.raku new file mode 100644 index 0000000000..7e34636e8c --- /dev/null +++ b/challenge-205/barroff/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +use v6.d; + +sub third-highest(Int @list where @list.elems > 1 --> Int) { + my Int @sorted = @list.unique.sort; + @sorted.elems ≥ 3 ?? @sorted[* - 3] !! @sorted[* - 1]; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is third-highest(Array[Int].new([5, 3, 4])), 3, 'works for (5, 3, 4)'; + is third-highest(Array[Int].new([5, 6])), 6, 'works for (5, 6)'; + is third-highest(Array[Int].new([5, 4, 4, 3])), 3, 'works for (5, 4, 4, 3)'; +} + +#| Take user provided list like 1 2 2 3 +multi sub MAIN(*@elements where @elements.elems ≥ 1 && all(@elements) ~~ /^<[+-]>?<[0..9]>+$/) { + my Int @int_elements = @elements; + say third-highest(@int_elements); +} + +#| Run test cases if no argument is supplied +multi sub MAIN() { + MAIN('test'); +} diff --git a/challenge-205/barroff/raku/ch-2.raku b/challenge-205/barroff/raku/ch-2.raku new file mode 100644 index 0000000000..dce657b874 --- /dev/null +++ b/challenge-205/barroff/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub maximum-xor(Int @list where @list.elems > 1 --> Int) { + my Int @unique-list = @list.unique; + my @pairs = map({ $_[0] +^ $_[1] }, @unique-list.combinations(2)); + return max(@pairs); +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is maximum-xor(Array[Int].new([1 , 2, 3, 4, 5, 6, 7])), 7, 'works for (1, 2, 3, 4, 5, 6, 7)'; + is maximum-xor(Array[Int].new([2 , 4, 1, 3])), 7, 'works for (2, 4, 1, 3)'; + is maximum-xor(Array[Int].new([10 , 5, 7, 12, 8])), 15, 'works for (10, 5, 7, 12, 8)'; +} + +#| Take user provided list like 1 2 2 3 +multi sub MAIN(*@elements where @elements.elems ≥ 1 && all(@elements) ~~ /^<[+-]>?<[0..9]>+$/) { + my Int @int_elements = @elements; + say maximum-xor(@int_elements); +} |
