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

Published on 12 May 2024 09:40 PM

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

Source code in the maple programming language

combinat:-permute(3);
   [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

combinat:-permute([a,b,c]);
   [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]]

fold:=(f,a,v)->`if`(nops(v)=0,a,fold(f,f(a,op(1,v)),[op(2...,v)])):
insert:=(v,a,n)->`if`(n>nops(v),[op(v),a],subsop(n=(a,v[n]),v)):
perm:=s->fold((a,b)->map(u->seq(insert(u,b,k+1),k=0..nops(u)),a),[[]],s):
perm([$1..3]);
   [[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]

  

You may also check:How to resolve the algorithm Find the missing permutation step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the Icon programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Ada programming language
You may also check:How to resolve the algorithm SHA-1 step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the C programming language