aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2023-11-29 09:39:28 -0500
committerMatthew Neleigh <matthew.neleigh@gmail.com>2023-11-29 09:39:28 -0500
commit4d15cd876f1bfb8f7627b2bfd0f6a84bca8d9056 (patch)
treefd4fb016d872457e999e46afd153034a8fa7de0b
parent313f3f870d8ba92f3a5f7b8e456e6487c380f9d9 (diff)
downloadperlweeklychallenge-club-4d15cd876f1bfb8f7627b2bfd0f6a84bca8d9056.tar.gz
perlweeklychallenge-club-4d15cd876f1bfb8f7627b2bfd0f6a84bca8d9056.tar.bz2
perlweeklychallenge-club-4d15cd876f1bfb8f7627b2bfd0f6a84bca8d9056.zip
new file: challenge-245/mattneleigh/perl/ch-1.pl
new file: challenge-245/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-245/mattneleigh/perl/ch-1.pl86
-rwxr-xr-xchallenge-245/mattneleigh/perl/ch-2.pl162
2 files changed, 248 insertions, 0 deletions
diff --git a/challenge-245/mattneleigh/perl/ch-1.pl b/challenge-245/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..d0c34a1552
--- /dev/null
+++ b/challenge-245/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @ranking_data_sets = (
+ [
+ [ 'perl', 'c', 'python' ],
+ [ 2, 1, 3 ]
+ ],
+ [
+ [ 'c++', 'haskell', 'java' ],
+ [ 1, 3, 2 ]
+ ]
+);
+
+print("\n");
+foreach my $ranking_data (@ranking_data_sets){
+ printf(
+ "Input: \@lang = (%s)\n \@popularity = (%s)\nOutput: (%s)\n\n",
+ join(
+ ", ",
+ map(
+ "'" . $_ . "'",
+ @{$ranking_data->[0]}
+ )
+ ),
+ join(", ", @{$ranking_data->[1]}),
+ join(
+ ", ",
+ map(
+ "'" . $_ . "'",
+ sort_languages_by_popularity($ranking_data)
+ )
+ )
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Sort a list of languages by their popularity rankings
+# Takes one argument:
+# * A set of language rankings in the form of a ref to an array that contains
+# a list of language names and a list of their respective popularity rankings
+# (e.g.
+# [
+# [ "c++", "haskell", "java" ],
+# [ 1, 3, 2 ]
+# ]
+# )
+# Returns:
+# * A list of language names sorted by their popularity (e.g.
+# ( "c++", "java", "haskell" ) )
+################################################################################
+sub sort_languages_by_popularity{
+
+ return(
+ # 2: Create an array slice consisting of
+ # the language names, sorted in the order
+ # defined by the rankings
+ @{$ARG[0][0]}[
+ # 1: Create a set of array indices based on
+ # the language rankings sorted in ascending
+ # order
+ sort(
+ { $ARG[0][1][$a] <=> $ARG[0][1][$b] }
+ 0 .. $#{$ARG[0][0]}
+ )
+ ]
+ );
+
+}
+
+
+
diff --git a/challenge-245/mattneleigh/perl/ch-2.pl b/challenge-245/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..51d4a676a1
--- /dev/null
+++ b/challenge-245/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,162 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_sets = (
+ [ 8, 1, 9 ],
+ [ 8, 6, 7, 1, 0 ],
+ [ 1 ]
+);
+
+print("\n");
+foreach my $integer_set (@integer_sets){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_set}),
+ find_max_divisible_permutation(3, @{$integer_set})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the largest number that can be formed by concatenating some
+# quantity of integers that are greater than or equal to zero, in any order,
+# but which is also evenly divisible by a given integer divisor.
+# Takes two arguments:
+# * The integer divisor (e.g. 3)
+# * A list of integers greater than or equal to zero (e.g.
+# ( 8, 6, 7, 1, 0 ) )
+# Returns on success
+# * The largest number that can be made from the digits in the list that is
+# also divisible by the specified divisor, if such a number can be
+# constructed (e.g. 8760 )
+# - OR -
+# * -1 if such a number cannot be constructed
+################################################################################
+sub find_max_divisible_permutation{
+ my $divisor = shift();
+
+ my $max = -1;
+
+ # Loop over each combination of digits
+ for my $combination (combinations(@ARG)){
+ # Skip the empty list that combinations() can
+ # return (see its documentation)
+ next
+ unless(@{$combination});
+
+ my $permutations = [];
+
+ # Generate permutations for this
+ # combination
+ permutations($permutations, $combination);
+
+ # Loop over each permutation within this
+ # combination
+ for my $permutation (@$permutations){
+ # Combine the digits from this permutation
+ my $num = join("", @{$permutation});
+
+ # Strip leading zeros, if any, so numbers
+ # don't get interpreted as octals
+ $num =~ s/^0+//;
+
+ # Skip cases in which the number has no
+ # digits left after removing zeros
+ next
+ unless(length($num));
+
+ # Update the maximum if this number is
+ # bigger and divisible by the divisor
+ $max = $num
+ if(($num > $max) && !($num % $divisor));
+ }
+ }
+
+ return($max);
+
+}
+
+
+
+################################################################################
+# Given a list of N integers, provides a list of all possible combinations of
+# M integers, for all M where 0 <= M <= N
+# Takes one argument:
+# * A list of integers (e.g. (2, 1, 4) )
+# Returns:
+# * A list of all possible combinations of M integers, including where M equals
+# zero (e.g.
+# (
+# [ ],
+# [ 2 ],
+# [ 1 ],
+# [ 2, 1 ],
+# [ 4 ],
+# [ 2, 4 ],
+# [ 1, 4 ],
+# [ 2, 1, 4 ]
+# )
+# )
+#
+# NOTE: Translated and adapted from a Python solution found in the comments at:
+# https://stackoverflow.com/questions/464864/get-all-possible-2n-combinations-of-a-list-s-elements-of-any-length
+################################################################################
+sub combinations{
+
+ return([])
+ unless(scalar(@ARG));
+
+ my @cs;
+
+ for my $c (combinations(@ARG[1 .. $#ARG])){
+ push(@cs, [ @{$c} ], [ $ARG[0], @{$c} ]);
+ }
+
+ return(@cs);
+
+}
+
+
+
+################################################################################
+# Generate all possible permutations of a given list
+# Takes two arguments:
+# * A ref to an empty array, which will be populated with a list of
+# permutations of the original list
+# * A ref to an array whose permutations are desired
+# Returns no meaningful value
+# NOTE: Adapted from a solution found at:
+# https://www.perlmonks.org/?node_id=1186402
+################################################################################
+sub permutations{
+ my $out = shift();
+ my( $in, $pre ) = ( @_, [] );
+
+ push(@{$out}, [ @{$pre} ])
+ unless(@$in);
+
+ for (0 .. $#$in){
+ permutations(
+ $out,
+ [ @{$in}[0 .. $_ - 1, $_ + 1 .. $#$in] ],
+ [ @$pre, $in->[$_] ]
+ );
+ }
+
+}
+
+
+