diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-01-14 22:06:46 +0100 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-01-14 22:06:46 +0100 |
| commit | c963984b1a3a82d18fd35878d96f4ff8092e8074 (patch) | |
| tree | b2ce420063fdb6c426c28bf67eb2bafa8fb328a8 | |
| parent | dba8c691b150fd0086ac6f95e674d0302f437cac (diff) | |
| download | perlweeklychallenge-club-c963984b1a3a82d18fd35878d96f4ff8092e8074.tar.gz perlweeklychallenge-club-c963984b1a3a82d18fd35878d96f4ff8092e8074.tar.bz2 perlweeklychallenge-club-c963984b1a3a82d18fd35878d96f4ff8092e8074.zip | |
feat: add solutions for challenge 251 from BarrOff
| -rw-r--r-- | challenge-251/barroff/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-251/barroff/raku/ch-1.p6 | 28 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-251/barroff/perl/ch-1.pl b/challenge-251/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..bc8dfc5789 --- /dev/null +++ b/challenge-251/barroff/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw/sum/; + +sub concatenation_value (@ints) { + if ( @ints % 2 == 0 ) { + return sum( + map( { $ints[$_] . $ints[ -$_ - 1 ] } 0 .. @ints / 2 - 1 ) ); + } + else { + my $mid = int( @ints / 2 ); + return sum( map( { $ints[$_] . $ints[ -$_ - 1 ] } 0 .. $mid - 1 ) ) + + $ints[$mid]; + } +} + +sub MAIN() { + if (@ARGV) { + + #| Run on command line argument + say concatenation_value(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + is concatenation_value( ( 6, 12, 25, 1 ) ), 1286, + 'works for (6, 12, 25, 1)'; + is concatenation_value( ( 10, 7, 31, 5, 2, 2 ) ), 489, + 'works for (10, 7, 31, 5, 2, 2)'; + is concatenation_value( ( 1, 2, 10 ) ), 112, 'works for (1, 2, 10)'; + } +} + +MAIN(); diff --git a/challenge-251/barroff/raku/ch-1.p6 b/challenge-251/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..82e72433d2 --- /dev/null +++ b/challenge-251/barroff/raku/ch-1.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub concatenation-value(@ints --> Int:D) { + if @ints.elems % 2 == 0 { + return sum(map({ @ints[$_] ~ @ints[* - $_ - 1]}, 0..@ints.elems / 2 - 1)); + } else { + my Int $mid = floor(@ints.elems รท 2); + return sum(map({ @ints[$_] ~ @ints[* - $_ - 1] }, 0..$mid - 1)) + + @ints[$mid]; + } +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is concatenation-value((6, 12, 25, 1)), 1286, 'works for (6, 12, 25, 1)'; + is concatenation-value((10, 7, 31, 5, 2, 2)), 489, 'works for (10, 7, 31, 5, 2, 2)'; + is concatenation-value((1, 2, 10)), 112, 'works for (1, 2, 10)'; +} + +#| Take user provided list like 1 2 10 +multi sub MAIN(*@s) { + say concatenation-value(@s); +} |
