Mercurial > public > think_complexity
comparison ch3ex4.py @ 13:be7f2cd15faf
Completing Ch 3.4 exercise 4.1.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 10 Dec 2012 19:42:38 -0600 |
parents | b75bf8bfa2cf |
children | bc2c07a059be |
comparison
equal
deleted
inserted
replaced
12:b75bf8bfa2cf | 13:be7f2cd15faf |
---|---|
40 """Appends (n) LinearMaps onto (self).""" | 40 """Appends (n) LinearMaps onto (self).""" |
41 self.maps = [] | 41 self.maps = [] |
42 for i in range(n): | 42 for i in range(n): |
43 self.maps.append(LinearMap()) | 43 self.maps.append(LinearMap()) |
44 | 44 |
45 def __len__(self): | |
46 """Returns the number of LinearMap buckets in the map.""" | |
47 return len(self.maps) | |
48 | |
45 def find_map(self, k): | 49 def find_map(self, k): |
46 """Finds the right LinearMap for key (k).""" | 50 """Finds the right LinearMap for key (k).""" |
47 index = hash(k) % len(self.maps) | 51 index = hash(k) % len(self.maps) |
48 return self.maps[index] | 52 return self.maps[index] |
49 | 53 |
76 or raises KeyError if the key is not found.""" | 80 or raises KeyError if the key is not found.""" |
77 return self.maps.get(k) | 81 return self.maps.get(k) |
78 | 82 |
79 def add(self, k, v): | 83 def add(self, k, v): |
80 """Resize the map if necessary and adds the new item.""" | 84 """Resize the map if necessary and adds the new item.""" |
81 if self.num == len(self.maps.maps): | 85 if self.num == len(self.maps): |
82 self.resize() | 86 self.resize() |
83 | 87 |
84 self.maps.add(k, v) | 88 self.maps.add(k, v) |
85 self.num += 1 | 89 self.num += 1 |
86 | 90 |