Clearly Capital
with Raku

by Arne Sommer

Clearly Capital with Raku

[352] Published 18. July 2025.

This is my response to The Weekly Challenge #330.

Challenge #330.1: Clear Digits

You are given a string containing only lower case English letters and digits.

Write a script to remove all digits by removing the first digit and the closest non-digit character to its left.

Example 1:
Input: $str = "cab12"
Output: "c"

Round 1: remove "1" then "b" => "ca2"
Round 2: remove "2" then "a" => "c"
Example 2:
Input: $str = "xy99"
Output: ""

Round 1: remove "9" then "y" => "x9"
Round 2: remove "9" then "x" => ""
Example 3:
Input: $str = "pa1erl"
Output: "perl"

File: clear-digits
#! /usr/bin/env raku

unit sub MAIN ($str where $str ~~ /^<[ a..z 0 .. 9 ]>+$/,  # [1]
               :v(:$verbose));

my @output;                                                # [2]

for $str.comb -> $current                                  # [3]
{
  if $current eq any(0..9)                                 # [4]
  {
    @output.pop;                                           # [4a]
    say ": Digit $current; Remove letter -> { @output.join}" if $verbose;
  }
  else                                                     # [5]
  {
    @output.push: $current;                                # [5a]
    say ": Letter $current; Added -> { @output.join}" if $verbose;
  }
}

say @output.join;                                          # [6]

[1] Ensure an input string containg lowercase letters and digits only, with at least one character.

[2] The result will end up here, as a list of single characters.

[3] Iterate over each character in the input.

[4] Is it a digit? If so, get rid of the previous character (already part of the result, which is either a letter or an empty string) with pop.

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

[5] It is not a digit, thus it is a letter and we add it to the result with push.

[6] Print the result as a string.

Running it:

$ ./clear-digits cab12
c

$ ./clear-digits xy99

$ ./clear-digits pa1erl
perl

Looking good.

With verbose mode:

$ ./clear-digits -v cab12
: Letter c; Added -> c
: Letter a; Added -> ca
: Letter b; Added -> cab
: Digit 1; Remove letter -> ca
: Digit 2; Remove letter -> c
c

$ ./clear-digits -v xy99
: Letter x; Added -> x
: Letter y; Added -> xy
: Digit 9; Remove letter -> x
: Digit 9; Remove letter -> 

$ ./clear-digits -v pa1erl
: Letter p; Added -> p
: Letter a; Added -> pa
: Digit 1; Remove letter -> p
: Letter e; Added -> pe
: Letter r; Added -> per
: Letter l; Added -> perl
perl

Challenge #330.2: Title Capital

You are given a string made up of one or more words separated by a single space.

Write a script to capitalise the given title. If the word length is 1 or 2 then convert the word to lowercase otherwise make the first character uppercase and remaining lowercase.

Example 1:
Input: $str = "PERL IS gREAT"
Output: "Perl is Great"
Example 2:
Input: $str = "THE weekly challenge"
Output: "The Weekly Challenge"
Example 3:
Input: $str = "YoU ARE A stAR"
Output: "You Are a Star"
File: title-capital
#! /usr/bin/env raku

unit sub MAIN ($str where $str.chars > 0,   # [1]
               :v(:$verbose));

say $str.words.map( *.&tica ).join(" ");    # [2]

sub tica ($word)                            # [3]
{
  say ": Word: $word" if $verbose;

  return $word.lc if $word.chars <= 2;      # [4]
  return $word.lc.tc;                       # [5]
}

[1] A string with at least one character.

[2] Use words to split the sentence into words, or rather what Raku regards as words. Then we apply the "tica" procedure on each word with map, and note the .& syntax used to invoke a procedure as if it were a method. Then we join the parts together (as space separated words) and print the result.

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

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

[3] The magic happens here.

[4] Return the lowercase version of the word (with {{C.lc}}) if the length is 1 or 2.

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

[5] Return the titlecase version of the word (with tc). This will uppercvase the first letter and lowercase the rest.

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

Running it:

$ ./title-capital "PERL IS gREAT"
Perl is Great

$ ./title-capital  "THE weekly challenge"
The Weekly Challenge

$ ./title-capital  "YoU ARE A stAR"
You Are a Star

Looking good.

With verbose mode:

$ ./title-capital -v "PERL IS gREAT"
:PERL
:IS
:gREAT
Perl is Great

$ ./title-capital -v "THE weekly challenge"
:THE
:weekly
:challenge
The Weekly Challenge

$ ./title-capital  -v "YoU ARE A stAR"
:YoU
:ARE
:A
:stAR
You Are a Star

And that's it.