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

Published on 12 May 2024 09:40 PM

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

Source code in the basic256 programming language

dim names$(1)
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" }

global names$, lnames, index, maxlen, first, last
maxlen = 0

lnames = names$[?]-1
dim index(names$[?])
dim first(names$[?])
dim last(names$[?])
for t = 0 to lnames
	index[t] = t
	last[t] = asc(right(names$[t],1))
	first[t] = asc(left(names$[t],1))
next t


# try each name as the first name on the list
for t = 0 to lnames
   call swapindex(0,t)
   call downlevel(1)
   call swapindex(0,t)
next t

end

subroutine downlevel(lev)
   #print n$[?] + " " + lev
   if lev <= lnames then
      for t = lev to lnames
         if last[index[lev-1]] = first[index[t]] then
            call swapindex(lev,t)
            if lev >= maxlen then
               maxlen = lev
               call showsolution(lev)
            end if
            call downlevel(lev+1)
            call swapindex(lev,t)
         end if
      next t
   end if
end subroutine

subroutine showsolution(l)
   print l+1;
   for t = 0 to l
      print " " + names$[index[t]];
   next t
   print
end subroutine

subroutine swapindex(a, b)
   # swap element a and bin in the array index (used to swap names$)
   t = index[a]
   index[a] = index[b]
   index[b] = t
end subroutine

  

You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the jq programming language
You may also check:How to resolve the algorithm A+B step by step in the Klong programming language
You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Wren programming language
You may also check:How to resolve the algorithm Factorial step by step in the Self programming language