1

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

6
  • @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. Commented Apr 11, 2017 at 21:40
  • I found a very similar question here: stackoverflow.com/questions/4964197/… Commented Apr 11, 2017 at 21:41
  • I suggest reading How to Ask a good question and the perfect question. Also, be sure to take the tour Commented Apr 11, 2017 at 21:47
  • @nomad: on similar note, I would suggest you read this page also [link]stackoverflow.com/help/how-to-answer Commented Apr 11, 2017 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. Commented Apr 11, 2017 at 22:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. This was what I was looking for. Thanks
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>';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.