Camel
Peter
Peter Campbell Smith

Lies and lies within

Weekly challenge 363 — 2 March 2026

Week 363: 2 Mar 2026

Task 1

Task — String lie detector

You are given a string. Write a script that parses a self-referential string and determines whether its claims about itself are true. The string will make statements about its own composition, specifically the number of vowels and consonants it contains.

Examples


Example 1
Input: $str = 'aa - two vowels and zero consonants'
Output: true

Example 2
Input: $str = 'iv - one vowel and one consonant'
Output: true

Example 3
Input: $str = 'hello - three vowels and two consonants'
Output: false

Example 4
Input: $str = 'aeiou - five vowels and zero consonants'
Output: true

Example 5
Input: $str = 'aei - three vowels and zero consonants'
Output: true

Analysis

Examination of the examples shows that the string is in two parts separated by a dash, which I've called the subject and the claim. The claim reports - correctly or not - the composition of the subject in terms of numbers of vowels and consonants.

For last week's challenge I discovered the module Lingua::EN and various modules in that family that translate between a number, eg 12, and its English representation, eg twelve*. I've used these again this week.

So the solution has two parts. Part 1 counts the number of vowels and consonants in the subject using the tr||| construct. Part 2 looks at the claim and uses words2num() from the Lingua module to convert the relevant words to numbers.

In presenting the answers I use the - somewhat unusual - Lingua feature which delivers text from numbers as $text = $N{$number}.

* See my answer to challenge 362/2 for my module-free alternative for number to words conversion.

Something different

My initial but incorrect reading of the challenge was that the string was to be truly self-referential, so that the number of vowels and consonants referred to the entire string. This led me to search for any 'claim' strings in the format used in this challenge that meet that criterion, and I found only three:

  • nine vowels and twenty consonants
  • ten vowels and twenty-one consonants
  • ten vowels and twenty-three consonants

Try it 

Try running the script with any input:



example: challenge - three vowels and six consonants

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-03-02
use utf8;     # Week 363 - task 1 - String lie detector
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;
use Lingua::EN::Words2Nums;
use Lingua::EN::Numbers::Easy;

string_lie_detector('aa — two vowels and zero consonants');
string_lie_detector('hello - three vowels and two consonants');
string_lie_detector('aei — three vowels and zero consonants');
string_lie_detector('perl weekly challenge - six vowels and thirteen consonants');
string_lie_detector('false example - zero vowels and zero consonants');
string_lie_detector('another example - eight consonants and six vowels');
string_lie_detector('- zero vowels and zero consonants');
string_lie_detector('supercalifragilisticexpialidocious — ' .
    'sixteen vowels and eighteen consonants');
    
sub string_lie_detector {
    
    my ($string, $claims, $subject, $v1, $c1, $v2, $c2);
    
    # initialise
    $string = $_[0];
    ($subject, $claims) = $string =~ m|(.*)\s*[-—]\s*(.*)|i;
    unless (defined $subject and defined $claims) {
        say qq[\ninvalid input];
        return;
    }
    
    # analyse subject
    $v1 = $subject =~ tr|aeiou||;
    $c1 = $subject =~ tr|bcdfghjklmnpqrstvwxyz||;
    
    # analyse claims
    $v2 = $c2 = 0;
    $v2 = words2nums($1) if $claims =~ m|([a-z]+)\s+vowel|i;
    $c2 = words2nums($1) if $claims =~ m|([a-z]+)\s+consonant|i;
    
    # report
    say qq[\nInput:  '$string'];
    say qq[Output: ] . ($v1 == $v2 && $c1 == $c2 ? 
        'true' : qq[false - $N{$v1} vowels and $N{$c1} consonants]);
}

Output


Input:  'aa — two vowels and zero consonants'
Output: true

Input:  'hello - three vowels and two consonants'
Output: false - two vowels and three consonants

Input:  'aei — three vowels and zero consonants'
Output: true

Input:  'perl weekly challenge - six vowels and thirteen 
   consonants'
Output: true

Input:  'false example - zero vowels and zero consonants'
Output: false - five vowels and seven consonants

Input:  'another example - eight consonants and six 
   vowels'
Output: true

Input:  '- zero vowels and zero consonants'
Output: true

Input:  'supercalifragilisticexpialidocious - sixteen 
   vowels and eighteen consonants'
Output: true

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain