Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- For example if you want to obtain the "natural" permutations for base-13, this is for the decimal digits and a,b,c
- notice that c=12 regarded as digit in the conventional notation, but since we started here to count from zero, each
- latin alphabet letter have its conventional value +1 when it is used for counts.
- Then follow the usual instructions given below (please read them), and call:
- -------------------------------------------------------------[Linux Shell example]
- gandalf@rivendell@sh$ permworep c
- -------------------------------------------------------------
- Written on my host machine:
- * 32-bit Intel Atom with 160Gb Hdd 1Gb Ram,
- * Slackware GNU Linux distro version 14
- * Free Pascal Compiler 2.6.2
- Formerly was http://pastebin.com/ZCLc63XG... all bogus bugs apparently fixed.
- *)
- program PermutationsWithoutRepetitions;
- (*
- USAGE:
- -----
- i] Compile this sourcecode and place the executable in a proper location.
- (It is strongly suggested to use the FPC compiler. Please visit: freepascal.org)
- ii] Create a empty file called "permworep_idata.txt" and fill it just with
- a sigle zero. Save it in the same directory for the executable mentioned
- in [i] and ensure that there will be granted read-write permissions
- for this program during its execution.
- iii] For obtaining the 10! permutations for the first 10 non-negative integers
- enumerated starting from zero, call the program passing "9" as its unique parameter.
- (This is due zero is the first digit in every positional number system)
- Instead of using double byte numbers like 11 or 16, use the letter representing
- it as digit in a base greater than the decimal. The progrma is theoretically
- limited up to 36 digits enumerated from zero.
- iv] Depending on the resources available during the execution, this program might not
- work properly as expected, being interrumped or crashing for example when used to
- computed sets of permutations greater than 10 ("a" letter).
- Written by R. J. Cano, <aallggoorriitthhmmuuss@gmail.com> on Dec 9th 2012.
- Observation: There exists better known algorithms/methods for computing pemutations.
- The present program is merely another one more among the possible implementations for
- doing such jobs.
- See for example the entry A055881 at OEIS.org, specifically the comment by Joerg Arndt.
- The purpose on writing this program was to serve as ilustration for the algorithm known
- as Steinhaus-Jhonson-Trotter, and additionally about how to do these thing using the
- hard disk instead the primary memory.
- Important: The present program is released under the terms
- of the GNU General Public License 3.0 and it goes
- without any warranty whatsoever.
- *)
- type
- my_string=string[15]; (* Mem. saving measure: Limit only 16 bytes per string. Up to the first 15! permutations only *)
- const
- basename:string='permworep';
- digit:array [0..35] of char=
- ('0','1','2','3','4','5','6','7','8','9',
- 'a','b','c','d','e','f','g','h','i','j',
- 'k','l','m','n','o','p','q','r','s','t',
- 'u','v','w','x','y','z');
- var
- f_input,
- f_output : text;
- input,
- output,
- request:my_string;
- size_i,
- size_o,
- index_a,
- index_b:byte;
- index_c,
- dir:shortint;
- insert:char;
- function f(x:char):byte;
- var ans:byte;
- begin
- if (x in ['a'..'z']) then begin
- ans:=10+ord(x)-ord('a');
- end else if (x in ['0'..'9']) then begin
- ans:=ord(x)-ord('0');
- end else begin
- ans:= 255;
- end;
- f:=ans;
- end;
- begin
- if (paramcount = 1) then begin
- request:=paramstr(1);
- size_o:=f(request[length(request)])+1;
- assign(f_input,basename+'_idata.txt');
- assign(f_output,basename+'_odata.txt');
- reset(f_input);
- readln(f_input,input);
- close(f_input);
- size_i:=f(input[length(input)])+1;
- while (size_o > size_i) do begin
- dir:=-1;
- assign(f_input,basename+'_idata.txt');
- assign(f_output,basename+'_odata.txt');
- reset(f_input);
- rewrite(f_output);
- insert:=digit[size_i];
- while (not eof(f_input)) do begin
- readln(f_input,input);
- for index_a:= 0 to size_i do begin
- output:='';
- index_c:=-1;
- for index_b:= 0 to size_i do begin
- if ((dir=-1) and (index_a+index_b=size_i)) then begin
- output:=output+digit[size_i];
- end else if ((dir=1) and (index_a=index_b)) then begin
- output:=output+digit[size_i];
- end else begin
- if (index_c=size_i-1) then index_c:=-1;
- index_c:=index_c+1;
- output:=output+input[index_c+1];
- end;
- end;
- writeln(f_output,output);
- end;
- dir:= dir*(-1);
- end;
- close(f_input);
- close(f_output);
- erase(f_input);
- assign(f_input,'tmp');
- rename(f_output,basename+'_idata.txt');
- size_i:= size_i + 1;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement