From 973974e01dfb710ecb927fb1a6fd43df9224a4b2 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Wed, 3 Jan 2024 10:55:05 +0000 Subject: Week 250 - Happy New Year! --- challenge-250/peter-campbell-smith/blog.txt | 1 + challenge-250/peter-campbell-smith/perl/ch-1.pl | 34 +++++++++++++++++++++++++ challenge-250/peter-campbell-smith/perl/ch-2.pl | 26 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 challenge-250/peter-campbell-smith/blog.txt create mode 100755 challenge-250/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-250/peter-campbell-smith/perl/ch-2.pl 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; +} -- cgit