How to resolve the algorithm Last letter-first letter step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Last letter-first letter step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

A certain children's game involves starting with a word in a particular category.   Each participant in turn says a word, but that word must begin with the final letter of the previous word.   Once a word has been given, it cannot be repeated.   If an opponent cannot give a word in the category, they fall out of the game.

For example, with   "animals"   as the category,

Take the following selection of 70 English Pokemon names   (extracted from   Wikipedia's list of Pokemon)   and generate the/a sequence with the highest possible number of Pokemon names where the subsequent name starts with the final letter of the preceding name. No Pokemon name is to be repeated.

Extra brownie points for dealing with the full list of   646   names.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Last letter-first letter step by step in the Icon and Unicon programming language

Source code in the icon programming language

global words

procedure main()
    words := table()
    every word := genwords(&input) do {
        /words[word[1]] := []
        put(words[word[1]], word)
        }
    bP := []
    every p := getPath(!!words,[]) do if *\p > *bP then bP := copy(p)
    write("Longest: ",*bP)
    every writes((!bP||" ")|"\n")
end

procedure getPath(word, p)
    if word == !p then return p
    if /words[word[-1]] then suspend p <- p ||| [word]
    else suspend getPath(!words[word[-1]], p <- p ||| [word])
end

procedure genwords(f)
    while l := !f do
        l ? while tab(upto(&letters)) do suspend tab(many(&letters))\1
end


  

You may also check:How to resolve the algorithm Input loop step by step in the Scala programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Fermat programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the zkl programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Visual Basic .NET programming language