How to resolve the algorithm The Name Game step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm The Name Game step by step in the Picat 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 Picat programming language

Source code in the picat programming language

print_name_game(Name), Name = [V|Rest], membchk(V, ['A', 'E', 'I', 'O', 'U'])  =>
    L = to_lowercase(V),
    Low = [L|Rest],
    print_verse(Name, [b|Low], [f|Low], [m|Low]).

print_name_game(Name), Name = ['B'|Rest] =>
    print_verse(Name, Rest, [f|Rest], [m|Rest]).

print_name_game(Name), Name = ['F'|Rest] =>
    print_verse(Name, [b|Rest], Rest, [m|Rest]).

print_name_game(Name), Name = ['M'|Rest] =>
    print_verse(Name, [b|Rest], [f|Rest], Rest).

print_name_game(Name), Name = [C|Rest] =>
    print_verse(Name, [b|Rest], [f|Rest], [m|Rest]).

print_verse(Full, B, F, M) =>
    printf("%w, %w, bo-%w\nBanana-fana fo-%w\nFee-fi-mo-%w\n%w!\n\n",
        Full,
        Full,
        B,
        F,
        M,
        Full
    ).

main(Args) =>
    foreach (Name in Args)
        print_name_game(Name)
    end.

  

You may also check:How to resolve the algorithm Ternary logic step by step in the SparForte programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Hope programming language
You may also check:How to resolve the algorithm Mutex step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the REXX programming language