[Tutor] Decision matrix
Jeff Shannon
jeff@ccvcorp.com
Thu Feb 13 12:51:49 2003
Whoops, I'd intended to reply to the list as well, and only sent this to
Poor Yorick.
--Jeff S.
-------- Original Message --------
Subject: Re: [Tutor] Decision matrix
Date: Thu, 13 Feb 2003 09:46:08 -0800
From: Jeff Shannon <jeff@ccvcorp.com>
To: Poor Yorick <gp@pooryorick.com>
References:
<7497DCA1C240C042B28F6657ADFD8E0901F850C3@i2km11-ukbr.domain1.systemhost.net>
<3E4B57B2.8060607@pooryorick.com>
Poor Yorick wrote:
> alan.gauld@bt.com wrote:
>
> >Sounds like the definition of a dictionary to me...
> >
> Two problems with using a dictionary:
>
> 1. I would like the flags to be accepted in any order.
> 2. If I come up with other flags in the future, I would like to deal
> with them in the most simple possible manner. Adding an exponentially
> larger number of permutations as dictionary keys doesn't seem like a
> good method.
If you want each unique set of flags to call a single (probably unique)
function, regardless of the order of those flags, you can always sort
your flags before searching the dictionary. This avoids the permutation
explosion, and it's probably a lot simpler than your proposed
Intersection() function.
flags = raw_input()
lflags = list(flags)
lflags.sort()
flags = ''.join(lflags)
result = Functions[flags]()
In order to find the intersection of a set of lists, you'll have to
traverse each list once for each item in the first list, which (if you
have a large number of flags) can grow to be a *lot* of list traversal.
Dictionary searching is much faster than list traversal, and is done in
constant time regardless of the size of the dict (where list traversal
is proportional to the size of the list).
Another possible issue with your Intersection() idea -- how are you
intending to verify that a given set of flags always resolves to a
*single* function? With only a few flags, you can eyeball and match up,
but when you've got a dozen flags, each of which has half a dozen
potential functions, it'll become almost impossible to ensure that no
group of flags will resolve to more than one function -- especially if
someone later needs to modify the list of flags. This will be a
maintenance nightmare. A dict, on the other hand, will make it very
easy to see what function will be called by a given combination of
flags, and there's no worries about accidentally selecting more than one
function.
Almost any time you're looking to select one item out of a set of items
based on some arbitrary parameter, a dictionary is the way to go. The
only trick is figuring out what the most efficient key scheme will be...
Jeff Shannon
Technician/Programmer
Credit International