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

Published on 12 May 2024 09:40 PM

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

Source code in the 11l programming language

F order_words(words)
   DefaultDict[Char, Set[String]] byfirst
   L(word) words
      byfirst[word[0]].add(word)
   R byfirst

F linkfirst(&byfirst; sofar)
   assert(!sofar.empty)
   V chmatch = sofar.last.last
   V options = byfirst[chmatch] - Set(sofar)

   I options.empty
      R sofar
   E
      V alternatives = options.map(word -> linkfirst(&@byfirst, @sofar [+] [word]))
      R max(alternatives, key' s -> s.len)

F llfl(words)
   V byfirst = order_words(words)
   R max((words.map(word -> linkfirst(&@byfirst, [word]))), key' s -> s.len)

V pokemon_str = ‘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’
V pokemon = pokemon_str.split((‘ ’, "\n"))
V l = llfl(pokemon)
L(i) (0 .< l.len).step(8)
   print(l[i .< i + 8].join(‘ ’))
print(l.len)

  

You may also check:How to resolve the algorithm Read entire file step by step in the Phix programming language
You may also check:How to resolve the algorithm Array length step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the R programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Oz programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the 68000 Assembly programming language