Archive for December, 2007

Python – How to Find all Substring Combinations in a String

Wednesday, December 12th, 2007

I got asked this question recently. Basically how many combinations can you make out of a string like “abcd”. For example the answer for “abcd” is:

a
b
c
d
ab
ac
ad
bd
cb
cd
abd
acb
acd
cbd
acbd

I believe this is basically the same question as find all subsets in a set. Please correct me if I’m wrong on that.

Here’s the code I finally came up with for my first attempt:

"""
Enumerate all subsets of characters in a string
"""

#Make a convenient sorter we'll user later
def len_alpha_sorter(val1,val2):
    """#sort by len then by alpha"""
    if len(val1)==len(val2):
        return cmp(val1,val2)
    else:
        return cmp(len(val1),len(val2))

def cross_lists_of_sets(lst1,lst2):
    """return list of unions for every combination of sets
    across the input lists.  We get duplicates here
    which we'll handle later.  This should be fixed later too."""
    cplst1,cplst2=lst1[:],lst2[:]
    retlst=[]
    for set1 in cplst1:
        for set2 in cplst2:
            retlst.append(set1.union(set2))
    return retlst

def get_all_subsets(set_val):
    """
    Main function to get the subsets.  See comments for details.
    """
    #temp is a container to hold lists of sets, keyed by the length
    #of the sets it will hold.
    temp={}
    #get individual characters
    temp[1]=[set([v]) for v in set_val]
    #cross list of sets for previous size with sets for individual
    #characters to get a list of sets for the next size.
    for i in range(2,len(set_val)):
        temp[i]=cross_lists_of_sets(temp[i-1],temp[1])
    #add in full string as a subset
    temp[len(set_val)]=set([set_val])
    #Put each list in temp into one big list instead
    all_sets_w_dupes=[]
    [all_sets_w_dupes.extend(v) for v in temp.values()]
    #We will have duplicates at this point, let's clean up, by
    #making the sets into frozensets, taking the set of that list
    #to get unique elements only, sorting it nicely, and finally
    #putting it together into a string.
    return '\n'.join(
        sorted(
            set([''.join(frozenset(s)) for s in all_sets_w_dupes]),
            len_alpha_sorter)
        )

Can you A.) Figure out the complexity of this beast? and B.) Find an algorithm with lower complexity? The current algorightm must be mamoth since this code ran overnight on a string with less than 15 characters!

Mini Searches with Answers

Friday, December 7th, 2007

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

Devil’s Pie [CH!MER!C.de] examples
If you actually want to use devilspie, you’ll need these examples to guide you.

linux:devilspie [foosel.net]
More devilspie examples

Python Challenge Answers
Answers to the Python challenge. Don’t cheat!

3D-box maker ‽ 3d-box & package on-line
instantly create 3d-box images online, does regular box, cd, and dvd boxes. Looks neat for promoting softwares.

RIFE : Blogs : Embedding images inside the source of a HTML page
A way to embed images right inside the img tag.

Developer’s Guide – Google Chart API – Google Code
An easy way to put nice-looking, custom charts into any webpage. I’m tempted to turn this into a utility, but there may be too many parameters.

Tags: , , , , , , , , , , , , , , , , , , , ,

SQL – How a Join without a Join Type Behaves

Thursday, December 6th, 2007

In MS SQL Server a plain join without specifying inner or outer, etc results in an inner join.

Example:

select * from books join authors on books.authorid=authors.id

is equivalent to:

select * from books inner join authors on books.authorid=authors.id

Please leave comments if you know how this works in MySQL, Oracle, and other database systems. I’m trying to collect them all :-) !

Bazaar IRC Link

Monday, December 3rd, 2007

irc://irc.freenode.net:6667/bzr

(I got tired of manually typing it an and wanted a nice link to take me there.)

More help

Mini Searches with Answers

Saturday, December 1st, 2007

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

Wikipedia:Technical FAQ – How does Wikipedia Handle Concurrent Editing?
When the second person (and later persons) attempts to save the page, MediaWiki will attempt to merge their changes into the current version of the text. If the merge fails the user will receive an "edit conflict" message, and can merge changes manually

Featured Windows Download: Decrypt Your DVD’s Copy Protection with DVD43
Perhaps try this when DVDShrink doesn’t work. I mean only for making fair use backups of your movies in countries that allow such things…

Coder Who Says Py: Bazaar vs. Mercurial : An unscientific comparison
Comparing distributed version control systems. This article favors Mercurial.

BzrVsHg – Bazaar Version Control
Again comparing distributed version control systems, Mercurial and Bazaar. This one favors Bazaar. Oh I can’t decide …

Launchpad
An alternative to google code and source forge for free code hosting?

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,