Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python, Functional Programming: I need help with my iterator class and it's methods. The idea is that I can turn any of my hash array

Python, Functional Programming: I need help with my iterator class and it's methods. The idea is that I can turn any of my hash array mapped trie objects into an iterable object and use sytem functions like map on it. I know that I need to take whatever my root node's (firstitem) non-None children and put them into a list and the repeat the process on each child, but I can't seem to be able to turn the hamt object into an object of HamtIter.
from functools import reduce
class hamt:
DEG =4 # Children per node (can be set to any power of 2)
BITS =2 # Should be log2(DEG), bits needed to select child
MASK =0b11 # Should be BITS one-bits
def __init__(self, item = None, children = None):
self._item = item
if children == None:
self._children =[None]* hamt.DEG # List of DEG Nones
else:
self._children = children
# returns copy of self node with child i set to node x
def updatechild(self, i, x):
updatedchildlist = list(self._children) # copy self's child list
updatedchildlist[i]= x # update child i
return hamt(self._item, updatedchildlist) # build and return new node
# Returns reference to new root if change made, else None
def _add(self, item, hashbits):
if self._item == item:
# This node matches item. Return None to indicate no change.
return None
else:
# Continue search using hashbits to pick direction
child_num = hashbits & hamt.MASK
if self._children[child_num]== None:
# item not in trie. Add it as new leaf node.
return self.updatechild(child_num, hamt(item))
else:
# Ask appropriate child to add item, receive updated reference
shiftedhashbits = hashbits >> hamt.BITS
newchild = self._children[child_num]._add(item, shiftedhashbits)
if newchild == None:
return None
else:
return self.updatechild(child_num, newchild)
def add(self, item):
# Pass item and hashbits to private recursive helper
return self._add(item, hash(item))
# Returns True if trie has no items, else False. Rutime O(1).
def empty(self):
return self._item == None and all(child == None for child in self._children)
# Returns True if item in trie, else False. Expected rutime O(log n).
def contains(self, item):
if self._item == item:
return True
else:
child_num = hash(item) & hamt.MASK
if self._children[child_num]== None:
return False
else:
shiftedhashbits = hash(item)>> hamt.BITS
return self._children[child_num].contains(item, shiftedhashbits)
# Returns number of items in trie. Runtime O(n).
def __len__(self):
if self._item == None:
return 0
else:
return sum(1 for child in self._children if child != None)+1
# Returns HAMT that is union of self and other. Expected rutime O(n log n).
def union(self, other):
result = hamt()
for item in self:
result = result.add(item)
for item in other:
result = result.add(item)
return result
def __iter__(self):
return HamtIter(self)
# Adds self's item string to acc list, then ask each child to do the same
def _str(self, acc):
if self._item != None: # Don't do anything in fake root node
acc.append(str(self._item))
for child in self._children:
if child != None:
child._str(acc)
class HamtIter:
def __init__(self, firstitem):
def __next__(self):

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions