2000-02-16 00:49:47 +00:00
|
|
|
import types
|
|
|
|
|
|
|
|
def flatten(tup):
|
|
|
|
elts = []
|
|
|
|
for elt in tup:
|
|
|
|
if type(elt) == types.TupleType:
|
|
|
|
elts = elts + flatten(elt)
|
|
|
|
else:
|
|
|
|
elts.append(elt)
|
|
|
|
return elts
|
|
|
|
|
2000-02-04 00:28:21 +00:00
|
|
|
class Set:
|
|
|
|
def __init__(self):
|
2000-02-21 22:46:00 +00:00
|
|
|
self.elts = {}
|
2000-02-10 20:54:27 +00:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.elts)
|
2000-02-04 00:28:21 +00:00
|
|
|
def add(self, elt):
|
2000-02-21 22:46:00 +00:00
|
|
|
self.elts[elt] = elt
|
2000-02-04 00:28:21 +00:00
|
|
|
def items(self):
|
2000-02-21 22:46:00 +00:00
|
|
|
return self.elts.keys()
|
2000-02-04 00:28:21 +00:00
|
|
|
def has_elt(self, elt):
|
2000-02-21 22:46:00 +00:00
|
|
|
return self.elts.has_key(elt)
|
2000-02-10 00:43:22 +00:00
|
|
|
def remove(self, elt):
|
|
|
|
del self.elts[elt]
|
2000-02-04 00:28:21 +00:00
|
|
|
|
|
|
|
class Stack:
|
|
|
|
def __init__(self):
|
2000-02-21 22:46:00 +00:00
|
|
|
self.stack = []
|
|
|
|
self.pop = self.stack.pop
|
2000-02-10 20:54:27 +00:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.stack)
|
2000-02-04 00:28:21 +00:00
|
|
|
def push(self, elt):
|
2000-02-21 22:46:00 +00:00
|
|
|
self.stack.append(elt)
|
2000-02-04 00:28:21 +00:00
|
|
|
def top(self):
|
2000-02-21 22:46:00 +00:00
|
|
|
return self.stack[-1]
|