making progress
So I got the old dictionary exercise updated so it uses python lists instead of the doubly linked lists....
Here's a test run with the python profile, on a random list build.
def random_list(alist, count):
for x in range(0, count):
number = randint(0, 1000)
alist.set(number, 'spam')
So all it's measuring is how long it's taking for the dictionary to build the list, compared to my binary search tree.
My tree-thing-pole is slow, but it's ordered. I ran the test the first few times and forgot to change the median, so it was real real slow. over 1 sec. Then I remembered I had to change it according to the numbers I was putting in the list, and it went down to averaging .35 , so I'm not feeling to bad about it. I know I'll still have to fix it so it's nodes only, and make it branch, but I wanted to go back and test what I made before I moved on.
Picture my last test run:
Here's the dictionary without the doubly linked list:
It's almost identical to the one from the lesson, but with the python lists in it and the modifications for the list to work instead of the linked list.
class Dictionary(object):
def __init__(self, num_buckets=256):
"""Initializes a Map with the given number of buckets (aka lists)."""
self.map = []
for i in range(0, num_buckets):
self.map.append([])
def hash_key(self, key):
"""Given a key this will create a number and then convert it to
an index for the aMap's buckets.(returns a number between 0-length of self.map)"""
return hash(key) % len(self.map)
def get_bucket(self, key):
"""with the key, this returns the python object in the self.map by using it's bucket_id(index in self.map) given by hash_key"""
bucket_id = self.hash_key(key)
return self.map[bucket_id]
def get_slot(self, key, default=None):
"""
Returns either the bucket and node for a slot, or Bucket, None or None, None
"""
bucket = self.get_bucket(key)
if bucket:
i=0
node = bucket[i]
length = len(node)
for x in range(0, length):
if key == node[0]:
return bucket, node
else:
i += 1
# fall through for both if and while above
return bucket, None
#######################################################################################
def get(self, key, default=None):
""" returns the first tuple item or None """
bucket, node = self.get_slot(key, default=default)
return node and node[1] or None
def set(self, key, value):
"""Places a tuple into the bucket(list inside self.map) that hash-key determined, or replaces one if that key entered already existed."""
bucket, slot = self.get_slot(key)
#print("set")
if slot:
# the key exists, replace it
slot = (key, value)
return 'replace'
else:
#print("set else")
# the key does not, append to create it
bucket.append((key, value))
#print(self.map)
return 'setting'
def delete(self, key):
"""Deletes the given key from the Map."""
bucket = self.get_bucket(key)
i=0
node = bucket[i]
while node:
k = node[0]
if key == k:
bucket.remove(node)
break
i += 1
def ink(self):
"""Prints out what's in the Map."""
xlength = len(self.map)
for x in range(0, xlength):
alist = self.map[x]
if len(alist) > 0:
lengthy = len(alist)
for y in range(0, lengthy):
value=alist[y]
print(value)
def count(self):
"""Counts tuples in the Map. For testing purposes."""
xlength = len(self.map)
amount = 0
for x in range(0, xlength):
alist = self.map[x]
if len(alist) > 0:
lengthy = len(alist)
amount += lengthy
return amount
Here's a test run with the python profile, on a random list build.
def random_list(alist, count):
for x in range(0, count):
number = randint(0, 1000)
alist.set(number, 'spam')
So all it's measuring is how long it's taking for the dictionary to build the list, compared to my binary search tree.
My tree-thing-pole is slow, but it's ordered. I ran the test the first few times and forgot to change the median, so it was real real slow. over 1 sec. Then I remembered I had to change it according to the numbers I was putting in the list, and it went down to averaging .35 , so I'm not feeling to bad about it. I know I'll still have to fix it so it's nodes only, and make it branch, but I wanted to go back and test what I made before I moved on.
Picture my last test run:
Here's the dictionary without the doubly linked list:
It's almost identical to the one from the lesson, but with the python lists in it and the modifications for the list to work instead of the linked list.
class Dictionary(object):
def __init__(self, num_buckets=256):
"""Initializes a Map with the given number of buckets (aka lists)."""
self.map = []
for i in range(0, num_buckets):
self.map.append([])
def hash_key(self, key):
"""Given a key this will create a number and then convert it to
an index for the aMap's buckets.(returns a number between 0-length of self.map)"""
return hash(key) % len(self.map)
def get_bucket(self, key):
"""with the key, this returns the python object in the self.map by using it's bucket_id(index in self.map) given by hash_key"""
bucket_id = self.hash_key(key)
return self.map[bucket_id]
def get_slot(self, key, default=None):
"""
Returns either the bucket and node for a slot, or Bucket, None or None, None
"""
bucket = self.get_bucket(key)
if bucket:
i=0
node = bucket[i]
length = len(node)
for x in range(0, length):
if key == node[0]:
return bucket, node
else:
i += 1
# fall through for both if and while above
return bucket, None
#######################################################################################
def get(self, key, default=None):
""" returns the first tuple item or None """
bucket, node = self.get_slot(key, default=default)
return node and node[1] or None
def set(self, key, value):
"""Places a tuple into the bucket(list inside self.map) that hash-key determined, or replaces one if that key entered already existed."""
bucket, slot = self.get_slot(key)
#print("set")
if slot:
# the key exists, replace it
slot = (key, value)
return 'replace'
else:
#print("set else")
# the key does not, append to create it
bucket.append((key, value))
#print(self.map)
return 'setting'
def delete(self, key):
"""Deletes the given key from the Map."""
bucket = self.get_bucket(key)
i=0
node = bucket[i]
while node:
k = node[0]
if key == k:
bucket.remove(node)
break
i += 1
def ink(self):
"""Prints out what's in the Map."""
xlength = len(self.map)
for x in range(0, xlength):
alist = self.map[x]
if len(alist) > 0:
lengthy = len(alist)
for y in range(0, lengthy):
value=alist[y]
print(value)
def count(self):
"""Counts tuples in the Map. For testing purposes."""
xlength = len(self.map)
amount = 0
for x in range(0, xlength):
alist = self.map[x]
if len(alist) > 0:
lengthy = len(alist)
amount += lengthy
return amount
Comments
Post a Comment