aboutsummaryrefslogtreecommitdiff
path: root/challenge-261
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2024-03-21 18:32:21 -0400
committerMatthew Neleigh <matthew.neleigh@gmail.com>2024-03-21 18:32:21 -0400
commit8311eb7ba69bf713909f0af1a6dfd1da8b6aec0b (patch)
tree357200b3635b85ed2d93ce357bf9603cc5af58a7 /challenge-261
parent8e95e94de527f8344f979dbb45b935139b1ddeb7 (diff)
downloadperlweeklychallenge-club-8311eb7ba69bf713909f0af1a6dfd1da8b6aec0b.tar.gz
perlweeklychallenge-club-8311eb7ba69bf713909f0af1a6dfd1da8b6aec0b.tar.bz2
perlweeklychallenge-club-8311eb7ba69bf713909f0af1a6dfd1da8b6aec0b.zip
new file: challenge-261/mattneleigh/perl/ch-1.pl
new file: challenge-261/mattneleigh/perl/ch-2.pl
Diffstat (limited to 'challenge-261')
-rwxr-xr-xchallenge-261/mattneleigh/perl/ch-1.pl65
-rwxr-xr-xchallenge-261/mattneleigh/perl/ch-2.pl81
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-261/mattneleigh/perl/ch-1.pl b/challenge-261/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..4b6e5520ff
--- /dev/null
+++ b/challenge-261/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 1, 2, 3, 45 ],
+ [ 1, 12, 3 ],
+ [ 1, 2, 3, 4 ],
+ [ 236, 416, 336, 350 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ calculate_absolute_difference(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the absolute difference between the sum of the elements of a list
+# of positive integers, and the sum of all the digits within each integer in
+# the list
+# Takes one argument:
+# * The list of integers to examine (e.g. ( 1, 12, 3 ) )
+# Returns:
+# * The absolute difference between the sum of the elements and the sum of the
+# digits within every element (e.g. 9 )
+################################################################################
+sub calculate_absolute_difference{
+
+ my $element_sum = 0;
+ my $digit_sum = 0;
+
+ foreach my $element (@ARG){
+ # Add each number- and all its digits-
+ # to separate running totals
+ $element_sum += $element;
+ foreach my $digit (split(//, $element)){
+ $digit_sum += $digit;
+ }
+ }
+
+ # Calculate the absolute value of the
+ # difference between the two totals
+ return(abs($element_sum - $digit_sum));
+
+}
+
+
+
diff --git a/challenge-261/mattneleigh/perl/ch-2.pl b/challenge-261/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..892cf93ead
--- /dev/null
+++ b/challenge-261/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @data_sets = (
+ [
+ [ 5, 3, 6, 1, 12 ],
+ 3
+ ],
+ [
+ [ 1, 2, 4, 3 ],
+ 1
+ ],
+ [
+ [ 5, 6, 7 ],
+ 2
+ ]
+);
+
+print("\n");
+foreach my $data_set (@data_sets){
+ printf(
+ "Input: \@ints = (%s) and \$start = %d\nOutput: %d\n\n",
+ join(", ", @{$data_set->[0]}),
+ $data_set->[1],
+ double_within_list_values($data_set)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a list of integers and a start value, continue doubling the start value
+# until a value that is NOT in the list is reached; this value is then
+# returned. If the original value was not found in the list, it is returned
+# unaltered.
+# Takes one argument:
+# * A ref to a data structure that contains the list of integers to examine,
+# and the start value (e.g.
+# [
+# [ 5, 3, 6, 1, 12 ],
+# 3
+# ]
+# )
+# Returns:
+# * The first doubled value that does NOT appear in the list, or the original
+# start value if it did not appear in the list (e.g. 24 )
+################################################################################
+sub double_within_list_values{
+
+ # Make a lookup table of supplied array
+ # values
+ my %value_table = map(
+ { $_ => 1 }
+ @{$ARG[0][0]}
+ );
+ my $value = $ARG[0][1];
+
+ # Double $value while its current... er,
+ # value... keeps appearing in the table
+ while($value_table{$value}){
+ $value *= 2;
+ }
+
+ return($value);
+
+}
+
+
+