Peter
Peter Campbell Smith

Four is magic and
equilibrium indices

Weekly challenge 160 — 11 April 2022

Week 160 - 11 Apr 2022

Task 1

Task — Four is magic

You are given a positive number, $n < 10. Write a script to generate the English text sequence starting with the English cardinal representation of the given number, the word ‘is’ and then the English cardinal representation of the count of characters that made up the first word, followed by a comma. Continue until you reach four.

Examples


Example 1:
Input: $n = 5
Output: Five is four, four is magic.

Analysis

Not much to say about this, really. Someone will have a one-line answer, but as usual I prefer readability to conciseness. For fun, I took it up to 20.

It's a little hard to see what practical use this might be!

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2022-03-30
# PWC 158 task 1

use v5.28;
use strict;
use warnings;
use utf8;

my (@numbers, $n, $string, $length, $k, $this, $next);

@numbers = qw(x one two three four five six seven eight 
    nine ten eleven twelve thirteen fourteen fifteen 
    sixteen seventeen eighteen nineteen twenty);

for $n (1 .. $#numbers) {
    $string = '';
    
    # do what the task says until we reach 4
    $this = $n;
    for $k (1 .. 20) {
        $next = length($numbers[$this]);
        $string .= qq[$numbers[$this] is $numbers[$next], ];
        last if $next == 4;
        $this = $next;
    }
    $string = ucfirst($string);
    say qq[\nInput:  \$n = $n\nOutput: ${string}four is magic.];
}

Output


Input:  $n = 1
Output: One is three, three is five, five is four, 
   four is magic.

Input:  $n = 2
Output: Two is three, three is five, five is four, 
   four is magic.

Input:  $n = 3
Output: Three is five, five is four, four is magic.

Input:  $n = 4
Output: Four is four, four is magic.

Input:  $n = 5
Output: Five is four, four is magic.

Input:  $n = 6
Output: Six is three, three is five, five is four, 
   four is magic.

Input:  $n = 7
Output: Seven is five, five is four, four is magic.

Input:  $n = 8
Output: Eight is five, five is four, four is magic.

Input:  $n = 9
Output: Nine is four, four is magic.

Input:  $n = 10
Output: Ten is three, three is five, five is four, 
   four is magic.

Input:  $n = 11
Output: Eleven is six, six is three, three is five, 
   five is four, four is magic.

Input:  $n = 12
Output: Twelve is six, six is three, three is five, 
   five is four, four is magic.

Input:  $n = 13
Output: Thirteen is eight, eight is five, five is four, 
   four is magic.

Input:  $n = 14
Output: Fourteen is eight, eight is five, five is four, 
   four is magic.

Input:  $n = 15
Output: Fifteen is seven, seven is five, five is four, 
   four is magic.

Input:  $n = 16
Output: Sixteen is seven, seven is five, five is four, 
   four is magic.

Input:  $n = 17
Output: Seventeen is nine, nine is four, four is magic.

Input:  $n = 18
Output: Eighteen is eight, eight is five, five is four, 
   four is magic.

Input:  $n = 19
Output: Nineteen is eight, eight is five, five is four, 
   four is magic.

Input:  $n = 20
Output: Twenty is six, six is three, three is five, 
   five is four, four is magic.

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