aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2024-04-30 14:55:56 -0400
committerMatthew Neleigh <matthew.neleigh@gmail.com>2024-04-30 14:55:56 -0400
commitadefddc0daafdfe4265009b9c67f19338ea9cda1 (patch)
treefa307627c789acce3c1dfa69545cf5a7728ba173
parent8af4e17dc115ce2e7a19f8fa11e70c799d2f6fb9 (diff)
downloadperlweeklychallenge-club-adefddc0daafdfe4265009b9c67f19338ea9cda1.tar.gz
perlweeklychallenge-club-adefddc0daafdfe4265009b9c67f19338ea9cda1.tar.bz2
perlweeklychallenge-club-adefddc0daafdfe4265009b9c67f19338ea9cda1.zip
new file: challenge-267/mattneleigh/perl/ch-1.pl
new file: challenge-267/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-267/mattneleigh/perl/ch-1.pl73
-rwxr-xr-xchallenge-267/mattneleigh/perl/ch-2.pl102
2 files changed, 175 insertions, 0 deletions
diff --git a/challenge-267/mattneleigh/perl/ch-1.pl b/challenge-267/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..64819f4765
--- /dev/null
+++ b/challenge-267/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ -1, -2, -3, -4, 3, 2, 1 ],
+ [ 1, 2, 0, -2, -1 ],
+ [ -1, -1, 1, -1, 2 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ list_product_sign(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Determine the sign of the product that would result from multiplying
+# together all members of a list of integers
+# Takes one argument:
+# * A list of integers to examine (e.g. ( -1, -2, -3, -4, 3, 2, 1 ) )
+# Returns:
+# * -1 if the sign of the product would be negative
+# * 0 if the product would be zero
+# * 1 if the sign of the product would be positive
+################################################################################
+sub list_product_sign{
+
+ my $negatives = 0;
+
+ foreach my $int (@ARG){
+ # If an int is zero, the product of the list
+ # will be zero
+ return(0)
+ unless($int);
+
+ # Increment the negative count if this int
+ # is below zero
+ $negatives++
+ if($int < 0);
+ }
+
+ return(
+ $negatives % 2 ?
+ # Negative count is odd- product would be
+ # negative
+ -1
+ :
+ # Negative count is even- product would be
+ # positive
+ 1
+ );
+
+}
+
+
+
diff --git a/challenge-267/mattneleigh/perl/ch-2.pl b/challenge-267/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..3043fec084
--- /dev/null
+++ b/challenge-267/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @text_definitions = (
+ [
+ "abcdefghijklmnopqrstuvwxyz",
+ [
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10
+ ]
+ ],
+ [
+ "bbbcccdddaaa",
+ [
+ 4, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10
+ ]
+ ]
+);
+
+print("\n");
+foreach my $text_definition (@text_definitions){
+ printf(
+ "Input: \$str = \"%s\"\n \@widths = (%s)\nOutput: (%s)\n\n",
+ $text_definition->[0],
+ join(", ", @{$text_definition->[1]}),
+ join(", ", count_displayed_lines($text_definition))
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a string of text and a table of letter widths, in pixels, determine
+# how many lines of text would be required to display the string, and how many
+# pixels are required to display the last line
+# Takes one argument:
+# * A ref to an array that contains the string to examine and a table of 26
+# letter widths, in pixels, representing characters a-z (e.g.
+# [
+# "abcdefghijklmnopqrstuvwxyz",
+# [
+# 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+# 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+# 10, 10, 10, 10, 10, 10
+# ]
+# ]
+# )
+# Returns:
+# * A list containing the number of lines required to display the provided
+# text, and the number of pixels required to display the last line (e.g.
+# ( 3, 60 ) )
+################################################################################
+sub count_displayed_lines{
+
+ my $line_width = 0;
+ my $lines = 1;
+
+ # An edge case- if the string is zero length,
+ # there will be no lines and zero pixels
+ return(0, 0)
+ unless(length($ARG[0][0]));
+
+ # Loop over each character
+ foreach my $char (split("", $ARG[0][0])){
+ # Get the character's widgh from the table
+ # using its ordinal position relative to 'a'
+ my $char_width = $ARG[0][1][ord(lc($char)) - 97];
+
+ if(($line_width + $char_width) > 100){
+ # This character won't fit on the line-
+ # increment line count and reset line width to
+ # the character's width
+ $lines++;
+ $line_width = $char_width;
+ } else{
+ # This character fits on the line- add its
+ # width
+ $line_width += $char_width;
+ }
+ }
+
+ return($lines, $line_width);
+
+}
+
+
+