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

Published on 12 May 2024 09:40 PM

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

Source code in the tcl programming language

proc search {path arcs} {
    set solutions {}
    set c [string index [lindex $path end] end]
    set i -1
    foreach arc $arcs {
	incr i
	if {[string index $arc 0] ne $c} continue
	set soln [search [concat $path [list $arc]] [lreplace $arcs $i $i]]
	lappend solutions [list [llength $soln] $soln]
    }
    if {[llength $solutions]} {
	return [lindex [lsort -integer -decreasing -index 0 $solutions] 0 1]
    } else {
	return $path
    }
}
proc firstlast names {
    set solutions {}
    set i -1
    foreach initial $names {
	incr i
	set soln [search [list $initial] [lreplace $names $i $i]]
	lappend solutions [list [llength $soln] $soln]
    }
    return [lindex [lsort -integer -decreasing -index 0 $solutions] 0 1]
}

set names {
    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
}
set path [firstlast $names]
puts "Path (length: [llength $path]): $path"


  

You may also check:How to resolve the algorithm Camel case and snake case step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the BASIC programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Delphi programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Action! programming language