def main(): testHashToIndex() print("Starting with empty hash table") hash = HashTable() hash.put("A key", "a value") assert hash.get("A key") == "a value" assert hash.get("A key") != "another value"
class Node(object): def __init__(self, location): self.edges = HashTable(10) self.location = location #Time Complexity: O(n) where n is the number of edges def addEdge(self, edge): self.edges.put(int(edge.id), edge) #Time Complexity: O(n) where n is the number of edges def findEdge(self, id): return self.edges.get(id) #Time Complexity: O(n) where n is the number of edges def getDistance(self, location): return self.edges.get(location.id).weight
class Graph(object): def __init__(self): self.nodes = HashTable(10) # Add a vertex to the graph # Time Complexity: O(n) def addNode(self, location): self.nodes.put(int(location.id), Node(location)) # Add edge between two nodes bidirectionally # Time Complexity: O(n) def addWeightedEdge(self, origin, terminus, weight): originEdge = Edge(origin, weight) terminusEdge = Edge(terminus, weight) self.nodes.get(origin.id).addEdge(terminusEdge) self.nodes.get(terminus.id).addEdge(originEdge) # Get id of a vertex at a location # Time Complexity: O(n) def getNode(self, location): return self.nodes.get(location.id) # Get first node returned by location name: O(n) def getNodeByAddress(self, address): for node in self.nodes.getAll(): if (node.location.address == address): return node # Get distance between two lcoations def getDistanceBetween(self, origin, terminus): return self.nodes.get(origin.id).getDistance(terminus) # Get nearest neighbor of a location # Returns the closest location to the origin # Time Complexity: O(n * m) def getNearestNeighbor(self, origin, group): resultEdges = [] originNode = self.getNodeByAddress(origin.address) originEdges = originNode.edges.getAll() for location in group: for edge in originEdges: if location == edge.location: resultEdges.append(edge) nearestNeighbor = min(resultEdges, key=lambda x: x.weight) return nearestNeighbor.location
class test_hash(unittest.TestCase): def setUp(self): self.hash = HashTable(11) def test_put(self): self.hash.put("Lindsey Raymond", "9786217554") self.assertEqual(self.hash.keys[3], "Lindsey Raymond") self.assertEqual(self.hash.data[3], "9786217554") def test_get(self): self.assertEqual(self.hash.get("Lindsey Raymond"), "9786217554") def test_size(self): self.assertEqual(self.hash.length, 11) def test_get(self): self.hash["James Hayward"] = "123456789" self.hash.put("Lindsey Raymond", "9786217554") self.assertEqual(self.hash["Lindsey Raymond"], self.hash.get("Lindsey Raymond")) self.assertEqual(self.hash["James Hayward"], "123456789")
def test_get_non_string_key_raises_error(): """Test taht get with a non string key raises error.""" from hash import HashTable, additive_hash t = HashTable(10, additive_hash) with pytest.raises(TypeError): t.get(22)
def test_get_raises_error_if_key_not_not_in_table(): """Test that keyerror raised if key not in table.""" from hash import HashTable, additive_hash t = HashTable(10, additive_hash) with pytest.raises(KeyError): t.get('key')
def test_get_returns_value_of_key_provided(): """Test that get function returns the value of key provided.""" from hash import HashTable, additive_hash t = HashTable(10, additive_hash) t.set('word', 444) assert t.get('word')
def test_hash_fill_table(read_test_lexicon): ht = HashTable() for i in read_test_lexicon: ht.set(i) for i in read_test_lexicon: assert ht.get(i) == i