Camel
Peter
Peter Campbell Smith

Root and game

Weekly challenge 105 — 22 March 2021

Week 105: 22 Mar 2021

Task 2

Task — The name game

You are given a $name.

Write a script to display the lyrics to the Shirley Ellis song The Name Game. Please checkout the Wikipedia page for more information.

Examples


Example 1
Input: $name = 'Katie'
Output:
    Katie, Katie, bo-batie,
    Bonana-fanna fo-fatie
    Fee fi mo-matie
    Katie!

Analysis

Well, this is a little unusual - but easily solved.

And you can hear it here.

Try it 

Try running the script with any input:



example: Susie

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2021-03-22
use utf8;     # Week 105 - task 2 - The name game
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

the_name_game('Katie');
the_name_game('Bobby');
the_name_game('Peter');
the_name_game('Johnny');

sub the_name_game {
    
    my ($pattern, $pattern1, $pattern2, $x, $y);
    
    # initialise
    $x = $_[0];
    
    $pattern1 = q[    (X), (X), bo-b(Y)
    Bonana-fanna fo-f(Y)
    Fee fi mo-m(Y)
    (X)!];
    $pattern2 = q[    (X), (X), bo-(Y)
    Bonana-fanna fo-(Y)
    Fee fi mo-(Y)
    (X)!];  

    # apply rules
    $y = substr($x, 1, 99);
    $pattern = ($x =~ m|^[bfm]|i ? $pattern2 : $pattern1);
    $pattern =~ s|\(X\)|$x|g;
    $pattern =~ s|\(Y\)|$y|g;
    
    say qq[\nInput:  \$name = '$x'];
    say qq[Output: \n$pattern];
}

17 lines of code

I completed this challenge after the closing date
and it has not been submitted to GitHub

Output


Input:  $name = 'Katie'
Output: 
    Katie, Katie, bo-batie
    Bonana-fanna fo-fatie
    Fee fi mo-matie
    Katie!

Input:  $name = 'Bobby'
Output: 
    Bobby, Bobby, bo-obby
    Bonana-fanna fo-obby
    Fee fi mo-obby
    Bobby!

Input:  $name = 'Peter'
Output: 
    Peter, Peter, bo-beter
    Bonana-fanna fo-feter
    Fee fi mo-meter
    Peter!

Input:  $name = 'Johnny'
Output: 
    Johnny, Johnny, bo-bohnny
    Bonana-fanna fo-fohnny
    Fee fi mo-mohnny
    Johnny!

 

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