How to resolve the algorithm Topswops step by step in the PicoLisp programming language
How to resolve the algorithm Topswops step by step in the PicoLisp 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 PicoLisp programming language
Source code in the picolisp programming language
(de fannkuch (N)
(let (Lst (range 1 N) L Lst Max)
(recur (L) # Permute
(if (cdr L)
(do (length L)
(recurse (cdr L))
(rot L) )
(zero N) # For each permutation
(for (P (copy Lst) (> (car P) 1) (flip P (car P)))
(inc 'N) )
(setq Max (max N Max)) ) )
Max ) )
(for I 10
(println I (fannkuch I)) )
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the Go programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the Dart programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Golfscript programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Wren programming language