Help with an algorithm wanted
Russell E. Owen
rowen at cesmail.net
Wed Jun 26 16:02:02 EDT 2002
I'd like some help solving a problem in Python.
The basic idea is I have an item of data that can be represented in any
of four ways: A,B,C and D. I also have conversion functions between
certain pairs of representations, such that there's always a path
between any two data representations. In this case suppose I know A<->C,
B<->C and C<->D, or graphically:
+--> C <---> D
I'd like to create objects that have one of these items (A,B,C,D) as a
given, then be able to query any such object for any data representation.
For instance, an object with A as the given would return D by solving
What I'm trying to figure out is a simple and reasonably elegant way to
code such objects.
I am pretty sure recursion is the heart of the solution. An object with
A as the given could have a method getD that returned D<-C and a method
getC that returned C<-A.
The real issue is creating such objects without hand coding each one
(since in the real world I have more representations and the number of
casese proliferates badly).
I suspect I should use recursion to create the object, too, but it
sounds like it'll need a lot of introspection. For instance to create an
object with A as the given, one might:
- start by defining method getA to return A
- look through the set of conversion functions that output A; the only
one is A<-C, so method getC returns C<-A.
- repeat with all methods that return C and don't have methods already
defined; this gives method getB returns B<-C and method getD returns
Alternatively, I can imagine making each object basically identical
except for some kind of meta-data that tells each method which data item
to query; the appropriate function can then be looked up as needed (e.g.
via a dictionary that takes as a key the output and input
representation). This would slow down the methods but avoid the
More information about the Python-list