How to resolve the algorithm Topswops step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Topswops step by step in the Lua programming language

Table of Contents

Problem Statement

Topswops is a card game created by John Conway in the 1970's.

Assume you have a particular permutation of a set of   n   cards numbered   1..n   on both of their faces, for example the arrangement of four cards given by   [2, 4, 1, 3]   where the leftmost card is on top. A round is composed of reversing the first   m   cards where   m   is the value of the topmost card. Rounds are repeated until the topmost card is the number   1   and the number of swaps is recorded.

For our example the swaps produce: For a total of four swaps from the initial ordering to produce the terminating case where   1   is on top.

For a particular number   n   of cards,   topswops(n)   is the maximum swaps needed for any starting permutation of the   n   cards.

The task is to generate and show here a table of   n   vs   topswops(n)   for   n   in the range   1..10   inclusive.

Topswops   is also known as   Fannkuch   from the German word   Pfannkuchen   meaning   pancake.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Topswops step by step in the Lua programming language

Source code in the lua programming language

-- Return an iterator to produce every permutation of list
function permute (list)
    local function perm (list, n)
        if n == 0 then coroutine.yield(list) end
        for i = 1, n do
            list[i], list[n] = list[n], list[i]
            perm(list, n - 1)
            list[i], list[n] = list[n], list[i]
        end
    end
    return coroutine.wrap(function() perm(list, #list) end)
end
 
-- Perform one topswop round on table t
function swap (t)
    local new, limit = {}, t[1]
    for i = 1, #t do
        if i <= limit then
            new[i] = t[limit - i + 1]
        else
            new[i] = t[i]
        end
    end
    return new
end
 
-- Find the most swaps needed for any starting permutation of n cards
function topswops (n)
    local numTab, highest, count = {}, 0
    for i = 1, n do numTab[i] = i end
    for numList in permute(numTab) do
        count = 0
        while numList[1] ~= 1 do
            numList = swap(numList)
            count = count + 1
        end
        if count > highest then highest = count end
    end
    return highest
end
 
-- Main procedure
for i = 1, 10 do print(i, topswops(i)) end


  

You may also check:How to resolve the algorithm List rooted trees step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the PL/I programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the Nim programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the BQN programming language
You may also check:How to resolve the algorithm Eertree step by step in the D programming language