How to resolve the algorithm The Name Game step by step in the Julia programming language
How to resolve the algorithm The Name Game step by step in the Julia programming language
Table of Contents
Problem Statement
Write a program that accepts a name as input and outputs the lyrics to the Shirley Ellis song "The Name Game".
The regular verse Unless your name begins with a vowel (A, E, I, O, U), 'B', 'F' or 'M' you don't have to care about special rules. The verse for the name 'Gary' would be like this: At the end of every line, the name gets repeated without the first letter: Gary becomes ary If we take (X) as the full name (Gary) and (Y) as the name without the first letter (ary) the verse would look like this: Vowel as first letter of the name If you have a vowel as the first letter of your name (e.g. Earl) you do not truncate the name. The verse looks like this: 'B', 'F' or 'M' as first letter of the name In case of a 'B', an 'F' or an 'M' (e.g. Billy, Felix, Mary) there is a special rule. The line which would 'rebuild' the name (e.g. bo-billy) is sung without the first letter of the name. The verse for the name Billy looks like this: For the name 'Felix', this would be right:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm The Name Game step by step in the Julia programming language
The provided Julia code is a function called printverse
that takes a name as an argument and prints a custom verse of the children's song "The Name Game." Here's a detailed breakdown of the code:
-
The
printverse
function is defined with one parameter,name
, which is expected to be an abstract string (a string or a subtype of string). -
The first line inside the function creates a new variable
X
by converting the lowercase version of the inputname
to uppercase using theuppercasefirst
function. This effectively capitalizes the first letter of the input name. -
The next line creates a new variable
Y
by extracting a substring fromX
. It checks if the first character ofX
is one of the vowels ('A', 'E', 'I', 'O', or 'U'). If it is,Y
is set to the value ofX
. Otherwise,Y
is set to a substring ofX
starting from the second character. -
The code then creates three more variables:
b
,f
, andm
. Each variable is set to an empty string ("") if the first character ofX
matches its respective consonant ('B', 'F', or 'M'). Otherwise, the corresponding lowercase consonant is assigned to the variable. -
Finally, the function prints a custom verse of the "The Name Game" song, replacing placeholders with the values of
X
,Y
,b
,f
, andm
. The printed verse follows the pattern:$(X), $(X), bo-$b$(Y) Banana-fana fo-$f$(Y) Fee-fi-mo-$m$(Y) $(X)!
This results in a verse like the following, which is customized based on the input name:
Gary, Gary, bo-bary Banana-fana fo-fary Fee-fi-mo-mary Gary!
-
After defining the
printverse
function, the code callsforeach
with theprintverse
function as the first argument and a tuple of names ("gARY", "Earl", "Billy", "Felix", "Mary", "sHIRley") as the second argument. This loop iterates through each name in the tuple and callsprintverse
with each name, effectively printing customized verses for each name.
Source code in the julia programming language
import Compat: uppercasefirst
function printverse(name::AbstractString)
X = uppercasefirst(lowercase(name))
Y = X[1] ∈ ('A', 'E', 'I', 'O', 'U') ? X : SubString(X, 2)
b = X[1] == 'B' ? "" : "b"
f = X[1] == 'F' ? "" : "f"
m = X[1] == 'M' ? "" : "m"
println("""\
$(X), $(X), bo-$b$(Y)
Banana-fana fo-$f$(Y)
Fee-fi-mo-$m$(Y)
$(X)!
""")
return nothing
end
foreach(TheNameGame.printverse, ("gARY", "Earl", "Billy", "Felix", "Mary", "sHIRley"))
You may also check:How to resolve the algorithm Bitmap step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm File modification time step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Perl programming language
You may also check:How to resolve the algorithm SOAP step by step in the Raku programming language