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

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Topswops step by step in the Go 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 Go programming language

The code you provided is a Go program that calculates the minimum number of steps required to sort a deck of cards using the Topswop algorithm.

The Topswop algorithm is a sorting algorithm that works by repeatedly swapping pairs of cards until the deck is sorted. The algorithm is known for its simplicity and efficiency, and it can be used to sort a deck of cards in linear time.

The code begins by creating two arrays, a and b, which will be used to store the state of the deck at each step of the algorithm. The array a stores the number of cards in each pile, and the array b stores a flag indicating whether or not each pile is sorted.

The code then enters a loop that iterates through the cards in the deck. For each card, the code checks if the card is in the correct position. If the card is not in the correct position, the code swaps the card with the card in the correct position.

The code also keeps track of the number of steps that have been taken. The number of steps is stored in the variable m.

The loop continues until the deck is sorted. The code then prints the number of steps that were taken to sort the deck.

Here is a detailed explanation of the code:

  • The main function is the entry point of the program. The main function calls the steps function to calculate the minimum number of steps required to sort a deck of cards.
  • The steps function takes one argument, n, which is the number of cards in the deck. The function returns the minimum number of steps required to sort the deck.
  • The steps function begins by creating two arrays, a and b, which will be used to store the state of the deck at each step of the algorithm. The array a stores the number of cards in each pile, and the array b stores a flag indicating whether or not each pile is sorted.
  • The steps function then enters a loop that iterates through the cards in the deck. For each card, the code checks if the card is in the correct position. If the card is not in the correct position, the code swaps the card with the card in the correct position.
  • The steps function also keeps track of the number of steps that have been taken. The number of steps is stored in the variable m.
  • The loop continues until the deck is sorted. The steps function then returns the number of steps that were taken to sort the deck.

The Topswop algorithm is a simple and efficient sorting algorithm that can be used to sort a deck of cards in linear time. The code provided is a Go implementation of the Topswop algorithm.

Source code in the go programming language

// Adapted from http://www-cs-faculty.stanford.edu/~uno/programs/topswops.w
// at Donald Knuth's web site.  Algorithm credited there to Pepperdine
// and referenced to Mathematical Gazette 73 (1989), 131-133.
package main

import "fmt"

const ( // array sizes
    maxn = 10 // max number of cards
    maxl = 50 // upper bound for number of steps
)

func main() {
    for i := 1; i <= maxn; i++ {
        fmt.Printf("%d: %d\n", i, steps(i))
    }
}

func steps(n int) int {
    var a, b [maxl][maxn + 1]int
    var x [maxl]int
    a[0][0] = 1
    var m int
    for l := 0; ; {
        x[l]++
        k := int(x[l])
        if k >= n {
            if l <= 0 {
                break
            }
            l--
            continue
        }
        if a[l][k] == 0 {
            if b[l][k+1] != 0 {
                continue
            }
        } else if a[l][k] != k+1 {
            continue
        }
        a[l+1] = a[l]
        for j := 1; j <= k; j++ {
            a[l+1][j] = a[l][k-j]
        }
        b[l+1] = b[l]
        a[l+1][0] = k + 1
        b[l+1][k+1] = 1
        if l > m-1 {
            m = l + 1
        }
        l++
        x[l] = 0
    }
    return m
}


  

You may also check:How to resolve the algorithm Search a list of records step by step in the Lua programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Futhark programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Clojure programming language
You may also check:How to resolve the algorithm Factorial step by step in the Panda programming language