diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-09-09 12:49:18 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-09-09 12:49:18 +0100 |
| commit | 7e545704bdb47154f8d310064bb4e3b6de86e633 (patch) | |
| tree | a18793b68fd7484a28ad07677f631af9da2a4c6f | |
| parent | e83f7e4d7285a22af4a40efa15e28272d79749e2 (diff) | |
| download | perlweeklychallenge-club-7e545704bdb47154f8d310064bb4e3b6de86e633.tar.gz perlweeklychallenge-club-7e545704bdb47154f8d310064bb4e3b6de86e633.tar.bz2 perlweeklychallenge-club-7e545704bdb47154f8d310064bb4e3b6de86e633.zip | |
Week 286 - My word - and min max
| -rw-r--r-- | challenge-286/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-286/peter-campbell-smith/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-286/peter-campbell-smith/perl/ch-2.pl | 56 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-286/peter-campbell-smith/blog.txt b/challenge-286/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..d64971bf8a --- /dev/null +++ b/challenge-286/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/286 diff --git a/challenge-286/peter-campbell-smith/perl/ch-1.pl b/challenge-286/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..2805764338 --- /dev/null +++ b/challenge-286/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-09-09 +use utf8; # Week 286 - task 1 - Self spammer +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use File::Spec; + +my ($script_path, @words, $rand); + +# find location of source script +$script_path = File::Spec->rel2abs(__FILE__); + +# split it into 'words' +@words = split(/\s+/, `cat $script_path`); + +# select a random 'word' +$rand = int(rand(@words)); +say qq[\nOutput: word $rand of ] . (scalar(@words) - 1) . qq[ is '$words[$rand]']; diff --git a/challenge-286/peter-campbell-smith/perl/ch-2.pl b/challenge-286/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..32e4c70e66 --- /dev/null +++ b/challenge-286/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-09-09 +use utf8; # Week 286 - task 2 - Order game +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +my ($minmax, @mm); +@mm = qw(min max); + +order_game(2, 1, 4, 5, 6, 3, 0, 2); +order_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8); + +# larger example +my ($j, @ints); +@ints[$_] = int(rand(50)) for 0 .. 31; +order_game(@ints); + +sub order_game { + + my (@ints, $m, $n, $op, $e); + + # initialise + @ints = @_; + $minmax = 0; + say qq[\nInput: \@ints = (] . join(', ', @ints) . ')'; + + # loop over operations + $n = @ints; + while ($n >= 2) { + $e .= qq[\n\nOperation ] . ++ $op . ':'; + + # loop over pairs + for ($m = 0; $m < $n; $m += 2) { + $e .= qq[\n $mm[$minmax]($ints[$m], $ints[$m + 1]) = ]; + $ints[$m / 2] = minmax($ints[$m], $ints[$m + 1]); + $e .= $ints[$m / 2]; + } + $n /= 2; + } + + # and the answer is ... + say qq[Output: $ints[0]$e]; +} + +sub minmax { + + $minmax = 1 - $minmax; + if ($minmax) { + return $_[0] < $_[1] ? $_[0] : $_[1]; + } else { + return $_[0] > $_[1] ? $_[0] : $_[1]; + } +} |
