How to resolve the algorithm Permutations by swapping step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Permutations by swapping step by step in the Quackery programming language

Table of Contents

Problem Statement

Generate permutations of n items in which successive permutations differ from each other by the swapping of any two items. Also generate the sign of the permutation which is +1 when the permutation is generated from an even number of swaps from the initial state, and -1 for odd. Show the permutations and signs of three items, in order of generation here. Such data are of use in generating the determinant of a square matrix and any functions created should bear this in mind. Note: The Steinhaus–Johnson–Trotter algorithm generates successive permutations where adjacent items are swapped, but from this discussion adjacency is not a requirement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Permutations by swapping step by step in the Quackery programming language

Source code in the quackery programming language

  [ stack ]                   is parity (   --> s )

  [ 1 & ]                     is odd    ( n --> b )

  [ [] swap witheach
    [ nested
      i odd 2 * 1 -
      join nested join ] ]    is +signs ( [ --> [ )

  [ dup
    [ dup 0 = iff
        [ drop ' [ [ ] ] ]
        done
      dup temp put
      1 - recurse
      [] swap
      witheach
        [ i odd parity put
          temp share times
            [ temp share 1 -
              over
              parity share
              iff i else i^
              stuff
              nested rot join
              swap ]
          drop
          parity release ]
      temp release ]
    swap odd if reverse
    +signs ]                  is perms  ( n --> [ )

  3 perms witheach [ echo cr ]
  cr
  4 perms witheach [ echo cr ]

  

You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the sed programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the F# programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Retro programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Kotlin programming language