Perl Weekly Challenge 376.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 376.
Task 1: Chessboard Squares
Submitted by: Mohammad Sajid Anwar
You are given two coordinates of a square on 8x8 chessboard.
Write a script to find the given two coordinates have the
same colour.
8 W B W B W B W B
7 B W B W B W B W
6 W B W B W B W B
5 B W B W B W B W
4 W B W B W B W B
3 B W B W B W B W
2 W B W B W B W B
1 B W B W B W B W
a b c d e f g h
Example 1
Input: $c1 = "a7", $c2 = "f4"
Output: true
Example 2
Input: $c1 = "c1", $c2 = "e8"
Output: false
Example 3
Input: $c1 = "b5", $c2 = "h2"
Output: false
Example 4
Input: $c1 = "f3", $c2 = "h1"
Output: true
Example 5
Input: $c1 = "a1", $c2 = "g8"
Output: false
Two coordinates have the same colour if the
number of horizontal and vertical steps to get from one of their
corresponding positions to the other is an even number. For example, one can reach from a7 to
f4 by taking ord(f)-ord(a)=5 horizontal steps and 7-4=3 vertical steps,
and 5+3=8 is an even number. Thus, from the coordinates I have to
get the horizontal components and subtract them, get the vertical
components and subtract them, and finally, add both results and check
their divisibility by 2. The results takes a 1.5-liner.
Examples:
perl -E '
for my($p,$q)(@ARGV){@c=map{split""}($q,$p);@c[0,2]=map{ord}@c[0,2];say "$p $q -> ",
($c[0]-$c[2]+$c[1]-$c[3])%2?"F":"T"}
' a7 f4 c1 e8 b5 h2 f3 h1 a1 g8
Results:
a7 f4 -> T
c1 e8 -> F
b5 h2 -> F
f3 h1 -> T
a1 g8 -> F
The full code is:
1 # Perl weekly challenge 376
2 # Task 1: Chessboard Squares
3 #
4 # See https://wlmb.github.io/2026/06/01/PWC376/#task-1-chessboard-squares
5 use v5.36;
6 use feature qw(try);
7 die <<~"FIN" unless @ARGV and @ARGV%2==0;
8 Usage: $0 P0 Q0 P1 Q1...
9 to find if positions Pn Qn on a chess board have
10 the same color.
11 FIN
12 for my($p,$q)(@ARGV){
13 try {
14 do {die "Invalid position: $_" unless /^[a-h][1-8]/} for $p, $q;
15 my ($horizontalP, $verticalP, $horizontalQ, $verticalQ)
16 = map {split ""} ($p, $q);
17 ($horizontalP, $horizontalQ) = map {ord} ($horizontalP, $horizontalQ);
18 my $steps = $horizontalP-$horizontalQ+$verticalP-$verticalQ;
19 say "$p $q -> ", $steps%2?"False":"True";
20 }
21 catch($e){warn $e}
22 }
Example:
./ch-1.pl a7 f4 c1 e8 b5 h2 f3 h1 a1 g8
Results:
a7 f4 -> True
c1 e8 -> False
b5 h2 -> False
f3 h1 -> True
a1 g8 -> False
Task 2: Doubled Words
Submitted by: Matt Martini
You are given a string (which may contain embedded newlines)
which is taken from a page on a website. The string will not
contain brackets qw{ [ ] }.
Write a script that will find doubled words (such as “this
this”) and highlight (wrap in brackets) each doubled word.
The script should:
- Work across lines, even finding situations where a word at
the end of one line is repeated at the beginning of the
next.
- Find doubled words despite capitalization differences,
such as with 'The the...', as well as allow differing
amounts of whitespace (spaces, tabs, newlines, and the
like) to lie between the words.
- Find doubled words even when separated by HTML tags. For
example, to make a word bold: '...it is <B>very</B> very
important...'. Only show lines containing doubled words.
Adapted from Mastering Regular Expressions, Third Edition by Jeffrey E. F. Friedl
Example 1
Input: $str = "you're given the job of checking the pages on a\nweb server for doubled words (such as 'this this'), a common problem\nwith documents subject to heavy editing."
Output: "web server for doubled words (such as '[this] [this]'), a common problem"
Example 2
Input: $str = "Find doubled words despite capitalization differences, such as with 'The\nthe...', as well as allow differing amounts of whitespace (spaces,\ntabs, newlines, and the like) to lie between the words."
Output: "Find doubled words despite capitalization differences, such as with '[The]\n[the]...', as well as allow differing amounts of whitespace (spaces,"
Example 3
Input: $str = "to make a word bold: '...it is <B>very</B> very important...'."
Output: "to make a word bold: '...it is <B>[very]</B> [very] important...'."
Example 4
Input: $str = "Perl officially stands for Practical Extraction and Report Language, except when it doesn't."
Output: ""
Example 5
Input: $str = "There's more than one one way to do it.\nEasy things should be easy and hard things should be possible."
Output: "There's more than [one] [one] way to do it."
I split the input using a capturing pattern, so I produce
an array with words and their separators: html tags, special
characters, spaces or punctuation. I can highlight
adjacent repeated words (from the examples, I guess repeated
words that are not adjacent are not to be highlighted) and
join the array to build the output. Finally, I print the
lines that contain highlighted words.
1 # Perl weekly challenge 376
2 # Task 2: Doubled Words
3 #
4 # See https://wlmb.github.io/2026/06/01/PWC376/#task-2-doubled-words
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to find, highlight and print doubled words in the string Sn
9 FIN
10 for(@ARGV){
11 my @parts=split / # split on word separators
12 ( # capture
13 \s* # leading spaces
14 ( # group
15 <\/?\w[^>]*?> # html tag
16 |
17 &.+?; # html special character
18 |
19 \s # space
20 |
21 [[:punct:]] # punctuation
22 )+ # arbitrarily repeated
23 \s* # trailing space
24 )
25 /xx; # extended re syntax
26 # As there are two capture groups, @parts contains word, sep, sep, word, sep, sep...
27 for(grep{$_%3==0} 0..@parts-4){ # for the actual words
28 next unless (lc $parts[$_] eq lc $parts[$_+3]); # check doubled words
29 $parts[$_]="[$parts[$_]]"; # and higlight them
30 $parts[$_+3]="[$parts[$_+3]]";
31 }
32
33 my @lines =
34 grep {/\[/}
35 split "\n",
36 join "", @parts[
37 grep{$_%3!=2}0..@parts-1
38 ],
39 "\n";
40 say "$_\n->";
41 say $_ for @lines;
42 say "---";
43 }
Example:
./ch-2.pl \
"you're given the job of checking the pages on a
web server for doubled words (such as 'this this'), a common problem
with documents subject to heavy editing." \
"Find doubled words despite capitalization differences, such as with 'The
the...', as well as allow differing amounts of whitespace (spaces,
tabs, newlines, and the like) to lie between the words." \
"to make a word bold: '...it is <B>very</B> very important...'." \
"Perl officially stands for Practical Extraction and Report Language, except when it doesn't." \
"There's more than one one way to do it.
Easy things should be easy and hard things should be possible."
Results:
you're given the job of checking the pages on a
web server for doubled words (such as 'this this'), a common problem
with documents subject to heavy editing.
->
web server for doubled words (such as '[this] [this]'), a common problem
---
Find doubled words despite capitalization differences, such as with 'The
the...', as well as allow differing amounts of whitespace (spaces,
tabs, newlines, and the like) to lie between the words.
->
Find doubled words despite capitalization differences, such as with '[The]
[the]...', as well as allow differing amounts of whitespace (spaces,
---
to make a word bold: '...it is <B>very</B> very important...'.
->
to make a word bold: '...it is <B>[very]</B> [very] important...'.
---
Perl officially stands for Practical Extraction and Report Language, except when it doesn't.
->
---
There's more than one one way to do it.
Easy things should be easy and hard things should be possible.
->
There's more than [one] [one] way to do it.
---
/;