diff options
| author | pme <hauptadler@gmail.com> | 2024-03-18 08:43:19 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-03-18 08:43:19 +0100 |
| commit | 6828eb73e0cc1583b5e03dfb28af5afee549c695 (patch) | |
| tree | 34005363bab2f913de614a904ba082e47fd346b7 | |
| parent | cad939ed2335566e31b26c5b74b9fc99c23db7a5 (diff) | |
| download | perlweeklychallenge-club-6828eb73e0cc1583b5e03dfb28af5afee549c695.tar.gz perlweeklychallenge-club-6828eb73e0cc1583b5e03dfb28af5afee549c695.tar.bz2 perlweeklychallenge-club-6828eb73e0cc1583b5e03dfb28af5afee549c695.zip | |
challenge-261
| -rwxr-xr-x | challenge-261/peter-meszaros/perl/ch-1.pl | 74 | ||||
| -rwxr-xr-x | challenge-261/peter-meszaros/perl/ch-2.pl | 72 |
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-261/peter-meszaros/perl/ch-1.pl b/challenge-261/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..976027a7f6 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 45], + [1, 12, 3], + [1, 2, 3, 4], + [236, 416, 336, 350], +]; + +sub element_digit_sum +{ + my $l = shift; + + my $elem_sum = 0; + my $digi_sum = 0; + + for my $e (@$l) { + $elem_sum += $e; + $digi_sum += $_ for split('', $e); + } + + return abs($elem_sum - $digi_sum); +} + +is(element_digit_sum($cases->[0]), 36, 'Example 1'); +is(element_digit_sum($cases->[1]), 9, 'Example 2'); +is(element_digit_sum($cases->[2]), 0, 'Example 3'); +is(element_digit_sum($cases->[3]), 1296, 'Example 4'); +done_testing(); + +exit 0; diff --git a/challenge-261/peter-meszaros/perl/ch-2.pl b/challenge-261/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..1cc344c748 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[5, 3, 6, 1, 12], 3], + [[1, 2, 4, 3], 1], + [[5, 6, 7], 2], +]; + +sub multiply_by_two +{ + my ($l, $s) = $_[0]->@*; + + START: + for my $i (@$l) { + if ($i == $s) { + $s *= 2; + goto START; + } + } + return $s; +} + +is(multiply_by_two($cases->[0]), 24, 'Example 1'); +is(multiply_by_two($cases->[1]), 8, 'Example 2'); +is(multiply_by_two($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; + |
