Answered step by step
Verified Expert Solution
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 nonNone 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 # Children per node can be set to any power of
BITS # Should be logDEG bits needed to select child
MASK b # Should be BITS onebits
def initself 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 updatechildself i x:
updatedchildlist listselfchildren # copy self's child list
updatedchildlisti x # update child i
return hamtselfitem, updatedchildlist # build and return new node
# Returns reference to new root if change made, else None
def addself 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
childnum hashbits & hamt.MASK
if self.childrenchildnum None:
# item not in trie. Add it as new leaf node.
return self.updatechildchildnum, hamtitem
else:
# Ask appropriate child to add item, receive updated reference
shiftedhashbits hashbits hamt.BITS
newchild self.childrenchildnumadditem shiftedhashbits
if newchild None:
return None
else:
return self.updatechildchildnum, newchild
def addself item:
# Pass item and hashbits to private recursive helper
return self.additem hashitem
# Returns True if trie has no items, else False. Rutime O
def emptyself:
return self.item None and allchild None for child in self.children
# Returns True if item in trie, else False. Expected rutime Olog n
def containsself item:
if self.item item:
return True
else:
childnum hashitem & hamt.MASK
if self.childrenchildnum None:
return False
else:
shiftedhashbits hashitem hamt.BITS
return self.childrenchildnumcontainsitem shiftedhashbits
# Returns number of items in trie. Runtime On
def lenself:
if self.item None:
return
else:
return sum for child in self.children if child None
# Returns HAMT that is union of self and other. Expected rutime On log n
def unionself other:
result hamt
for item in self:
result result.additem
for item in other:
result result.additem
return result
def iterself:
return HamtIterself
# Adds self's item string to acc list, then ask each child to do the same
def strself acc:
if self.item None: # Don't do anything in fake root node
acc.appendstrselfitem
for child in self.children:
if child None:
child.stracc
class HamtIter:
def initself firstitem:
def nextself:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started