How to resolve the algorithm Last letter-first letter step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Last letter-first letter step by step in the Julia 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 Julia programming language

The provided Julia code deals with the task of finding the longest linked list that can be formed by connecting words where the last character of a word matches the first character of the next word. Here's a breakdown of the code and its functionality:

  1. orderwords Function:

    • It takes a vector of words as input and creates a dictionary where the keys are the first characters of the words, and the values are sets of words that start with that character. This dictionary helps in efficiently finding words that can be linked based on their first and last characters.
  2. linkfirst Function:

    • This function takes two arguments: byfirst, which is the dictionary created by orderwords, and sofar, which is a vector of words that have been linked so far.
    • It starts by asserting that sofar is not empty.
    • Then, it gets the last character of the last word in sofar (chmatch).
    • If chmatch is not a key in byfirst, it means there are no more words that can be linked, so the function returns sofar.
    • If there are possible continuations, it computes the set of options as the set difference between the set of words starting with chmatch (from byfirst) and the words already in sofar.
    • If there are no options, the function returns sofar.
    • If there are options, it recursively calls itself for each option, appending the option to sofar, and stores the results in alternatives.
    • Finally, it reduces alternatives using the longest function to find the longest linked list.
  3. longest Function:

    • This is a helper function that takes two vectors and returns the longer one. It is used to find the longest linked list among the alternatives generated by linkfirst.
  4. llfl Function:

    • This function takes a vector of words as input.
    • It first calls orderwords to create the byfirst dictionary.
    • Then, it generates a set of alternatives by calling linkfirst with each word in the input vector as the starting word.
    • Finally, it uses the longest function to find the longest linked list among the alternatives and returns it.
  5. Usage Example:

    • The code includes an example that uses a vector of Pokemon names as input and calls the llfl function on it.
    • It prints out an example of the longest linked list found and its length.

In summary, this code finds the longest linked list of words where the last character of each word matches the first character of the next word. It does this by using a dictionary to efficiently find possible continuations and then recursively exploring all options to find the longest one.

Source code in the julia programming language

using IterTools.groupby

orderwords(words::Vector) = Dict(w[1][1] => Set(w) for w in groupby(first, words))
longest(a, b) = ifelse(length(a) > length(b), a, b)
function linkfirst(byfirst::Dict, sofar::Vector)
    @assert(!isempty(sofar))
    chmatch = sofar[end][end]
    if ! haskey(byfirst, chmatch) return sofar end
    options = setdiff(byfirst[chmatch], sofar)
    if isempty(options)
        return sofar
    else
        alternatives = ( linkfirst(byfirst, vcat(sofar, word)) for word in options )
        mx = reduce(longest, alternatives)
        return mx
    end
end
function llfl(words)
    byfirst = orderwords(words)
    alternatives = ( linkfirst(byfirst, [word]) for word in words )
    return reduce(longest, alternatives)
end

pokemon = String.(unique(split("""
    audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon
    cresselia croagunk darmanitan deino emboar emolga exeggcute gabite
    girafarig gulpin haxorus heatmor heatran ivysaur jellicent jumpluff kangaskhan
    kricketune landorus ledyba loudred lumineon lunatone machamp magnezone mamoswine
    nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena porygon2
    porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking
    sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko
    tyrogue vigoroth vulpix wailord wartortle whismur wingull yamask
    """)))

l = llfl(pokemon)
println("Example of longest seq.:\n", join(l, ", "))
println("Max length: ", length(l)


  

You may also check:How to resolve the algorithm Secure temporary file step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Forth programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Erlang programming language
You may also check:How to resolve the algorithm Number names step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm A+B step by step in the NetRexx programming language