Max Validate
with Raku

by Arne Sommer

Max Validate with Raku

[377] Published 27. December 2025.

This is my response to The Weekly Challenge #353.

Challenge #353.1: Max Words

You are given an array of sentences.

Write a script to return the maximum number of words that appear in a single sentence.

Example 1:
Input: @sentences = ("Hello world", "This is a test", "Perl is great")
Output: 4
Example 2:
Input: @sentences = ("Single")
Output: 1
Example 3:
Input: @sentences = ("Short", "This sentence has seven words in total", \
                     "A B C", "Just four words here")
Output: 7
Example 4:
Input: @sentences = ("One", "Two parts", "Three part phrase", "")
Output: 3
Example 5:
Input: @sentences = ("The quick brown fox jumps over the lazy dog", \
                     "A", "She sells seashells by the seashore", \
                     "To be or not to be that is the question")
Output: 10
File: max-words
#! /usr/bin/env raku

unit sub MAIN (*@sentences where @sentences.elems > 0);  # [1]

@sentences>>.words>>.elems.max.say;
# [2]       [3]     [4]   [5] [6] 

[1] A slurpy array for the sentences.

[2] Start with the sentences,

[3] and split all of them into words.

See docs.raku.org/routine/words for more information about words.

[4] Get the number of elements (words) in each sentence,

[5] and get the higheste one.

[6] Print the result.

Running it:

$ ./max-words "Hello world" "This is a test" "Perl is great"
4

$ ./max-words "Single"
1

$ ./max-words "Short" "This sentence has seven words in total" \
    "A B C" "Just four words here"
7

$ ./max-words "One" "Two parts" "Three part phrase" ""
3

$ ./max-words "The quick brown fox jumps over the lazy dog" \
    "A" "She sells seashells by the seashore" \
    "To be or not to be that is the question"
10

Looking good.

Challenge #353.2: Validate Coupon

You are given three arrays, @codes, @names and @status.

Write a script to validate codes in the given array.

A code is valid when the following conditions are true:
  • codes[i] is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (_)
  • names[i] is one of the following four categories: "electronics", "grocery", "pharmacy", "restaurant"
  • status[i] is true
Return an array of booleans indicating validity: output[i] is true if and only if codes[i], names[i] and status[i] are all valid.

Example 1:
Input: @codes  = ("A123", "B_456", "C789", "D@1", "E123")
       @names  = ("electronics", "restaurant", "electronics", \
                  "pharmacy", "grocery")
       @status = ("true", "false", "true", "true", "true")
Output: (true, false, true, false, true)
Example 2:
Input: @codes  = ("Z_9", "AB_12", "G01", "X99", "test")
       @names  = ("pharmacy", "electronics", "grocery", "electronics", \
                  "unknown")
       @status = ("true", "true", "false", "true", "true")
Output: (true, true, false, true, false)
Example 3:
Input: @codes  = ("_123", "123", "", "Coupon_A", "Alpha")
       @names  = ("restaurant", "electronics", "electronics", \
                  "pharmacy", "grocery")
       @status = ("true", "true", "false", "true", "true")
Output: (true, true, false, true, true)
Example 4:
Input: @codes  = ("ITEM_1", "ITEM_2", "ITEM_3", "ITEM_4")
       @names  = ("electronics", "electronics", "grocery", "grocery")
       @status = ("true", "true", "true", "true")
Output: (true, true, true, true)
Example 5:
Input: @codes  = ("CAFE_X", "ELEC_100", "FOOD_1", "DRUG_A", "ELEC_99")
       @names  = ("restaurant", "electronics", "grocery", "pharmacy", \
                  "electronics")
       @status = ("true", "true", "true", "true", "false")
Output: (true, true, true, true, false)
File: validate-coupon
#! /usr/bin/env raku

unit sub MAIN ($codes, $names, $status, :v(:$verbose));        # [1]

my @codes  = $codes.words;                                     # [2]
my @names  = $names.words;                                     # [3]
my @status = $status.words;                                    # [4]
my @result;                                                    # [5]

die "Uneven length"
  unless @codes.elems == @names.elems == @status.elems;        # [6]

