How to resolve the algorithm Permutations step by step in the langur programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Permutations step by step in the langur programming language

Table of Contents

Problem Statement

Write a program that generates all   permutations   of   n   different objects.   (Practically numerals!)

Let's start with the solution:

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

Source code in the langur programming language

val .factorial = f if(.x < 2: 1; .x x self(.x - 1))

val .permute = f(.arr) {
    if not isArray(.arr): throw "expected array"

    val .limit = 10
    if len(.arr) > .limit: throw $"permutation limit exceeded (currently \.limit;)"

    var .elements = .arr
    var .ordinals = pseries len .elements

    val .n = len(.ordinals)
    var .i, .j

    for[.p=[.arr]] of .factorial(len .arr)-1 {
        .i = .n - 1
        .j = .n
        while .ordinals[.i] > .ordinals[.i+1] {
            .i -= 1
        }
        while .ordinals[.j] < .ordinals[.i] {
            .j -= 1
        }

        .ordinals[.i], .ordinals[.j] = .ordinals[.j], .ordinals[.i]
        .elements[.i], .elements[.j] = .elements[.j], .elements[.i]

        .i += 1
        for .j = .n; .i < .j ; .i, .j = .i+1, .j-1 {
            .ordinals[.i], .ordinals[.j] = .ordinals[.j], .ordinals[.i]
            .elements[.i], .elements[.j] = .elements[.j], .elements[.i]
        }
        .p = more .p, .elements
    }
}

for .e in .permute([1, 3.14, 7]) {
    writeln .e
}

  

You may also check:How to resolve the algorithm Letter frequency step by step in the 11l programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Simula programming language
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Substring step by step in the Burlesque programming language