Published Sept. 8, 2021, 6:14 p.m.
So you want to generate strings? This tutorial will show you how to generate any number of unique strings just in case you fancy doing so.
In one series of mine I show how to solve the Monty Hall problem.
In that problem you have three doors from which to choose to get started -- 'A', 'B' and 'C'. After you select a door, Monty Hall opens one and then you have a choice to switch doors or not. If you don't know which answer is the best for getting the prize then perhaps you should check out the series.
However, if you're just here for the strings then proceed!
Three doors was easy enough. But I wondered, what if there were 100 doors? Originally I gave the doors numbers for names (0 to 99 for simplicity). But I wanted to have a way that I could give them proper letter names too...
How can we generate a list of unique names for 100 doors using alphabet characters?
To get started we will import the module 'string' at the top of our code.
import string
In case you didn't know, you can always find out what extensions are a part of a module by running dir(module_name) in a python interactive session.
I liked the uppercase letters to name my doors so we will go with that.
alphabet = list(string.ascii_uppercase)
Now our alphabet variable is thus:
>>> alphabet
['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']
>>>
So now we have a list with 26 choices. But what if we want fewer or more choices? Let us define a variable with just that purpose:
how_many_choices = 100
We will stick with 100 choices. Now we need to use some looping and logic to establish unique strings.
What do we want to accoplish in words? If we want more than 26 unique strings then we need to add to our list. I propose that after 'Z' we start over with 'AA, AB, AC ...' and so on. We can acomplish what we want with 2 'while' loops. While loops are notorious for being vulnerable to causing 'infinite loops' or breaking your code. Nevertheless, they have proper use and I can create what I want with a simple set of nested while loops.
We must first establish two 'counters':
i=0
j=0
Then we should take the length of our original alphabet list.
lenorig = len(alphabet)
I do it this way in case you want to use a different alphabet or include special characters or a mix of upper and lower case. Whatever you use for your original alphabet you should take the length of it and record that to be used in our loops.
Now for the while loops. We want our code to have the behavoir that 'while the length of our list is less than how many choices that we want, we want to add to our list.'
while len(alphabet) < how_many_choices:
Now that we know under what circumstance to keep on adding to our list we want to establish what to add to our list.
We want to append to our alphabet the first string ('A') plus another string (initially also 'A'). But if and when we get to 'AZ' we want to be able to start 'BA', then 'BB' and so on. Therefore we will simply append to our alphabet list the following:
alphabet.append(alphabet[j]+alphabet[i])
i+=1
But our conditions demand that we keep the variable 'i' less than the length of our initial alphabet:
while(i < lenorig):
alphabet.append(alphabet[j]+alphabet[i])
i+=1
So what happens if 'i' == the original length of our alphabet? Well then we need to reset 'i' to equal zero and then we need to increment 'j' by 1:
while(i < lenorig):
alphabet.append(alphabet[j]+alphabet[i])
i+=1
j+=1
i=0
Also, I don't want to overshoot. We should stop adding to our alphabet if the length of the alphabet is the same as the number of choices that we ultimately want.
Therefore we will make two conditions that must both be true to continue:
while(i < lenorig and len(alphabet) != how_many_choices):
Put all together the loops look like the following:
i=0
j=0
lenorig = len(alphabet)
while len(alphabet) < how_many_choices:
while(i < lenorig and len(alphabet) != how_many_choices):
alphabet.append(alphabet[j]+alphabet[i])
i+=1
j+=1
i=0
Finally, we need to use the alphabet to establish our 'choices'. Notice that our set of while loops only gets used if the number of doors that we want is larger than 26. Therefore, to be sure that our number of choices is correct we need to use a 'slice' operation to force the list to have the correct number of entries:
choices = alphabet[:how_many_choices]
Now, if we revert back to a Monty Hall problem with 3 doors where 'how_many_choices = 3' our choices will be only 'A', 'B', 'C' as desired. However, if we set our choices up to 2000 different choices, we start to get every letter combination starting with 'A' and ending with 'BXX'.
Now if we leave our number of choices at 2000 and have Monty Hall reveal 1998 doors we should basically make the correct choice every time we swith our answer. I would caution you to lower the number of simulations that you perform so that this problem dosent' eat up all of your computer's memory.
That's all for this tutorial. Thanks for watching.