for ^@codes -> $i                                              # [7]
{
  my $ok-code   = so @codes[$i] ~~ /^<[a..z A..Z 0..9 _]>+$/;  # [8]
  my $ok-name   = so @names[$i] eq
       any <electronics grocery pharmacy restaurant>;          # [9]

  my $ok-status = so @status[$i] eq 'true';                    # [10]
  my $ok        = so $ok-code && $ok-name && $ok-status;       # [11]

  say ": Index:$i Code:$ok-code Name:$ok-name Status:$ok-status -> $ok"
    if $verbose;

  @result.push: $ok;                                           # [12]
}

say "({ @result.join(", ") })";                                # [13]

[1] The three arrays, each one as a space separated string of words.

[2] Extract the codes,

[3] the names,

[4] and the statuses.

[5] The result will end up here.

[6] The three arrays must have the same length. Protest if they are not.

[7] Iterate over the indices in the arrays.

[8] Do we have a valid code? Note the Boolean coercer so, used to make the verbose ouput nicer.

See docs.raku.org/routine/so for more information about so.

[9] A valid name?

[10] And a valid status?

[11] The coupon is valid if all three conditions ([8], [9] and [10]) are satisfied.

[12] Add the validity (true/false) to the result.

[13] Pretty print the result.

Running it:

$ ./validate-coupon "A123 B_456 C789 D@1 E123" \
    "electronics restaurant electronics pharmacy grocery" \
    "true false true true true"
(True, False, True, False, True)

$ ./validate-coupon "Z_9 AB_12 G01 X99 test" \
    "pharmacy electronics grocery electronics unknown" \
    "true true false true true"
(True, True, False, True, False)

$ ./validate-coupon "_123 123 @ Coupon_A Alpha" \
    "restaurant electronics electronics pharmacy grocery" \
    "true true false true true"
(True, True, False, True, True)

$ ./validate-coupon "ITEM_1 ITEM_2 ITEM_3 ITEM_4" \
    "electronics electronics grocery grocery" "true true true true"
(True, True, True, True)

$ ./validate-coupon "CAFE_X ELEC_100 FOOD_1 DRUG_A ELEC_99" \
    "restaurant electronics grocery pharmacy electronics" \
    "true true true true false"
(True, True, True, True, False)

Looking good.

With verbose mode:

$ ./validate-coupon -v "A123 B_456 C789 D@1 E123" \
    "electronics restaurant electronics pharmacy grocery" \
    "true false true true true"
: Index:0 Code:True Name:True Status:True -> True
: Index:1 Code:True Name:True Status:False -> False
: Index:2 Code:True Name:True Status:True -> True
: Index:3 Code:False Name:True Status:True -> False
: Index:4 Code:True Name:True Status:True -> True
(True, False, True, False, True)

$ ./validate-coupon -v "Z_9 AB_12 G01 X99 test" \
    "pharmacy electronics grocery electronics unknown" \
    "true true false true true"
: Index:0 Code:True Name:True Status:True -> True
: Index:1 Code:True Name:True Status:True -> True
: Index:2 Code:True Name:True Status:False -> False
: Index:3 Code:True Name:True Status:True -> True
: Index:4 Code:True Name:False Status:True -> False
(True, True, False, True, False)

$ ./validate-coupon -v "_123 123 @ Coupon_A Alpha" \
    "restaurant electronics electronics pharmacy grocery" \
    "true true false true true"
: Index:0 Code:True Name:True Status:True -> True
: Index:1 Code:True Name:True Status:True -> True
: Index:2 Code:False Name:True Status:False -> False
: Index:3 Code:True Name:True Status:True -> True
: Index:4 Code:True Name:True Status:True -> True
(True, True, False, True, True)

$ ./validate-coupon -v "ITEM_1 ITEM_2 ITEM_3 ITEM_4" "\
    electronics electronics grocery grocery" "true true true true"
: Index:0 Code:True Name:True Status:True -> True
: Index:1 Code:True Name:True Status:True -> True
: Index:2 Code:True Name:True Status:True -> True
: Index:3 Code:True Name:True Status:True -> True
(True, True, True, True)

$ ./validate-coupon -v "CAFE_X ELEC_100 FOOD_1 DRUG_A ELEC_99" \
    "restaurant electronics grocery pharmacy electronics" \
    "true true true true false"
: Index:0 Code:True Name:True Status:True -> True
: Index:1 Code:True Name:True Status:True -> True
: Index:2 Code:True Name:True Status:True -> True
: Index:3 Code:True Name:True Status:True -> True
: Index:4 Code:True Name:True Status:False -> False
(True, True, True, True, False)

And that's it.