Generating the sub-sets of a string in python :
If you are searching for a program to generate all the sub sets of a string in python, this one could be the place where you have to end up your searching process.Yes this post is all about generating all the sub-sets of a string in python.
What is a sub set of a string...???
You are in the situation that you need to consider all the subsets. What are you going to do.I noticed my friend doing a code which looks like this. He ran two for loops and then generated some strings out of the original string.This is what the origin for this post.
consider "abc" is your string then the subsets of this string are...[](CONFUSING...Yes,refresh your set theory knowledge...empty set is also a subset of a string),[a],[b],[c],[ab],[bc],[ca],[abc]...consider Bi is a subset of A then the elements in the subset Bi should belong to the set A.
if 'N' is a length of your SET then the total number of subsets possible is given by the relation...
Tot. No. of SubSets = 2**n
I Think there is no need to explain the algorithm since it is very simple for you to understand it from the program...
CODE IN "PYTHON" :
def Set(s):
n = len(s)
masks = [1<<j for j in xrange(n)]
for i in xrange(2**n):
yield [s[j] for j in range(n) if (masks[j] & i)]
That's it you are now with your subsets...all you need to do is to use this function like below...
lists=[]
for i in Set(s) :
lists.append(i)
Yes that's it...now go and code your own....
FOR MORE ON THIS TOPIC GO HERE :
NOTE : Wait...if you have any doubt on any topic related to python...just have some touch with the comment box of the post...!!!
03:18
Unknown
0 comments :
Post a Comment