Advertisement
thoga31

Permutations with(out) repetition

Mar 14th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.44 KB | None | 0 0
  1. {$mode objfpc}
  2. program permutations;
  3. const
  4.    NEWLINE =
  5.       {$ifdef windows}
  6.          #13+
  7.       {$endif}
  8.          #10;
  9.  
  10. type
  11.    TStrArray = array of string;
  12.  
  13.  
  14. function Fact(n : byte) : Int64;
  15. (* Factorial of n: n! = n * (n-1)! *)
  16. begin
  17.    if n in [0, 1] then
  18.       Fact := 1
  19.    else
  20.       Fact := n * Fact(Pred(n));
  21. end;
  22.  
  23.  
  24. function Rm(x : char; xs : string) : string;
  25. (* Remove the first occurrence of x in xs. *)
  26. begin
  27.    Rm := xs;
  28.    Delete(Rm, Pos(x, xs), 1);
  29. end;
  30.  
  31.  
  32. procedure Clean(var l : TStrArray);
  33. (* Clears any garbage present in list l. *)
  34. var i : LongInt;
  35. begin
  36.    for i := Low(l) to High(l) do
  37.       l[i] := '';
  38. end;
  39.  
  40.  
  41. operator in (x : string; ys : TStrArray) res : boolean;
  42. (* Overloads the operator in: is there any x in list ys? *)
  43. var y : string;
  44. begin
  45.    res := false;
  46.    for y in ys do
  47.       if x = y then begin
  48.          res := true;
  49.          break;
  50.       end;
  51. end;
  52.  
  53.  
  54. procedure WriteArray(arr : TStrArray; sep : string = NEWLINE);
  55. (* Writes contents of list arr separated by sep. *)
  56. var s : string;
  57. begin
  58.    for s in arr do
  59.       writeln(s);
  60. end;
  61.  
  62.  
  63. function Permute(const xs : string) : TStrArray;
  64. (* Returns permutations without repetition. *)
  65.  
  66.    function Perm(const xs : string) : TStrArray;
  67.    (* Returns total permutations. *)
  68.    var
  69.       i  : LongInt;
  70.       x  : char;
  71.       y  : string;
  72.       ys : TStrArray;
  73.    begin
  74.       if Length(xs) = 0 then begin
  75.          Perm := nil
  76.       end else begin
  77.          SetLength(Perm, Fact(Length(xs)));
  78.          
  79.          if Length(xs) = 1 then
  80.             Perm[0] := xs
  81.          else begin
  82.             i := 0;
  83.             for x in xs do begin
  84.                ys := Perm(Rm(x, xs));
  85.                for y in ys do begin
  86.                   Perm[i] := Concat(String(x), y);
  87.                   Inc(i);
  88.                end;
  89.             end;
  90.          end;
  91.       end;
  92.    end;
  93.    
  94.    function Nub(zs : TStrArray) : TStrArray;
  95.    (* Eliminates duplicated elements from zs. *)
  96.    var
  97.       z     : string;
  98.       i     : LongInt;
  99.       count : LongInt = 0;
  100.    begin
  101.       SetLength(Nub, Length(zs));
  102.       Clean(Nub);
  103.       for z in zs do begin
  104.          if not (z in Nub) then begin
  105.             Nub[count] := z;
  106.             Inc(count);
  107.          end;
  108.       end;
  109.       SetLength(Nub, count);
  110.    end;
  111.  
  112. begin  (* Permute *)
  113.    Permute := Nub(Perm(xs));
  114. end;
  115.  
  116.  
  117. begin  (* MAIN BLOCK *)
  118.    WriteArray(Permute('CAII'));
  119.    readln;
  120. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement