标签:serve items nta necessary test 不可 分布 lower 停止
>>> A = set([0,1,2]) >>> B = set() >>> 1 in A True >>> A & B set() >>> B.add(1) >>> B.add(1) >>> B.add(5) >>> B {1, 5} >>> A & B {1} >>> A | B {0, 1, 2, 5} >>> A - B {0, 2} >>> B.remove(5) >>> B {1} >>> B.issubset(A) True >>> for item in A: print(item, end=" ") 0 1 2 >>> |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: abstractset.py """
class AbstractSet(object): """Generic set method implementations."""
def __or__(self, other): """Return the union of self and other.""" return self + other
def __and__(self, other): """Return the intersection of self and other.""" intersection = type(self)() for item in self: if item in other: intersection.add(item) return intersection
def __sub__(self, other): """Return the difference of self and other.""" difference = type(self)() for item in self: if item not in other: difference.add(item) return difference
def issubset(self, other): """Returns True if self is a subset of other or False otherwise.""" for item in self: if item not in other: return False return True |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: arrayset.py """
from arraybag import ArrayBag from abstractset import AbstractSet
class ArraySet(ArrayBag, AbstractSet): """An array-based implementation of a set"""
def __init__(self, sourceCollection=None): """Sets the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" ArrayBag.__init__(self, sourceCollection)
def add(self, item): """Add item to the set if it is not in the set.""" if item not in self: ArrayBag.add(self, item) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File name: linkedset.py """
from linkedbag import LinkedBag from abstractset import AbstractSet
class LinkedSet(AbstractSet, LinkedBag): """An linked-based set implementation"""
# Constructor method def __init__(self, sourceCollection = None): """Set the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" LinkedBag.__init__(self, sourceCollection)
# Mutator method def add(self, item): """Adds item to self, avoid deplicate item.""" # Check array memory here and increase it if necessary. if item not in self: LinkedBag.add(self, item) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: testset.py A simple test program """
from arrayset import ArraySet from linkedset import LinkedSet
def testSet(setType): print("Add [0, 1, 2] to A.") A = setType([0, 1, 2]) B = setType() print("1 in A, Expect True: ", end="") print(1 in A) print("A & B, expect ‘{}‘:", A & B) print("Adding 1 1 5 to B") B.add(1) B.add(1) B.add(5) print("B: ", B) print("A & B, expect ‘{1}‘: ", A & B) print("A | B: ", A | B) print("A - B: ", A - B) print("Remove 5 from B") B.remove(5) print("Expect ‘{1}‘: ", B) print("B is s subset of A?, expect True:", B.issubset(A)) print("Overview of A:", end="") for item in A: print(item, end=" ")
# testSet(ArraySet) testSet(LinkedSet) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
"""" File: abstractdict.py """
from abstractcollection import AbstractCollection
class Item(object): """Represents a dictionary item. Supports comparision by key."""
def __init__(self, key, value): self.key = key self.value = value
def __str__(self): return str(self.key) + ":" + str(self.value)
def __eq__(self, other): if type(self) != type(other): return False return self.key == other.key
def __lt__(self, other): if type(self) != type(other): return False return self.key < other.key
def __le__(self, other): if type(self) != type(other): return False return self.key <= other.key
class AbstractDict(AbstractCollection): """Common data and method implementations for dictionaries."""
def __init__(self, sourceCollection=None): """Will copy item to collection from sourceCollection if it‘s present.""" AbstractCollection.__init__(self) if sourceCollection: for key, value in sourceCollection: self[key] = value
def __str__(self): return "{" + ", ".join(map(str, self.items())) + "}"
def __add__(self, other): """Return a new dictionary containing the contents of self and other. if self.key == other.key, use the other.value""" result = type(self)(map(lambda item: (item.key, item.value), self.items())) for key in other: result[key] = other[key] return result
def __eq__(self, other): """Return True if self equals other, or False otherwise.""" if self is other: return True if type(self) != type(other) or len(self) != len(other): return False for key in self: if key not in other: return False else: if self[key] != other[key]: return False return True
def keys(self): """Returns an iterator on keys in the dictionary.""" return iter(self)
def values(self): """Return an iterator on values in the dictionary.""" return map(lambda key: self[key], self)
def items(self): """Returns an iterator on the items in the dictionary.""" return map(lambda key: Item(key, self[key]), self) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
from abstractdict import AbstractDict, Item from arraylist import ArrayList
class ArrayDict(AbstractDict): """Represents an array-list based dictionary."""
def __init__(self, sourceCollection=None): """Will copy items to collection from sourceCollection if it‘s present.""" self._items = ArrayList() self._index = -1 AbstractDict.__init__(self, sourceCollection)
# Accessor def __iter__(self): """Serves up the key in the dictionary.""" cursor = 0 while cursor < len(self): yield self._items[cursor].key cursor += 1
def __getitem__(self, key): """Precondition: the key is in the dictionary Raise KeyError if the key is not in the dictionary Return the value associated with the key. """ if key not in self: raise KeyError("Missing: " + str(key)) return self._items[self._index].value
def __contains__(self, item): """Set the self._index to the target position if item is in self.""" self._index = 0 for entry in self._items: if entry.key == item: return True self._index += 1 self._index = -1 return False
# Mutator def __setitem__(self, key, value): """If the key is not in the dictionary, adds the key and value to it, otherwise, replace the old value with the new one."""
if key not in self: self._items.insert(len(self), Item(key, value)) self._size += 1 else: self._items[self._index].value = value
def pop(self, key): """Precondition: the key is in the dictionary. Raise: KeyError if the key is not in the dictionary. Remove the key and return the associated value if the key is in the dictionary.""" if key not in self: raise KeyError("Missing: " + str(key)) self._size -= 1 return self._items.pop(self._index).value |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
from abstractdict import AbstractDict, Item from node import Node
class LinkedDict(AbstractDict): """Represents an linked based dictionary."""
def __init__(self, sourceCollection=None): """Will copy items to collection from sourceCollection if it‘s present.""" self._head = None self._priorNode = self._foundNode = None AbstractDict.__init__(self, sourceCollection)
# Accessor def __iter__(self): """Serves up the key in the dictionary.""" probe = self._head while probe is not None: yield probe.data.key probe = probe.next
def __contains__(self, key): self._priorNode = None self._foundNode = self._head while self._foundNode is not None: if self._foundNode.data.key == key: return True self._priorNode = self._foundNode self._foundNode = self._foundNode.next return False
def __getitem__(self, key): """Precondition: the key is in the dictionary Raise KeyError if the key is not in the dictionary Return the value associated with the key. """ if key not in self: raise KeyError("Missing: " + str(key)) return self._foundNode.data.value
# Mutator def __setitem__(self, key, value): """If the key is not in the dictionary, adds the key and value to it, otherwise, replace the old value with the new one.""" if key not in self: newNode = Node(Item(key, value), self._head) self._head = newNode self._size += 1 else: self._foundNode.data.value = value
def pop(self, key): """Precondition: the key is in the dictionary. Raise: KeyError if the key is not in the dictionary. Remove the key and return the associated value if the key is in the dictionary.""" if key not in self: raise KeyError("Missing: " + str(key)) self._size -= 1 if self._priorNode is None: self._head = self._head.next else: self._priorNode.next = self._foundNode.next return self._foundNode.data.value |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: testdict.py """
from arraydict import ArrayDict from linkeddict import LinkedDict
def test(dictType): print("Adding [(‘Ming‘, ‘AC1644‘), (‘Qing‘, ‘AC1911‘), (‘Tang‘, ‘AC960‘)] to dict A") A = dictType([(‘Ming‘, ‘AC1644‘), (‘Qing‘, ‘AC1911‘), (‘Tang‘, ‘AC960‘)]) print("Print A: ", str(A))
print("\nSet B = A") B = dictType() for key in A: B[key] = A[key] print("Print B: ", str(B)) print("B == A:, expect True: ", B == A)
print("\nGet Ming‘s value:", A["Ming"])
print("\nAdding (‘Qin‘, ‘BC221‘) to A") A[‘Qin‘] = ‘BC221‘ print("Print A: ", str(A)) print("B == A, expect False:", B == A)
print("\nSet B == A") B = dictType() for key in A: B[key] = A[key] print("Print B: ", str(B))
print("\nReplace ‘Tang‘ with the value AC907.") A[‘Tang‘] = ‘AC907‘ print("Print A: ", str(A)) print("B == A, expect False:", B == A)
print("\nRemove Qing.") print(A.pop("Qing")) print("Print A: ", str(A)) print("Remove Qin.") print(A.pop("Qin")) print("Print A: ", str(A))
print("\nTest the iterator of keys.") for key in A: print(key, end="->") print("\b\b") for key in A.keys(): print(key, end="->") print("\b\b")
print("\n Test the iterator of values.") for value in A.values(): print(value, end="->") print("\b\b")
print("\n Test the iterator of items.") for item in A.items(): print(item, end="->") print("\b\b")
print("\nThe length of dictionary A: ", len(A))
print("\nTest __getitem__ precondition.") try: print(A["Song"]) except KeyError as err: print("KeyError: " + str(err))
print("\nTest pop precondition.") try: print(A.pop("Yuan")) except KeyError as err: print("KeyError: " + str(err))
# test(ArrayDict) test(LinkedDict) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
def keysToIndexes(keys, n): """Return the indexes corresponding to the keys for an array of length n.""" return list(map(lambda key: key % n, keys)) |
>>> keysToIndexes([3, 5, 8, 10], 4 ) [3, 1, 0, 2] >>> keysToIndexes([3, 4, 8, 10], 4 ) [3, 0, 0, 2] >>> keysToIndexes([3, 5, 8, 10], 8 ) [3, 5, 0, 2] >>> keysToIndexes([3, 4, 8, 10], 8 ) [3, 4, 0, 2] |
>>> keysToIndexes([10, 20, 30, 40, 50, 60, 70], 15 ) [10, 5, 0, 10, 5, 0, 10] >>> keysToIndexes([10, 20, 30, 40, 50, 60, 70], 11 ) [10, 9, 8, 7, 6, 5, 4] |
def stringHash(item): """Generates an integer key from a string.""" if len(item) > 4 and (item[0].islower() or item[0].isupper()): # Drop first letter item = item[1:] sum = 0 for ch in item: sum += ord(ch) if len(item) > 2: # Subtract last ASCII sum -= 2 * ord(item[-1]) return sum |
>>> stringHash("cinema") 328 >>> stringHash("iceman") 296 |
def keysToIndexes(keys, n, hash= lambda key: key): """Return the indexes corresponding to the keys for an array of length n.""" return list(map(lambda key: hash(key) % n, keys)) |
>>> keysToIndexes([3, 5, 8, 10], 4) [3, 1, 0, 2] >>> keysToIndexes(["cinema", "iceman"], 2, stringHash) [0, 0] >>> keysToIndexes(["cinema", "iceman"], 3, stringHash) [1, 2] |
>>> keysToIndexes(["cinema", "iceman"], 2, hash) [1, 0] >>> keysToIndexes(["cinema", "iceman"], 2, stringHash) [0, 0] |
# Get the home index index = abs(hash(item)) % len(table)
# Stop searching when an empty cell is encountered while table[index] not in (EMPTY, DELETED): # Increment the index and wrap around to the first position if necessary index = (index + 1) % len(table)
# An empty cell is found, so store the item. tabel[index] = item |
# Set the initial key, index, and distance key = abs(hash(item)) distance = 1 homeIndex = key % len(table)
# Stop searching when an unoccupied cell is encountered while table[index] not in (EMPTY, DELETED): # Increment the index and warp around to the first position if necessary. index = (homeIndex + distance ** 2) % len(table) distance += 1
# An empty cell is found, so store the item. tabel[index] = item |
# Get the home index index = abs(hash(item)) % len(table)
# Access a bucket and store the item at the head of its linked list table[index] = Node(item, table[index]) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: hasetable.py """
from arrays import Array
class HashTable(object): """Represent a hash table"""
EMPTY = None DELETED = True
def __init__(self, capacity=29, hashFunction=hash, liner=True): self._table = Array(capacity, HashTable.EMPTY) self._size = 0 self._hash = hashFunction self._homeIndex = -1 self._actualIndex = -1 self._liner = liner self._probeCount = 0
# Accessor method def __len__(self): return self._size
def loadFactor(self): return self._size / len(self._table)
def homeIndex(self): return self._homeIndex
def actualIndex(self): return self._actualIndex
def probeCount(self): return self._probeCount
# Mutator method def insert(self, item): """Insert item into the table. Precondition: There is at least one empty cell or one previously occupied cell. There is not a duplicate item.""" self._probeCount = 0 # Get the home index self._homeIndex = self._hash(item) % len(self._table) distance = 1 index = self._homeIndex
# Stop searching when an empty cell in encountered while self._table[index] not in (HashTable.EMPTY, HashTable.DELETED): # Increment the index and wrap around to the first position if necessary. if self._liner: increment = index + 1 else: # Quadratic probing increment = index + distance ** 2 distance += 1 index = increment % (len(self._table)) self._probeCount += 1
# An empty cell is found, so store the item self._table[index] = item self._size += 1 self._actualIndex = index
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: profiler.py """
from hashtable import HashTable
class Profiler(object): """Represent a profiler for hash table."""
def __init__(self): self._table = None self._collisions = 0 self._probeCount = 0 self._result = ""
def test(self, table, data): """Inserts the data into table and gathers statistics""" self._table = table self._collisions = 0 self._probeCount = 0 self._result = "Load Factor | Item Inserted | Home Index | Actual Index | Probes\n" for item in data: loadFactor = table.loadFactor() table.insert(item) homeIndex = table.homeIndex() actualIndex = table.actualIndex() probes = table.probeCount() self._probeCount += probes if probes > 0: self._collisions += 1 self._result += "%8.3f%14d%12d%12d%14d" % (loadFactor, item, homeIndex, actualIndex, probes)\ + "\n" self._result += "Total collisions: " + str(self._collisions) + \ "\nTotal probes: " + str(self._probeCount) + \ "\nAverage probes per collision: " + str(self._probeCount / self._collisions)
def __str__(self): if self._table is None: return "No test has been run yet." else: return self._result
def collisions(self): return self._collisions
def probeCount(self): return self._probeCount
def main(): # Create a table with 8 cells, an identity hash function and liner probing. table = HashTable(8, lambda x: x) data = list(range(10, 71, 10)) profiler = Profiler() profiler.test(table, data) print(profiler)
if __name__ == "__main__": main() |
Load Factor | Item Inserted | Home Index | Actual Index | Probes 0.000 10 2 2 0 0.125 20 4 4 0 0.250 30 6 6 0 0.375 40 0 0 0 0.500 50 2 3 1 0.625 60 4 5 1 0.750 70 6 7 1 Total collisions: 3 Total probes: 3 Average probes per collision: 1.0 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
from node import Node from arrays import Array from abstractset import AbstractSet from abstractcollection import AbstractCollection
class HashSet(AbstractCollection, AbstractSet): """A hashing implementation of a set."""
DEFAULT_CAPACITY = 3
def __init__(self, sourceCollection=None, capacity=None): if capacity is None: self._capacity = HashSet.DEFAULT_CAPACITY else: self._capacity = capacity self._items = Array(self._capacity) self._foundNode = self._priorNode = None self._index = -1 AbstractCollection.__init__(self, sourceCollection)
# Accessor method def __contains__(self, item): """Return True if item is in the set or False otherwise.""" self._index = hash(item) % len(self._items) self._priorNode = None self._foundNode = self._items[self._index] while self._foundNode is not None: if self._foundNode.data == item: return True else: self._priorNode = self._foundNode self._foundNode = self._foundNode.next return False
def __iter__(self): """Supports iteration over a view of self.""" for item in self._items: while item is not None: yield item.data item = item.next
def __str__(self): """Return a string representation of self""" return "{" + ", ".join(map(str, self)) + "}"
# Mutator methods def clear(self): """Makes self becomes empty.""" self._size = 0 self._items = Array(HashSet.DEFAULT_CAPACITY)
def add(self, item): """Adds item to the set if if is not in the set.""" if item not in self: newNode = Node(item, self._items[self._index]) self._items[self._index] = newNode self._size += 1
def remove(self, item): """Precondition: item is in self. Raise: KeyError if item is not in self. return the removed item if item is in self""" if item not in self: raise KeyError("Missing: " + str(item)) if self._priorNode is None: self._items[self._index] = self._foundNode.next else: self._priorNode.next = self._foundNode.next self._size -= 1 return self._foundNode.data |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: hashdict.py """
from abstractdict import AbstractDict, Item from node import Node from arrays import Array
class HashDict(AbstractDict): """Represents a hash-based dictionary"""
DEFAULT_CAPACITY = 9
def __init__(self, sourceDictionary=None): """Will copy items to collection from sourceDictionary if it‘s present.""" self._array = Array(HashDict.DEFAULT_CAPACITY) self._foundNode = self._priorNode = None self._index = -1 AbstractDict.__init__(self, sourceDictionary)
# Accessor method def __contains__(self, key): """Return True if item is in self, or False otherwise.""" self._index = hash(key) % len(self._array) self._priorNode = None self._foundNode = self._array[self._index] while self._foundNode is not None: if self._foundNode.data.key == key: return True else: self._priorNode = self._foundNode self._foundNode = self._foundNode.next return False
def __iter__(self): """Serves up the key in the dictionary.""" for item in self._array: while item is not None: yield item.data.key item = item.next
def __getitem__(self, key): """Precondition: the key is in the dictionary Raise KeyError if the key is not in the dictionary Return the value associated with the key. """ if key not in self: raise KeyError("Missing: " + str(key)) return self._foundNode.data.value
# Mutator method def __setitem__(self, key, value): """If the key is not in the dictionary, adds the key and value to it, otherwise, replace the old value with the new one.""" if key in self: self._foundNode.data.value = value else: newNode = Node(Item(key, value), self._array[self._index]) self._array[self._index] = newNode self._size += 1
def pop(self, key): """Precondition: the key is in the dictionary. Raise: KeyError if the key is not in the dictionary. Remove the key and return the associated value if the key is in the dictionary.""" if key not in self: raise KeyError("Missing: " + str(key)) if self._priorNode is None: self._array[self._index] = self._foundNode.next else: self._priorNode.next = self._foundNode.next self._size -= 1 return self._foundNode.data.value |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: treesortedset.py """
from linkedbst import LinkedBST from abstractcollection import AbstractCollection from abstractset import AbstractSet
class TreeSortedSet(AbstractCollection, AbstractSet): """A tree-based implementation of a sorted set."""
def __init__(self, sourceCollection=None): self._items = LinkedBST() AbstractCollection.__init__(self, sourceCollection)
def __contains__(self, item): """Return True if item is in the set or False otherwise.""" return item in self._items
def __iter__(self): """Supports iteration over a view of self.""" return self._items.inorder()
def __str__(self): """Return a string representation of self""" return "{" + ", ".join(map(str, self)) + "}"
# Mutator method def add(self, item): """Adds item to the set if if is not in the set.""" if item not in self: self._items.add(item) self._size += 1
def clear(self): """Makes self becomes empty.""" self._size = 0 self._items = LinkedBST()
def remove(self, item): """Precondition: item is in self. Raise: KeyError if item is not in self. return the removed item if item is in self""" if item not in self: raise KeyError("Missing: " + str(item)) self._items.remove(item) self._size -= 1 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: treesorteddict.py """
from linkedbst import LinkedBST from abstractdict import AbstractDict, Item
class TreeSortedDict(AbstractDict): """A tree-based implementation of a sorted dictionary."""
def __init__(self, sourceCollection=None): """Will copy items to collection from sourceCollection if it‘s present.""" self._items = LinkedBST() self._foundItem = None AbstractDict.__init__(self, sourceCollection)
# Accessor def __iter__(self): """Serves up the key in the dictionary.""" for item in self._items.inorder(): yield item.key
def __getitem__(self, key): """Precondition: the key is in the dictionary Raise KeyError if the key is not in the dictionary Return the value associated with the key. """ if key not in self: raise KeyError("Missing: " + str(key)) return self._foundItem.value
def __contains__(self, key): """Set the self._index to the target position if key is in self.""" newItem = Item(key, None) self._foundItem = self._items.find(newItem) if self._foundItem is None: return False else: return True
# Mutator def __setitem__(self, key, value): """If the key is not in the dictionary, adds the key and value to it, otherwise, replace the old value with the new one."""
if key not in self: self._items.add(Item(key, value)) self._size += 1 else: self._foundItem.value = value
def pop(self, key): """Precondition: the key is in the dictionary. Raise: KeyError if the key is not in the dictionary. Remove the key and return the associated value if the key is in the dictionary.""" if key not in self: raise KeyError("Missing: " + str(key)) self._size -= 1 removedItem = self._items.remove(Item(key, None)) return removedItem.value |
数据结构( Pyhon 语言描述 ) — —第11章:集和字典
标签:serve items nta necessary test 不可 分布 lower 停止
原文地址:https://www.cnblogs.com/lijunjie9502/p/10017679.html