Jeremy Hylton | 65d4ea0 | 2000-02-16 00:49:47 +0000 | [diff] [blame^] | 1 | import types |
| 2 | |
| 3 | def flatten(tup): |
| 4 | elts = [] |
| 5 | for elt in tup: |
| 6 | if type(elt) == types.TupleType: |
| 7 | elts = elts + flatten(elt) |
| 8 | else: |
| 9 | elts.append(elt) |
| 10 | return elts |
| 11 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 12 | class Set: |
| 13 | def __init__(self): |
| 14 | self.elts = {} |
Jeremy Hylton | 95b8614 | 2000-02-10 20:54:27 +0000 | [diff] [blame] | 15 | def __len__(self): |
| 16 | return len(self.elts) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 17 | def add(self, elt): |
| 18 | self.elts[elt] = elt |
| 19 | def items(self): |
| 20 | return self.elts.keys() |
| 21 | def has_elt(self, elt): |
| 22 | return self.elts.has_key(elt) |
Jeremy Hylton | 69926ea | 2000-02-10 00:43:22 +0000 | [diff] [blame] | 23 | def remove(self, elt): |
| 24 | del self.elts[elt] |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 25 | |
| 26 | class Stack: |
| 27 | def __init__(self): |
| 28 | self.stack = [] |
| 29 | self.pop = self.stack.pop |
Jeremy Hylton | 95b8614 | 2000-02-10 20:54:27 +0000 | [diff] [blame] | 30 | def __len__(self): |
| 31 | return len(self.stack) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 32 | def push(self, elt): |
| 33 | self.stack.append(elt) |
| 34 | def top(self): |
| 35 | return self.stack[-1] |