Home computickets random word generator without repeating letters without searching

random word generator without repeating letters without searching

Author

Date

Category

What parameters are transmitted to the generator:

  • x – word number;
  • n – alphabet size;
  • L – length of the output word.

It is necessary to implement a non-neurous algorithm, which by three parameters transmitted by three will return the word.

Alphabet – Latin letters in alphabetical order, capsa.

for n = 5 , L = 3 Connect the correspondence X words:

  • 0: ABC
  • 1: abd
  • 2: ABE
  • 3: ACB
  • 4: ACD
  • 5: ace
  • 6: adb
  • 7: adc
  • 8: ADE
  • 9: aeb
  • 10: AEC
  • 11: AED
  • 12: bac

My implementation of the algorithm works on L = 1; 2. But on L = 3 errors appear. The algorithm itself is constructed on shifts when contacting the alphabet. An array H stores indexes of letters in the new dictionary (from which the characters are excluded that have already come to the word). The array A stores the clarification of the H indexes into the initial dictionary (adds an indentation for each remote from the alphabet to the left). Thus, ultimately, array A stores placing without repetitions.

Private Static String Gets (int x, int n, int l) {
  STRING S = "ABCDEFGHJKLMNOPQ";
  String Out = "";
  int [] h = new int [n];
  int [] a = new int [n];
  For (int i = 0; i & lt; l; i ++) {
    h [i] = (X / (Factory (n - 1 - i) / Factory (N - L)))% (N-I);
    INT SUM = H [i];
    for (int j = 0; j & lt; i; j ++)
      Sum + = ((H [i] & gt; = h [j])? 1: 0);
    A [I] = SUM;
    Out + = S.Charat (A [I]);
  }
  RETURN OUT;
}

Answer 1

There is F = a (N, L) = N! / (NL)! The desired accommodation .

So you can generate a random number in the 0..f-1 range and build an accommodation corresponding to this number.

A, in order to obtain accommodation by number (in lexicographical order), you can use the following – the first element of the set is in the first place in A (N-1, L-1) accommodation (in the example from the question These are 12 pieces starting with A ). Then we exclude the element used from the list, and you find the element in the second position, etc.

Here is an example on Delphi from of this answer (DIV – integer division, mod , balance from division, % )

For arguments 5, 3, 15 (number 15) It turns out the placement of 1,2,0 , corresponding to BCA

function arrangementByrank (N, K, Rank: Integer): String;
  FUNCTION NUMARRNK (N, K: INTEGER): INT64;
  var.
   I: integer;
  Begin.
   Result: = 1;
   For i: = 0 to k - 1 do
    Result: = Result * (N - i);
  end;
 var.
  Dig: Array of Byte;
  I, J, ID, ANK: INTEGER;
 Begin.
  Result: = '';
  SetLENGTH (DIG, N);
  For i: = 0 to n - 1 Do
    Dig [i]: = i; // Initial Digit List
  For i: = 1 to k do begin
   ANK: = NUMARRNK (N - I, K - I); // Might Be Optimized
   ID: = Rank Div ​​Ank;
   Rank: = Rank MOD ANK; // Prepare for the Next Round
   Result: = Result + inttostr (Dig [ID]);
   for j: = id to n - i - 1 do
    Dig [j]: = Dig [j + 1]; // Squeeze Digit List
  end;
 end;

Java translation: (test on IDEONE )

Public Static Int Numarrnk (Int N, Int K) {
  int Out = 1;
  for (int i = 0; i & lt; = k - 1; i ++)
    Out * = n - i;
  RETURN OUT;
}
Public Static String ArrByr (int n, int k, int x) {
  STRING S = "ABCDEFGHJKLMNOPQ";
  int [] dig = new int [n];
  int id, ank;
  String Out = "";
  for (int i = 0; i & lt; n; i ++)
    Dig [i] = i; 
For (int i = 1; i & lt; = k; i ++) {
     ANK = NUMARRNK (N - I, K - I); // Might Be Optimized
     id = x / ank;
     x = x% ank; // Prepare for the Next Round
     Out = Out + S.Charat (Dig [ID]);
     for (int j = id; j & lt; n - i; j ++)
       Dig [j] = Dig [j + 1];
   }
   RETURN OUT;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions