diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-01-03 10:55:05 +0000 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-01-03 10:55:05 +0000 |
| commit | 973974e01dfb710ecb927fb1a6fd43df9224a4b2 (patch) | |
| tree | 9005c61d30a217694e1d1c496329e104ee471d87 | |
| parent | 8fc3b368ac11c9b264fc21c5ccf853e4b61118a6 (diff) | |
| download | perlweeklychallenge-club-973974e01dfb710ecb927fb1a6fd43df9224a4b2.tar.gz perlweeklychallenge-club-973974e01dfb710ecb927fb1a6fd43df9224a4b2.tar.bz2 perlweeklychallenge-club-973974e01dfb710ecb927fb1a6fd43df9224a4b2.zip | |
Week 250 - Happy New Year!
| -rw-r--r-- | challenge-250/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-250/peter-campbell-smith/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-250/peter-campbell-smith/perl/ch-2.pl | 26 |
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-250/peter-campbell-smith/blog.txt b/challenge-250/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..3f30d6cd51 --- /dev/null +++ b/challenge-250/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/250 diff --git a/challenge-250/peter-campbell-smith/perl/ch-1.pl b/challenge-250/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..9134dd5129 --- /dev/null +++ b/challenge-250/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-01 +use utf8; # Week 250 task 1 - Smallest index +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode STDOUT, ':utf8'; + +smallest_index(4, 3, 2, 1); +smallest_index(2, 4, 6, 7, 3, 9, 1, 0, 2, 4, 7, 8, 2, 3); +smallest_index(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); + +sub smallest_index { + + my (@ints, $i, $i_mod10); + + #initialise + @ints = @_; + say qq[\nInput: \@ints = (] . join(', ', @ints) . ')'; + + # loop over supplied values + for $i (0 .. @ints - 1) { + $i_mod10 = $i % 10; + + # apply stated rule + if ($i_mod10 == $ints[$i]) { + say qq[Output: $i ∵ \$i = $i, \$i mod 10 = $i_mod10, \$ints[$i] = $ints[$i]]; + return; + } + } + say qq[Output: -1]; +} +
\ No newline at end of file diff --git a/challenge-250/peter-campbell-smith/perl/ch-2.pl b/challenge-250/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..94ccd20736 --- /dev/null +++ b/challenge-250/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-01 +use utf8; # Week 250 task 2 - Alphanumeric string value +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +alpha_string_value('perl', '2', '000', 'python', 'r4ku'); +alpha_string_value('001', '1', '000', '0001'); +alpha_string_value('3', 'blind', 'mice', 'and', '10', 'green', 'bottles'); + +sub alpha_string_value { + + my (@a, $max, $s, $value); + + @a = @_; + + # loop over array elements and apply the stated rule + $max = -1; + for $s (@a) { + $value = $s =~ m|^(\d+)$| ? $1 + 0 : length($s); + $max = $value if $value > $max; + } + + say qq[\nInput: \@alphanumstr = ('] . join(q[', '], @a) . qq[')\nOutput: ] . $max; +} |
