aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-28 11:14:02 +0000
committerGitHub <noreply@github.com>2023-11-28 11:14:02 +0000
commit3af55a87ac019c14b9e775507a9533b81d056f03 (patch)
treead2a82a51b850caeba1580fded482556dc5c325d
parentf6e40557378159dd7f891429532b64b551fe88f0 (diff)
parentb340a22018bf5f874e9a0749132647cd4e411773 (diff)
downloadperlweeklychallenge-club-3af55a87ac019c14b9e775507a9533b81d056f03.tar.gz
perlweeklychallenge-club-3af55a87ac019c14b9e775507a9533b81d056f03.tar.bz2
perlweeklychallenge-club-3af55a87ac019c14b9e775507a9533b81d056f03.zip
Merge pull request #9151 from pjcs00/wk245
Week 245
-rw-r--r--challenge-245/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-245/peter-campbell-smith/perl/ch-1.pl24
-rwxr-xr-xchallenge-245/peter-campbell-smith/perl/ch-2.pl69
3 files changed, 94 insertions, 0 deletions
diff --git a/challenge-245/peter-campbell-smith/blog.txt b/challenge-245/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..ab6c9e16fb
--- /dev/null
+++ b/challenge-245/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/245
diff --git a/challenge-245/peter-campbell-smith/perl/ch-1.pl b/challenge-245/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..6cad9a701d
--- /dev/null
+++ b/challenge-245/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use v5.26; # The Weekly Challenge - 2023-11-27
+use utf8; # Week 245 task 1 - Sort language
+use strict; # Peter Campbell Smith
+use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+sort_language(['perl', 'c', 'python'], [2, 1, 3]);
+sort_language(['perl', 'fortran', 'algol', 'pascal', 'html', 'apl', 'c++', 'French'],
+ [7, 3, 5, 1, 8, 2, 4, 6]);
+
+sub sort_language {
+
+ my (@languages, @ranks, @order);
+
+ @languages = @{$_[0]};
+ @ranks = @{$_[1]};
+
+ $order[$ranks[$_] - 1] = $languages[$_] for 0 .. @languages - 1;
+
+ say qq[\nInput: \@lang = ('] . join(q[', '], @languages) . q[')];
+ say qq[ \@popularity = (] . join(', ', @ranks) . ')';
+ say qq[Output: ('] . join(q[', '], @order) . q[')];
+}
diff --git a/challenge-245/peter-campbell-smith/perl/ch-2.pl b/challenge-245/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..a9f90640ff
--- /dev/null
+++ b/challenge-245/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+
+use v5.26; # The Weekly Challenge - 2023-11-27
+use utf8; # Week 245 task 2 - Largest of three
+use strict; # Peter Campbell Smith
+use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use Algorithm::Combinatorics 'combinations';
+
+my ($j, @digits);
+
+largest_of_three(8, 6, 7, 1, 0);
+largest_of_three(0);
+largest_of_three(18446744073709551614, 3);
+largest_of_three(333, 666, 1, 4, 7);
+
+# for $j (0..9) {
+ # push @digits, int(rand(10000));
+# }
+# largest_of_three(@digits);
+largest_of_three(13, 14, 1, 102);
+
+sub largest_of_three {
+
+ my (@digits, $c, $iter, $comb, @set, $value, $result, $length);
+
+ # loop over sizes of combination to consider
+ @digits = @_;
+ $result = -1;
+ $length = 0;
+
+ for ($c = @digits; $c > 0; $c --) {
+
+ # iterate over combinations of length $c numbers
+ $iter = combinations(\@digits, $c);
+ while ($comb = $iter->next) {
+
+ # reverse sort them alphabetically to get best bet first
+ @set = reverse sort @$comb;
+
+ # concatenate them and see if that's the best so far
+ $value = join('', @set);
+
+ # this if is just: if (mult_of_3($value) and $value > $result) accommodating BigInts
+ if (is_mult_of_3($value) and (length($value) > $length
+ or (length($value) eq $length and $value gt $result))) {
+ $result = $value;
+ $length = length($result);
+ }
+ }
+
+ # if we have an answer, no smaller subset will produce a larger number
+ last if $result >= 0;
+ }
+
+ say qq[\nInput: \@digits = (] . join(q[, ], @_) . q[)];
+ say qq[Output: $result];
+}
+
+sub is_mult_of_3 {
+
+ my ($input, $sum);
+
+ $input = shift;
+ $sum = 0;
+ $sum += $1 while $input =~ m|(\d)|g;
+ return $sum % 3 == 0;
+
+}