Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unique string creation

I've looked at several other SO questions (and google'd tons) that are 'similar'-ish to this, but none of them seem to fit my question right.

I am trying to make a non fixed length, unique text string, only containing characters in a string I specify. E.g. made up of capital and lower case a-zA-Z characters. (for this example I use only a, b, and c lower case)

Something like this (broken code below)

def next(index, validCharacters = 'abc'):
    return uniqueShortAsPossibleString

The index argument would be an index (integer) that relate to a text string, for instance:

next(1)  == 'a'
next(2)  == 'b'
next(3)  == 'c'

next(4)  == 'aa'
next(5)  == 'ab'
next(6)  == 'ac'

next(7)  == 'ba'
next(8)  == 'bb'
next(9)  == 'bc'

next(10) == 'ca'
next(11) == 'cb'
next(12) == 'cc'

And so forth. The string:

  1. Must be unique, I'll be using it as an identifier, and it can only be a-zA-Z chars
  2. As short as possible, with lower index numbers being shortest (see above examples)
  3. Contain only the characters specified in the given argument string validCharacters

In conclusion, how could I write the next() function to relate an integer index value to an unique short string with the characters specified?

P.S. I'm new to SO, this site has helped me tons throughout the years, and while I've never made an account or asked a question (till now), I really hope I've done an okay job explaining what I'm trying to accomplish with this.

like image 404
powerpup118 Avatar asked Oct 25 '12 06:10

powerpup118


People also ask

How do you generate unique strings in Python?

In order to generate random strings in Python, we use the string and random modules. The string module contains Ascii string constants in various text cases, digits, etc. The random module on the other hand is used to generate pseudo-random values.

How do you generate unique random strings?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.

How do you create a string of specified length in Python?

In Python, strings have a built-in method named ljust . The method lets you pad a string with characters up to a certain length. The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.


1 Answers

What you are trying to do is write the parameter of the next function in another base.

Let's suppose validCharacters contains k characters: then the job of the next function will be to transform parameter p into base k by using the characters in validCharacters.

In your example, you can write the numbers in base 3 and then associate each digit with one letter:

next(1) -> 1 -> 'a'
next(2) -> 2 -> 'b'

next(4) -> 11 -> 'aa'
next(7) -> 21 -> 'ba'

And so forth.

With this method, you can call next(x) without knowing or computing any next(x-i), which you can't do with iterative methods.

like image 144
alestanis Avatar answered Sep 25 '22 18:09

alestanis