49 lines
1.0 KiB
ReStructuredText
49 lines
1.0 KiB
ReStructuredText
>>> def most_significant(value):
|
|
... while value >= 10:
|
|
... value //= 10
|
|
... return value
|
|
|
|
>>> most_significant(12345)
|
|
1
|
|
>>> most_significant(99)
|
|
9
|
|
>>> most_significant(0)
|
|
0
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
>>> def add(collection, key, value):
|
|
... index = most_significant(key)
|
|
... collection[index].append((key, value))
|
|
|
|
>>> def contains(collection, key):
|
|
... index = most_significant(key)
|
|
... for k, v in collection[index]:
|
|
... if k == key:
|
|
... return True
|
|
... return False
|
|
|
|
# Create the collection of 10 lists
|
|
|
|
>>> collection = [[], [], [], [], [], [], [], [], [], []]
|
|
|
|
# Add some items, using key/value pairs
|
|
|
|
>>> add(collection, 123, 'a')
|
|
>>> add(collection, 456, 'b')
|
|
>>> add(collection, 789, 'c')
|
|
>>> add(collection, 101, 'c')
|
|
|
|
# Look at the collection
|
|
|
|
>>> collection
|
|
[[], [(123, 'a'), (101, 'c')], [], [],
|
|
[(456, 'b')], [], [], [(789, 'c')], [], []]
|
|
|
|
# Check if the contains works correctly
|
|
|
|
>>> contains(collection, 123)
|
|
True
|
|
>>> contains(collection, 1)
|
|
False
|