The Weekly Challenge - 190

TASK #1: Capital Detection
You are given a string with alphabetic characters only: A..Z and a..z.
Write a script to find out if the usage of Capital is appropriate if it
satisfies at least one of the following rules:
1) Only first letter is capital and all others are small.
2) Every letter is small.
3) Every letter is capital.

#!/usr/bin/perl
use strict;
use warnings;

sub capital_detection {
    my ($str) = @_;

    # Die if any character is not A-Z or a-z

    die "Invalid string: contains non-letter characters\n" unless $str =~ /^[A-Za-z]+$/;
    
    # Check if string is:
    # - all uppercase: [A-Z]+
    # - all lowercase: [a-z]+
    # - capitalized:   [A-Z][a-z]+
    # Anchors ^ and $ ensure the whole string matches
    # (?:...) is a non-capturing group (no need to store the match)
    # Returns 1 if true, 0 otherwise
    
    return ($str =~ /^(?:[A-Z]+|[a-z]+|[A-Z][a-z]+)$/) ? 1 : 0;
}


# Tests

# Example 1
print capital_detection("Perl"), "\n"; # Output: 1

# Example 2
print capital_detection("TPF"), "\n"; # Output: 1

# Example 3
print capital_detection("PyThon"), "\n"; # Output: 0

# Example 4
print capital_detection("raku"), "\n"; # Output: 1