Next / Previous / Contents / Shipman's homepage

52. class Slot: One place where a word goes

Each instance of this class describes either a horizontal slot or a vertical slot.


# - - - - -   c l a s s   S l o t

class Slot(object):
    '''Represents one location for a word in the puzzle.

        Slot ( origin, length, isV ):
          [ (origin is the location of the first cell of the slot
            as a Coord instance) and
            (length is the length as a positive int) and
            (isV is 0 for horizontal, 1 for vertical) ->
              return a new Slot instance representing these values ]
        .origin:  [ as passed to constructor, read-only ]
        .length:  [ as passed to constructor, read-only ]
        .isV:     [ as passed to constructor, read-only ]
        .__str__(self): [ return a string repr. of self ]
        .__len__(self): [ returns self.length ]
        .__getitem__(self, k):
          [ return the location of the (k)th character of self
            as a Coord instance ]

The __cmp__ method defines a full ordering on slots. This is necessary for reasons discussed in Section 4.2, “Data structures”.

        .__cmp__(self, other):
          [ usual comparison function, with self.origin as
            the primary key and self.isV the secondary key ]

These methods convert from positions within the puzzle to and from positions within the slot.

          [ generate the coordinates of cells in self in
            ascending order by index, as Coord instances ]
        .findCoord ( coord ):
          [ if coord is within self ->
              return the position of that coord relative to self's
            else -> raise IndexError ]