|
| 1 | +#!/usr/bin/env -S perl -CSDA |
| 2 | + |
| 3 | +=pod |
| 4 | +
|
| 5 | +-------------------------------------------------------------------------------------------------------------- |
| 6 | +COLOPHON: |
| 7 | +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). |
| 8 | +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 |
| 9 | +
|
| 10 | +-------------------------------------------------------------------------------------------------------------- |
| 11 | +TITLE BLOCK: |
| 12 | +Solutions in Perl for The Weekly Challenge 245-2. |
| 13 | +Written by Robbie Hatley on Tue Nov 28, 2023. |
| 14 | +
|
| 15 | +-------------------------------------------------------------------------------------------------------------- |
| 16 | +PROBLEM DESCRIPTION: |
| 17 | +Task 245-2: Largest of Three |
| 18 | +Submitted by: Mohammad S Anwar |
| 19 | +You are given an array of integers >= 0. Write a script to return |
| 20 | +the largest number formed by concatenating some of the given |
| 21 | +integers in any order which is also multiple of 3. Return -1 if |
| 22 | +none found. |
| 23 | +
|
| 24 | +Example 1: |
| 25 | +Input: @ints = (8, 1, 9) |
| 26 | +Output: 981 |
| 27 | +981 % 3 == 0 |
| 28 | +
|
| 29 | +Example 2: |
| 30 | +Input: @ints = (8, 6, 7, 1, 0) |
| 31 | +Output: 8760 |
| 32 | +
|
| 33 | +Example 3: |
| 34 | +Input: @ints = (1) |
| 35 | +Output: -1 |
| 36 | +
|
| 37 | +-------------------------------------------------------------------------------------------------------------- |
| 38 | +PROBLEM NOTES: |
| 39 | +This WOULD BE just a matter of combinatorics if not for the fact that no upper bound is given for our |
| 40 | +non-negative integers. That means that we'll be "concatenating" integers with possibly more-than-one digit, |
| 41 | +so we can't just assume that each integer is one digit then concatenate all permutations of all combinations. |
| 42 | +Instead, we need to make a "sub concatenate($aref)" which first splits each input into its digits, then |
| 43 | +pushes those clusters of digits onto an array, then joins that array. Also we'll need a "sub are_nni($aref)" |
| 44 | +to check that all inputs are non-negative integers, and a "sub largest_of_three($aref)" to find the largest |
| 45 | +multiple of 3 we can make or return -1 if we can't make any. THEN the rest is just permutations of |
| 46 | +combinations. More work for CPAN module "Math::Combinatorics". This time I'll use it's non-OOP functions, |
| 47 | +as OOP just isn't necessary for a problem like this, and indeed just gets in the way. |
| 48 | +
|
| 49 | +-------------------------------------------------------------------------------------------------------------- |
| 50 | +IO NOTES: |
| 51 | +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a |
| 52 | +single-quoted array of arrays of non-negative integers, in proper Perl syntax, like so: |
| 53 | +./ch-2.pl '([3,14,0,5,72],[1,0,97,23])' |
| 54 | +
|
| 55 | +Output is to STDOUT and will be each input array followed by the corresponding output. |
| 56 | +
|
| 57 | +=cut |
| 58 | + |
| 59 | +# ------------------------------------------------------------------------------------------------------------ |
| 60 | +# PRAGMAS AND MODULES USED: |
| 61 | + |
| 62 | +use v5.38; |
| 63 | +use strict; |
| 64 | +use warnings; |
| 65 | +use utf8; |
| 66 | +use warnings FATAL => 'utf8'; |
| 67 | +use Sys::Binmode; |
| 68 | +use Time::HiRes 'time'; |
| 69 | +use Math::Combinatorics; |
| 70 | + |
| 71 | +# ------------------------------------------------------------------------------------------------------------ |
| 72 | +# START TIMER: |
| 73 | +our $t0; |
| 74 | +BEGIN {$t0 = time} |
| 75 | + |
| 76 | +# ------------------------------------------------------------------------------------------------------------ |
| 77 | +# SUBROUTINES: |
| 78 | + |
| 79 | +# Are all of the elements of a referred-to array decimal representations of non-negative integers? |
| 80 | +sub are_nni ($aref) { |
| 81 | + return 0 if 'ARRAY' ne ref $aref; |
| 82 | + return 0 if scalar(@$aref) < 1; |
| 83 | + for (@$aref) {return 0 if !/^0$|^[1-9]\d*$/} |
| 84 | + return 1; |
| 85 | +} |
| 86 | + |
| 87 | +sub concatenate($aref) { |
| 88 | + my @digits; |
| 89 | + for (@$aref) {push @digits, split(//,$_)} |
| 90 | + return join('',@digits); |
| 91 | +} |
| 92 | + |
| 93 | +sub largest_of_three($aref) { |
| 94 | + # For each possible non-empty subset size of @$aref, get all combinations |
| 95 | + # of that size, then get all permutations of each of those combinations, |
| 96 | + # then concatentate each of those permutations to an integer, and keep track |
| 97 | + # of the maximum divisible-by-3 integer seen, then return the maximum |
| 98 | + # which will be -1 if we couldn't make any divisible-by-3 integers: |
| 99 | + my $max = -1; |
| 100 | + for ( my $size = scalar(@$aref) ; $size >= 1 ; --$size ) { |
| 101 | + my @combs = combine($size,@$aref); |
| 102 | + for my $cref ( @combs ) { |
| 103 | + my @perms = permute(@$cref); |
| 104 | + for my $pref ( @perms ) { |
| 105 | + my $integer = concatenate($pref); |
| 106 | + 0 == $integer % 3 && $integer > $max and $max = $integer; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return $max; |
| 111 | +} |
| 112 | + |
| 113 | +# ------------------------------------------------------------------------------------------------------------ |
| 114 | +# MAIN BODY OF PROGRAM: |
| 115 | + |
| 116 | +# Inputs: |
| 117 | +my @arrays = @ARGV ? eval($ARGV[0]) : |
| 118 | +( |
| 119 | + # Example 1 Input: |
| 120 | + [8, 1, 9], |
| 121 | + # Expected Output: 981 |
| 122 | + |
| 123 | + # Example 2 Input: |
| 124 | + [8, 6, 7, 1, 0], |
| 125 | + # Expected Output: 8760 |
| 126 | + |
| 127 | + # Example 3 Input: |
| 128 | + [1], |
| 129 | + # Expected Output: -1 |
| 130 | +); |
| 131 | + |
| 132 | +# Main loop: |
| 133 | +for my $aref (@arrays) { |
| 134 | + say ''; |
| 135 | + say 'Array = (' . join(', ', @$aref) . ')'; |
| 136 | + if ( !are_nni($aref) ) { |
| 137 | + say 'Error: not an array of non-negative integers.'; |
| 138 | + say 'Moving on to next array.'; |
| 139 | + next; |
| 140 | + } |
| 141 | + say 'Greatest multiple of 3 creatable from array = ', |
| 142 | + largest_of_three($aref); |
| 143 | +} |
| 144 | +exit; |
| 145 | + |
| 146 | +# ------------------------------------------------------------------------------------------------------------ |
| 147 | +# DETERMINE AND PRINT EXECUTION TIME: |
| 148 | +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} |
| 149 | +__END__ |
0 commit comments