0

Need pointer for algorithm on generating alphanumeric sequence. The requirement is as follows.

Only 0-9, A-Z and a-z characters can be used. Initially the sequence will start with only one character and when the sequence has been exhausted will it go to two characters and similarly the sequence will be incremented when the previous sequence has been exhausted.

Example of the sequences are given below

One Character sequence

0 1 2 .. 9 A B .. Z a b .. z

Now the one character sequence has been exhausted. Then a 2 digit series will start.

00 01 02 .. 09 0A 0B .. 0Z 0a 0b .. 0z 10 11 .. 19 1A .. 1Z 1a 1b .. zz

After two characters series has been exhausted then 3 character series will start as given below

000 ... zzz

And the series will generate till the 12 character series has been exhausted.

Can anybody help me point to some link or suggest me the mechanism to do it?

I am trying to do this in PHP.

Thanks

user4943000
  • 111
  • 12
  • @nogad.. if you read the description, I am not asking anybody to write code for me. I am asking if anybody can point me to some direction or give me some suggestions. No need to say negative words if you do not know just like me. – user4943000 Apr 11 '17 at 21:40
  • I found a very similar question here: http://stackoverflow.com/questions/4964197/converting-a-number-base-10-to-base-62-a-za-z0-9 – KIKO Software Apr 11 '17 at 21:41
  • I suggest reading [How to Ask](http://stackoverflow.com/questions/how-to-ask) a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question). Also, be sure to take the [tour](http://stackoverflow.com/tour) –  Apr 11 '17 at 21:47
  • @nomad: on similar note, I would suggest you read this page also [link]https://stackoverflow.com/help/how-to-answer – user4943000 Apr 11 '17 at 22:09
  • @maraca: Yes.. I do realize that, but that will be over a period of time. This alpha numeric sequence is going to be used for URL shortener site and we wanted some flexibility to increase the lengths without re-working on the logic every time the sequence is getting exhausted. – user4943000 Apr 11 '17 at 22:14
  • that's not my name, and that's not how you post a link in a comment –  Apr 12 '17 at 02:35

2 Answers2

1

Python:

digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

def NthStr(n):
    str=digits[n%len(digits)];
    while n>=len(digits):
        n=(n/len(digits) - 1)
        str=digits[n%len(digits)]+str
    return str

Try it (with fewer digits, so you can see the pattern): https://ideone.com/rZEV1m

I imagine this would be pretty easy to translate into PHP

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
1

Given below is the PHP implementation of Matt's python code.

Following characters have been removed from the implementation 0,o,O,1,i,I

<?php
function GenerateShortCodes($n)
{
    $alphabet=array('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','8','9');
    $len=56;
    $ns=$alphabet[$n%$len];
    while ($n>=$len)
    {
       $n=($n/$len-1);
       $ns=$alphabet[$n%$len].$ns;
    }
    return $ns;
}
$startval=0;

for( $i = $startval; $i<$startval+10000; $i++ ) 
{
    echo GenerateShortCodes($i);
    echo '</br>';
}
?>
user4943000
  • 111
  • 12