Creating javascript alphanumeric sequence

I wrote a terribly slow function to generate codes that go from AA000 to ZZ999 (in a sequence not random). And I came to the conclusion that there should be a better way to do this. Any suggestions on how to make this faster?

function generateAlphaNumeric(){ theAlphabet = ['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']; resultArrray = []; resultArrray2 = []; teller = 0; for(i in theAlphabet){ for(x in theAlphabet){ resultArrray[teller] = theAlphabet[i] + theAlphabet[x]; teller++; } } teller = 0; for(x = 0; x<10; x++){ for(y = 0; y<10; y++){ for(z = 0; z<10; z++){ resultArrray2[teller] = x.toString() + y.toString() +z.toString(); teller++; } } } teller = 0; finalArray = []; for(index in resultArrray){ for(i in resultArrray2){ finalArray[teller] = resultArrray[index] + resultArrray2[i]; teller++; } } //console.log(resultArrray); //console.log(resultArrray2); console.log(finalArray); } 
+5
source share
5 answers

This should be significantly faster:

 var theAlphabet = ['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 theDigits = ['0','1','2','3','4','5','6','7','8','9']; var result = []; for (var i=0 ; i<26 ; i++) { var prefix1 = theAlphabet[i]; for (var j=0 ; j<26; j++) { var prefix2 = prefix1 + theAlphabet[j]; for(var x = 0; x<10; x++){ var prefix3 = prefix2 + theDigits[x]; for(var y = 0; y<10; y++){ var prefix4 = prefix3 + theDigits[y]; for(var z = 0; z<10; z++){ result.push(prefix4 + theDigits[z]); } } } } } 

Key ideas:

  • Generate all in one go
  • Use partial strings as much as possible

However, I do not see how useful such an exhaustive list is. There are exactly 26 * 26 * 1000 different codes. Therefore, instead of supporting an array with all codes, it makes sense to simply create a function that generates the requested code:

 function getCode(number) { var z = number % 10; number -= z; number /= 10; var y = number % 10; number -= y; number /= 10; var x = number % 10; number -= x; number /= 10; var a = number % 26; number -= a; number /= 26; var b = number; return theAlphabet[a] + theAlphabet[b] + theDigits[x] + theDigits[y] + theDigits[z]; } 
+3
source

Try this solution:

  function generate() { var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ar = []; for (var index1 = 0; index1 < str.length; index1++) { for (var index2 = 0; index2 < str.length; index2++) { for (var index3 = 0; index3 < 1000; index3++) { ar.push(str[index1] + str[index2] + ('000' + index3).slice(-3)); } } } return ar; } console.log(generate()); 
0
source
 function generate() { var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', array = []; for (var i = 0; i < str.length; i++) { for (var j = 0; j < str.length; j++) { for (var k = 0; k < 10; k++) { for (var l = 0; l < 10; l++) { for (var m = 0; m < 10; m++) { ar.push(str[i] + str[j] + k + l + m); } } } } } return array; } console.log(generate()); 

This will create an array of all codes. U can save this array and easily parse it with a loop.

0
source

I have not tested it, but it should do the trick.

 function generateAlphaNumeric() { var theAlphabet = ['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 result = []; // Will take a random letter inside theAlphabet // Math.floor(Math.random() * theAlphabet.length) will generate a random number between 0 and 25 var i = 0; while(i<2) { var letter = theAlphabet[Math.floor(Math.random() * theAlphabet.length)]; result.push(letter); i++; } i = 0; while(i<3) { // Adds a random number between 0 and 9 result.push(Math.floor(Math.random() * 10)); i++; } return result; } 
0
source

In terms of computational complexity, unfortunately, this is the best you can do. From a pure number of instructions, you can do a little better (as others have pointed out), but it will still be the same order of complexity (remember that constants / factors are not related to complexity with large O). You can also optimize storage a bit.

Think about it. Your array should have 26 * 26 * 10 * 10 * 10 members. This means that you need to at least touch this many elements.

Let N = number of elements in the alphabet Let M = number of elements in your queue

The complexity of the order of the best case = O (N * N * M * M * M) (if you had to assign values)

Best data storage complexity = same as above (you must store all codes)

You are currently using the following operations:

 for(i in theAlphabet){ // *O(N)* for(x in theAlphabet){ // *O(N)* resultArrray[teller] = theAlphabet[i] + theAlphabet[x];// *(O(1))* } } for(x = 0; x<10; x++){ // O(M) for(y = 0; y<10; y++){ // O(M) for(z = 0; z<10; z++){ // O(M) resultArrray2[teller] = x.toString() + y.toString() +z.toString(); // O(1) (technically this is O(length of x + y + z) teller++; } } } for(index in resultArrray){ // O(N * N) for(i in resultArrray2){ // O(M * M * M( finalArray[teller] = resultArrray[index] + resultArrray2[i]; //O(1) teller++; } } 

So at the end of the day, your order complexity is O (N * N * M * M * M), which is the best you can do.

The bigger question is why you want to generate all the codes in general. If you want to create a unique code for an order number or something else, you can make a state machine, for example:

 function getNextCode(previousCode) { // in here, just increment the previous code } 

If all you need is a random identifier, try using a timestamp hash instead + something about the request instead.

If you don't care about uniqueness, you can always just generate random code.

All of the above applies to O (1).

0
source

Source: https://habr.com/ru/post/1241630/


All Articles