diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-05-07 17:53:29 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-05-07 17:53:29 +0200 |
| commit | ae9e38cfc18bed452a3879e5f772cbaba6f4b0c8 (patch) | |
| tree | d34df8f0eff9acff6d6ef36a7bac4711d13c1222 /challenge-163 | |
| parent | 961737c54500d9ff4dd41e1478e3b370863be67c (diff) | |
| download | perlweeklychallenge-club-ae9e38cfc18bed452a3879e5f772cbaba6f4b0c8.tar.gz perlweeklychallenge-club-ae9e38cfc18bed452a3879e5f772cbaba6f4b0c8.tar.bz2 perlweeklychallenge-club-ae9e38cfc18bed452a3879e5f772cbaba6f4b0c8.zip | |
feat(challenge-163/lubos-kolouch/perl/ch-{1,2}.pl): Challenge 163 LK Task 1 2
Diffstat (limited to 'challenge-163')
| -rw-r--r-- | challenge-163/lubos-kolouch/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-163/lubos-kolouch/perl/ch-2.pl | 30 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-163/lubos-kolouch/perl/ch-1.pl b/challenge-163/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..a05f05951f --- /dev/null +++ b/challenge-163/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,30 @@ +package main; +use strict; +use warnings; +use List::Util qw/uniq/; +use Algorithm::Combinatorics qw /combinations/; + +sub get_bitwise_sum { + my @what = @_; + + # it says we should consider only unique pairs + @what = uniq @what; + + my $sum = 0; + my $iter = combinations( \@what, 2 ); + + while ( my $c = $iter->next ) { + $sum += $c->[0] & $c->[1]; + } + + return $sum; +} + +use Test::More; + +is( get_bitwise_sum( 1, 2, 3 ), 3 ); +is( get_bitwise_sum( 1, 1, 2, 3 ), 3 ); +is( get_bitwise_sum( 2, 3, 4 ), 2 ); + +done_testing; +1; diff --git a/challenge-163/lubos-kolouch/perl/ch-2.pl b/challenge-163/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2e5ecc2580 --- /dev/null +++ b/challenge-163/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,30 @@ +package main; + +use strict; +use warnings; + +sub get_summations { + my @what = @_; + + while ( scalar @what > 1 ) { + + for my $item ( 2 .. scalar @what - 1 ) { + $what[$item] += $what[ $item - 1 ]; + } + + @what = @what[ 1 .. scalar @what - 1 ]; + } + + return $what[-1]; +} + +use Test::More; + +is( get_summations( 1, 2, 3, 4, 5 ), 42, 'Test 1 2 3 4 5' ); +is( get_summations( 1, 3, 5, 7, 9 ), 70, 'Test 1 3 5 7 9' ); +is( get_summations(1), 1, 'Test 1' ); +is( get_summations( 1, 2 ), 2, 'Test 1 2' ); + +done_testing; + +1; |
