Peter’s blog ✴ Week 385 ✴ 3 August 2026
THE WEEKLY CHALLENGE
Lonely words and (outer)
You are given two sentences. Write a script to return list of all uncommon words, order is not important.
Example 1 Input: $sentence1 = 'apple banana apple' $sentence2 = 'banana orange' Output: ('orange') Example 2 Input: $sentence1 = 'cat dog' $sentence2 = 'bird fish' Output: ('cat', 'dog', 'bird', 'fish') Example 3 Input: $sentence1 = 'the quick brown fox' $sentence2 = 'the quick' Output: ('brown', 'fox') Example 4 Input: $sentence1 = 'hello' $sentence2 = 'hello' Output: () Example 5 Input: $sentence1 = 'blue blue red' $sentence2 = 'red green green yellow' Output: ('yellow')
From the examples I deduce 'uncommon' in this challenge means 'appears only once', and that is easily solved by counting the frequency of each word and reporting on those with frequency 1.
One could suggest that the sample size is rather small to deduce that a word is uncommon, and indeed in Example 4 above, 'hello' is classed as uncommon despite being 100% of the studied sample. But let's not quibble.
Peter gives a very simple yet practical solution for Task 1 of The Weekly Challenge 385 that just takes applying regex iteration (m|([a-z]+)|g) in mere 10 lines of code to obtain word frequencies. The beauty of this implementation is in simple coding approach taken, like converting input to lowercase using lc() for case-insensitive matching and handling empty outputs smoothly with defaults.
This review may cover either or both challenges for this week.
I am sorry that the 'Try it' feature is currently working very slowly or not at all owing to some issue with my web hosting provider.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/385/1 use v5.26; # The Weekly Challenge - 2026-08-03 use utf8; # Week 385 - task 1 - Uncommon words use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; uncommon_words('the cat sat on the mat', 'the dog sat on the rug'); uncommon_words('house', 'house'); uncommon_words('apple banana apple', 'banana orange'); uncommon_words('blue blue red', 'red green green yellow'); sub uncommon_words { my ($words, %freq, $uncommon); # initialise $words = lc(qq[$_[0] $_[1]]); $uncommon = ''; # count frequencies $freq{$1} ++ while $words =~ m|([a-z]+)|g; # check for frequency 1 $uncommon .= ($freq{$_} == 1 ? qq['$_', ] : '') for keys %freq; $uncommon = 'none ' unless $uncommon; say qq[\nInput: \$sentence1 = '$_[0]',]; say qq[ \$sentence2 = '$_[1]']; say qq[Output: ] . substr($uncommon, 0, -2); }
10 lines of code
Input: $sentence1 = 'the cat sat on the mat', $sentence2 = 'the dog sat on the rug' Output: 'cat', 'mat', 'dog', 'rug' Input: $sentence1 = 'house', $sentence2 = 'house' Output: none Input: $sentence1 = 'apple banana apple', $sentence2 = 'banana orange' Output: 'orange' Input: $sentence1 = 'blue blue red', $sentence2 = 'red green green yellow' Output: 'yellow'
Any content of this website which has been created by Peter Campbell Smith is in the public domain