Embercode
✕
Built-in CollectionsQuick quiz

Hashing with dict and set

8 questions. Covers Custom keys in Python.

Questions 1 to 5 of 8

  1. 1. Which statement about a hash collision is correct?
  2. 2. A dict picks a bucket from the hash of a key. A table has 16 buckets, and hash(35) is 35 because Python ints hash to themselves. Which bucket does 35 go to, using hash(35) % 16?
  3. 3. What does this program print? ``python d = {} d[-1] = 'a' d[-2] = 'b' print(len(d)) print(d[-1], d[-2]) print(d.get(-3)) ``
  4. 4. What does this program print? ``python class Tag: def __init__(self, name, hash_value): self.name = name self.hash_value = hash_value def __hash__(self): return self.hash_value def __eq__(self, other): if self is other: return True if not isinstance(other, Tag): return False return self.name == other.name s = set() s.add(Tag('a', 7)) s.add(Tag('b', 7)) s.add(Tag('a', 7)) print(len(s)) print(Tag('b', 7) in s) print(Tag('c', 7) in s) ``
  5. 5. This class is used as a dict key or set item. What happens when the program runs Name('Ana') == 'Ana'? ``python class Name: def __init__(self, first): self.first = first def __eq__(self, other): return self.first == other.first ``

5 question(s) left on this page.