aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-258/mattneleigh/perl/ch-1.pl59
-rwxr-xr-xchallenge-258/mattneleigh/perl/ch-2.pl86
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-258/mattneleigh/perl/ch-1.pl b/challenge-258/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..d3c979e846
--- /dev/null
+++ b/challenge-258/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 10, 1, 111, 24, 1000 ],
+ [ 111, 1, 11111 ],
+ [ 2, 8, 1024, 256 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ count_ints_with_even_digits(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a list of integers, determine how many have an even number of digits
+# Takes one argument:
+# * A list of integers to examine
+# Returns
+# * The count of integers that have an even number of digits
+################################################################################
+sub count_ints_with_even_digits{
+
+ my $count = 0;
+
+ foreach my $integer (@ARG){
+ # Remove anything that isn't a digit
+ $integer =~ s/\D//g;
+
+ # Increment the count unless the number of
+ # digits is odd
+ $count++
+ unless(length($integer) % 2);
+ }
+
+ return($count);
+
+}
+
+
+
diff --git a/challenge-258/mattneleigh/perl/ch-2.pl b/challenge-258/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..9ceff07fee
--- /dev/null
+++ b/challenge-258/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @things = (
+ [
+ [ 2, 5, 9, 11, 3 ],
+ 1
+ ],
+ [
+ [ 2, 5, 9, 11, 3 ],
+ 2
+ ],
+ [
+ [ 2, 5, 9, 11, 3 ],
+ 0
+ ]
+);
+
+print("\n");
+foreach my $thing (@things){
+ printf(
+ "Input: \@ints = (%s)\n \$k = %d\nOutput: %d\n\n",
+ join(", ", @{$thing->[0]}),
+ $thing->[1],
+ sum_elements_with_particularly_positive_indices(
+ $thing->[1],
+ @{$thing->[0]}
+ )
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of integers, sum the elements whose indices within the array
+# have a specific number of ones in their binary representation
+# Takes two arguments:
+# * The desired number of ones in array indices (e.g. 1 )
+# * An array of integers (e.g. ( 2, 5, 9, 11, 3 ) )
+# Returns:
+# * The sum of the integers from the array whose indices have the desired
+# quantity of ones (e.g. 17 (which is 5 + 9 + 3))
+################################################################################
+sub sum_elements_with_particularly_positive_indices{
+ my $desired_ones = shift();
+
+ my $index_value;
+ my $ones;
+ my $sum = 0;
+
+ for my $index (0 .. $#ARG){
+ $index_value = $index;
+ $ones = 0;
+
+ # Count the number of ones in this index value
+ while($index_value){
+ $ones++
+ if($index_value & 0x01);
+ $index_value >>= 1;
+ }
+
+ # If this index had the number of ones we're
+ # looking for, add the array value at this
+ # index to the sum
+ $sum += $ARG[$index]
+ if($ones == $desired_ones);
+ }
+
+ return($sum);
+
+}
+
+
+