diff options
| -rw-r--r-- | challenge-244/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-244/peter-campbell-smith/ch-1.pl | 38 | ||||
| -rwxr-xr-x | challenge-244/peter-campbell-smith/ch-2.pl | 53 |
3 files changed, 92 insertions, 0 deletions
diff --git a/challenge-244/blog.txt b/challenge-244/blog.txt new file mode 100644 index 0000000000..b570786a2c --- /dev/null +++ b/challenge-244/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge diff --git a/challenge-244/peter-campbell-smith/ch-1.pl b/challenge-244/peter-campbell-smith/ch-1.pl new file mode 100755 index 0000000000..a76a458d1f --- /dev/null +++ b/challenge-244/peter-campbell-smith/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-11-20 +use utf8; # Week 244 task 1 - Count smaller +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +count_smaller(8, 1, 2, 2, 3); +count_smaller(0, 6, 5, 4, 8); + +my @nums; +$nums[$_] = int(rand(100)) + 1 for 0 .. 99; +count_smaller(@nums); + +sub count_smaller { + + my (@ints, @sorted, $i, $old_i, $count, %less_than, $result); + + # initialise + @ints = @_; + @sorted = sort {$a <=> $b} @ints; + $count = 0; + $old_i = -1; + + # create $less_than[$i] = number of $ints[$i] less than $i + for $i (@sorted) { + $less_than{$i} = $count if $i > $old_i; + $count ++; + $old_i = $i; + } + + # format and output + $result .= $less_than{$_} . ', ' for @ints; + $result =~ s|..$||; + + say qq[\nInput: \@ints = (] . join(', ', @ints) . ')'; + say qq[Output: ($result)]; +}
\ No newline at end of file diff --git a/challenge-244/peter-campbell-smith/ch-2.pl b/challenge-244/peter-campbell-smith/ch-2.pl new file mode 100755 index 0000000000..488a821fb5 --- /dev/null +++ b/challenge-244/peter-campbell-smith/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-11-20 +use utf8; # Week 244 task 2 - Group hero +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use Algorithm::Combinatorics 'combinations'; +my $details = 1; + +group_hero(2, 1, 4); +group_hero(6, 8, 10); + +$details = 0; +group_hero(1, 2, 4, 5, 7, 9, 11, 15, 19, 23, 30, 31, 39, 41, 47); + +sub group_hero { + + my (@nums, $count, $sum, $c, $iter, $comb, $smallest, $largest, $i, $explain, $product); + + # initialise + @nums = @_; + $count = @_; + $sum = 0; + $explain = ''; + + # loop over sizes of combination to consider + for $c (1 .. $count) { + + # loop over combinations from @nums of that size + $iter = combinations(\@nums, $c); + while ($comb = $iter->next) { + + # get smallest and largest + $smallest = 1e6; + $largest = -1e6; + for $i (0 .. $c - 1) { + $smallest = $comb->[$i] if $comb->[$i] < $smallest; + $largest = $comb->[$i] if $comb->[$i] > $largest; + } + + # increment $sum + $product = $smallest * $largest ** 2; + $explain .= ' (' . join(', ', @$comb) . ') => ' . ($largest ** 2) . qq[ * $smallest = $product\n] + if $details; + $sum += $product + } + } + + # show results + say qq[\nInput: \@nums = (] . join(', ', @nums) . ')'; + say qq[Output: $sum\n] . substr($explain, 0, -1); +}
\ No newline at end of file |
