aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-05 22:51:16 +0100
committerGitHub <noreply@github.com>2025-09-05 22:51:16 +0100
commit4b664b3a7ba9b30cbbee50d3b0bde1b2c2bcb45f (patch)
treea954698d5d96e421097dfe041a49cb847782af4c
parent44298448ebe72d2ef46cd89d3b9eea93a35a861e (diff)
parented23953c753d60b87354401ec42a2e0dccd0f4b7 (diff)
downloadperlweeklychallenge-club-4b664b3a7ba9b30cbbee50d3b0bde1b2c2bcb45f.tar.gz
perlweeklychallenge-club-4b664b3a7ba9b30cbbee50d3b0bde1b2c2bcb45f.tar.bz2
perlweeklychallenge-club-4b664b3a7ba9b30cbbee50d3b0bde1b2c2bcb45f.zip
Merge pull request #12628 from mattneleigh/pwc337
new file: challenge-337/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-337/mattneleigh/perl/ch-1.pl99
-rwxr-xr-xchallenge-337/mattneleigh/perl/ch-2.pl106
2 files changed, 205 insertions, 0 deletions
diff --git a/challenge-337/mattneleigh/perl/ch-1.pl b/challenge-337/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..0d9ae8f23b
--- /dev/null
+++ b/challenge-337/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 6, 5, 4, 8 ],
+ [ 7, 7, 7, 7 ],
+ [ 5, 4, 3, 2, 1 ],
+ [ -1, 0, 3, -2, 1 ],
+ [ 0, 1, 1, 2, 0 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@num1 = (%s)\nOutput: (%s)\n\n",
+ join(", ", @{$integer_list}),
+ join(", ", count_lesser_instances(@{$integer_list})),
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of numbers, determine how many numbers within the array have a
+# value less than or equal to each individual number, NOT including itself
+# Takes one argument:
+# * An array of integers to examine (e.g. (-1, 0, 3, -2, 1) )
+# Returns:
+# * An array that corresponds to the given array, such that each element in the
+# array returned is a count of elements in the original array that are less
+# than or equal to the value of the element at that position, NOT including
+# itself (e.g. (1, 2, 4, 0, 3) )
+################################################################################
+sub count_lesser_instances{
+
+ my $int;
+ my @sorted_counts;
+ my $current_count;
+
+ # Count instances of every value in the given
+ # list, and produce a sorted list of these
+ # values and their instance counts
+ {
+ my %instance_counts;
+
+ foreach $int (@ARG){
+ $instance_counts{$int}++;
+ }
+ @sorted_counts = map(
+ [ $_, $instance_counts{$_} ],
+ sort({ $a <=> $b } keys(%instance_counts))
+ );
+ }
+
+ return(
+ map(
+ # Run this block on every value from the gven
+ # list
+ {
+ $current_count = 0;
+
+ # Loop over the sorted counts of each instance
+ foreach my $instance_count (@sorted_counts){
+ if($instance_count->[0] == $_){
+ # This count is of values that equal the
+ # current one- add one less than this count
+ # to the total and break out of the loop
+ $current_count += $instance_count->[1] - 1;
+ last;
+ } else{
+ # This count is of a value that is not equal
+ # to the current one- add this count to the
+ # total
+ $current_count += $instance_count->[1];
+ }
+ }
+
+ $current_count;
+ }
+ @ARG
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-337/mattneleigh/perl/ch-2.pl b/challenge-337/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..c2845f7a2f
--- /dev/null
+++ b/challenge-337/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,106 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @matrix_parameter_sets = (
+ [
+ 2, 3, [ [0, 1], [1, 1] ]
+ ],
+ [
+ 2, 2, [ [1, 1], [0, 0] ]
+ ],
+ [
+ 3, 3, [ [0, 0], [1, 2], [2, 1] ]
+ ],
+ [
+ 1, 5, [ [0, 2], [0, 4] ]
+ ],
+ [
+ 4, 2, [ [1, 0], [3, 1], [2, 0], [0, 1] ]
+ ]
+);
+
+print("\n");
+foreach my $matrix_parameters (@matrix_parameter_sets){
+ printf(
+ "Input: \$row = %d, \$col = %d, \@locations = (%s)\nOutput: %d\n\n",
+ $matrix_parameters->[0],
+ $matrix_parameters->[1],
+ join(
+ ", ",
+ map(
+ "[ " . join(", ", @{$_}) . " ]",
+ @{$matrix_parameters->[2]}
+ )
+ ),
+ increment_matrix_count_odds($matrix_parameters)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a set of dimensions that define a matrix (rows, columns) and a list of
+# positions therein, increment by one all the values in each row and column
+# specified by each position, and then count how many positions within the
+# matrix hold odd values
+# Takes one argument:
+# * A ref to an array that contains the size of the desired matrix, and the
+# list of positions to increment (e.g. [ 2, 3, [ [0, 1], [1, 1] ] ] )
+# Returns
+# * The number of positions in the matrix that have odd values after
+# incrementing all the values in the rows and columns specified in the
+# specified list of positions (e.g. 6)
+################################################################################
+sub increment_matrix_count_odds{
+ my $parameters = shift();
+
+ my $col;
+ my $row;
+ my $odd_count = 0;
+
+ # Create a matrix of zeros with the speciied
+ # dimensions
+ my @matrix = map(
+ [ map(0, 1 .. $parameters->[1]) ],
+ 1 .. $parameters->[0]
+ );
+
+ # Loop over each specified location
+ foreach my $location (@{$parameters->[2]}){
+ # Increment each value in the specified row
+ foreach $col (0 .. $parameters->[1] - 1){
+ $matrix[$location->[0]][$col]++;
+ }
+ # Increment each value in the specified column
+ foreach $row (0 .. $parameters->[0] - 1){
+ $matrix[$row][$location->[1]]++;
+ }
+ }
+
+ # Scan the entire matrix and add to the count
+ # whenever an odd value is detected
+ foreach $row (0 .. $parameters->[0] - 1){
+ foreach $col (0 .. $parameters->[1] - 1){
+ $odd_count++
+ if($matrix[$row][$col] % 2);
+ }
+ }
+
+ return($odd_count);
+
+}
+
+
+