diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-03-18 13:31:17 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-03-18 13:31:17 +0100 |
| commit | b63339d7d4f13572bf0087b32a07f5760aa132af (patch) | |
| tree | d909d899b78b05ba5401977ccf295e9acd665b90 | |
| parent | cad939ed2335566e31b26c5b74b9fc99c23db7a5 (diff) | |
| download | perlweeklychallenge-club-b63339d7d4f13572bf0087b32a07f5760aa132af.tar.gz perlweeklychallenge-club-b63339d7d4f13572bf0087b32a07f5760aa132af.tar.bz2 perlweeklychallenge-club-b63339d7d4f13572bf0087b32a07f5760aa132af.zip | |
Solve 261: Element Digit Sum & Multiple by Two by E. Choroba
| -rwxr-xr-x | challenge-261/e-choroba/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-261/e-choroba/perl/ch-2.pl | 15 |
2 files changed, 32 insertions, 0 deletions
diff --git a/challenge-261/e-choroba/perl/ch-1.pl b/challenge-261/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..7d99202d5e --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub element_digit_sum(@ints) { + abs(sum(@ints) - sum(map { split // } @ints)) +} + +use Test::More tests => 4; + +is element_digit_sum(1, 2, 3, 45), 36, 'Example 1'; +is element_digit_sum(1, 12, 3), 9, 'Example 2'; +is element_digit_sum(1, 2, 3, 4), 0, 'Example 3'; +is element_digit_sum(236, 416, 336, 350), 1296, 'Example 4'; diff --git a/challenge-261/e-choroba/perl/ch-2.pl b/challenge-261/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..2cca2ac3f6 --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub multiple_by_two($start, @ints) { + $start *= 2 while grep $_ == $start, @ints; + return $start +} + +use Test::More tests => 3; + +is multiple_by_two(3, 5, 3, 6, 1, 12), 24, 'Example 1'; +is multiple_by_two(1, 1, 2, 4, 3), 8, 'Example 2'; +is multiple_by_two(2, 5, 6, 7), 2, 'Example 3'; |
