https://pollev.com/slade You may also download the app to your phone. Use the "slade" poll id.
hw4: staff solution for myhash had errors:
def __eq__(self, other):
if type(other) != type(self):
return False
if self.table == other.table:
return True
else:
return False
The eq method compares lists, in which order matters. However, for the
hash table buckets, the order does not matter.
>>> from hw4a import *
>>> h = myhash(5)
>>> h.put('a',1)
>>> h.put('f',2)
>>> h2 = myhash(5)
>>> h2.put('f',2)
>>> h2.put('a',1)
>>> str(h)
"myhash([[('a', 1), ('f', 2)], [], [], [], []])"
>>> str(h2)
"myhash([[('f', 2), ('a', 1)], [], [], [], []])"
>>> h == h2
False
>>>
You might want to use flatten as a set comprehension, so that order does not matter.
def flatten(matrix):
return {item for row in matrix for item in row}
I have fixed the problem in hw4a.pyc. Note that the tables do not need to be the same size.
>>> from hw4a import *
>>> h = myhash()
>>> h.put('a',1)
>>> h.put('b',2)
>>> h2 = myhash(5)
>>> h2.put('b',2)
>>> h2.put('a',1)
>>> str(h)
"myhash([[], [], [], [], [], [], [], [], [], [], [('a', 1)], [('b', 2)], [], [], [], [], [], [], [], []])"
>>> str(h2)
"myhash([[('a', 1)], [('b', 2)], [], [], []])"
>>> h == h2
True
There is a different problem with the copy method.
def copy(self):
h = myhash(self.size)
h.table = self.table[:]
h.count = self.count
return h
This code makes a shallow copy of the list. We need to make a deep copy
of the embedded lists. Otherwise, there are problems. You can use Python Tutor
to demonstrate.
from hw4a import *
>>> h = myhash(5)
>>> h.put('a', 1)
>>> h2 = h.copy()
>>> str(h)
"myhash([[('a', 1)], [], [], [], []])"
>>> str(h2)
"myhash([[('a', 1)], [], [], [], []])"
>>> h.put('b',2)
>>> str(h)
"myhash([[('a', 1)], [('b', 2)], [], [], []])"
>>> str(h2)
"myhash([[('a', 1)], [('b', 2)], [], [], []])"
The current staff solution, hw4a.pyc, fixes these issues.
There will be a UNIX question, as in the first midterm. Here is a sample UNIX transcript (solutions) UNIX will cover through principle 4.
There was a question about overflow in the hash function, djb2:
The djb2 hash function inherently relies on integer overflow for its operation, particularly when implemented in languages like C or C++ where unsigned integer types exhibit defined wrap-around behavior on overflow.How djb2 handles overflow:
Initialization: The hash starts with an arbitrary large prime number, 5381.
Iteration: For each character c in the input string, the current hash value hash is updated using the formula:
hash = ((hash << 5) + hash) + c.
This is equivalent to hash = hash * 33 + c.Overflow as a feature: As the hash value grows with each character, it will eventually exceed the maximum value representable by the chosen unsigned integer type (e.g., unsigned int, unsigned long). When this occurs, the value "wraps around" or "overflows" according to modular arithmetic rules inherent to unsigned integer types in C/C++. This wrap-around behavior is a core part of the djb2 algorithm, contributing to its non-linear nature and helping to distribute hash values.
Implications of overflow:
Non-linearity: The overflow ensures that the hash function is not simply a linear accumulation, making it harder to predict output values and reducing the likelihood of simple collisions.
Fixed-size output: Regardless of the input string length, the final hash value will always fit within the chosen unsigned integer type, providing a fixed-size hash.
Consistency: The predictable wrap-around behavior of unsigned integer overflow ensures that the same input string will always produce the same hash value, which is crucial for a hash function.
I recorded a lecture on Friday which covers iterators and decorators.
>>> time.time() 1761309320.6466186 >>> time.time() 1761309374.0827036
Key aspects of time.time():Unix Epoch: This is a fixed point in time used as a reference for measuring time in many systems. It is defined as January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC).
Seconds since Epoch: The returned value is a float, providing a high-precision measurement of elapsed seconds since this epoch.