Task 1: String Lie Detector
"The Truth About Words: Can They Lie About Themselves?"
This week's first task involves parsing self-referential strings that make claims about their own composition. We need to verify whether the number of vowels and consonants mentioned in the string actually matches the content.
The Strategy: Parse the string to extract the subject and the claims. Count the actual vowels and consonants in the subject, then compare with the claimed values. Handle both word numbers (like "two") and digit numbers.
Perl Implementation
sub string_lie_detector ($text) {
my ( $subject, $vowel_token, $consonant_token ) =
$text =~ /^\s*(.*?)\s*[—-]\s*(\w+)\s+vowels?\s+and\s+(\w+)\s+consonants?\s*$/i;
return 0 if !defined $subject;
my $claimed_vowels = _parse_count($vowel_token);
my $claimed_consonants = _parse_count($consonant_token);
return 0 if !defined($claimed_vowels) || !defined($claimed_consonants);
my @letters = grep { /[A-Za-z]/ } split //, $subject;
my $vowels = scalar grep { /[AEIOU]/i } @letters;
my $consonants = scalar(@letters) - $vowels;
return ( $vowels == $claimed_vowels && $consonants == $claimed_consonants ) ? 1 : 0;
}
Python Implementation
def string_lie_detector(text: str) -> bool:
"""
Parse self-referential string and check if vowel/consonant claims are true.
"""
import re
match = re.match(
r'^\s*(.*?)\s*[—-]\s*(\w+)\s+vowels?\s+and\s+(\w+)\s+consonants?\s*$',
text, re.IGNORECASE
)
if not match:
return False
subject, vowel_token, consonant_token = match.groups()
claimed_vowels = _parse_count(vowel_token)
claimed_consonants = _parse_count(consonant_token)
if claimed_vowels is None or claimed_consonants is None:
return False
letters = [c for c in subject if c.isalpha()]
vowels = sum(1 for c in letters if c.upper() in 'AEIOU')
consonants = len(letters) - vowels
return vowels == claimed_vowels and consonants == claimed_consonants