#๐Ÿ”’ Need help with relation to Hashmaps and tests regarding it

11 messages ยท Page 1 of 1 (latest)

devout mist
#

I'm having trouble with the expected behavior of my add, connect, subnets, and undo functions in the Gadget class. Despite implementing the operations as outlined, my tests don't match the expected results. Specifically, Iโ€™m struggling to correctly manage the subsize when adding nodes, properly track and merge clusters in connect, and correctly revert operations in undo. Could you clarify how these operations should interact with the NetworkWithUndo class, especially when managing the undos stack and ensuring that node connections and disconnections are handled efficiently?" This type of question will help you focus on understanding how to handle the core functionality of the Gadget class and debug issues with the interaction between your operations.

oblique jewelBOT
#

@devout mist

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

devout mist
#

So essentially, I was tasked with 3 challenges and I got the first 2 to run

#

however the 3rd question gives me a different answer as opposed to what it should be

#

class Gadget:
    def __init__(self):
        self.inNetwork = NetworkWithUndo(0)
        self.subsize = 0
        self.nameMap = {}
        self.undos = Stack()
        self.helper = {}  # Dictionary to track steps for each name
        
    def getSize(self):
        return self.inNetwork.getSize()
        
    def isIn(self, name):
        return name in self.nameMap
        
    def add(self, name):
        if name not in self.nameMap:
            newIndex = len(self.nameMap)
            self.nameMap[name] = newIndex
            self.inNetwork.add()
            self.subsize += 1
            self.undos.push(("rem", 1, name))
            self.helper[name] = self.undos.size
    
    def connect(self, name1, name2):
        if name1 not in self.nameMap or name2 not in self.nameMap:
            return True
        idx1 = self.nameMap[name1]
        idx2 = self.nameMap[name2]
        root1 = self._find_root(idx1)
        root2 = self._find_root(idx2)
        if root1 == root2:
            return True
        self.inNetwork.merge(root1, root2)
        self.subsize -= 1
        self.undos.push(("brk", 3, None))
        return False
        
    def clean(self, name):
        if name in self.helper:
            steps = self.undos.size - self.helper[name] + 1
            self.undo(steps)
        
    def subnets(self):
        self.undos.push(("oth", self.inNetwork.getSize(), None))
        clusters = {}
        for name, idx in self.nameMap.items():
            root = self._find_root(idx)
            if root not in clusters:
                clusters[root] = []
            clusters[root].append(name)
        return list(clusters.values())
    
    def _find_root(self, idx):
        while self.inNetwork.inArray.get(idx) >= 0:
            idx = self.inNetwork.inArray.get(idx)
        return idx
        
    def undo(self, n):
        while n > 0 and self.undos.size > 0:
            op, count, name = self.undos.pop()
            if op == "rem":
                if name in self.nameMap:
                    del self.nameMap[name]
                if name in self.helper:
                    del self.helper[name]
                self.subsize -= 1
            elif op == "brk":
                self.subsize += 1
            for _ in range(count):
                self.inNetwork.undo()
            n -= 1
            if len(self.nameMap) == 0:
                break
        
        # Rebuild nameMap to ensure consistency
        new_nameMap = {}
        for name, idx in self.nameMap.items():
            if idx < self.inNetwork.getSize():
                new_nameMap[name] = idx
        self.nameMap = new_nameMap
                
    def toArray(self):
        A = self.inNetwork.toArray()
        result = []
        for i in range(len(A)):
            name = next((s for s, idx in self.nameMap.items() if idx == i), None)
            if name:
                result.append((name, A[i]))
            else:
                result.append((str(i), A[i]))
        return result
        
    def __str__(self):
        return str(self.toArray()) + "\n-> " + str(self.nameMap) + "\n-> " + str(self.undos)```
#

This is my tests

# minimal tests Gadget

def tprint(g,i,s=""):
    print("\n=== Test",i,"===")
    if s!="": print(s)
    print(g,"\nsize",g.getSize(),"subsize",g.subsize)

g = Gadget()
A = ["128.0.0.1", "216.58.204.68", "212.58.235.1", "qmul", "Nikos.1", "Nikos.2", "Edon.1", "Shitong.1"]
for x in A: g.add(x)
tprint(g,0)
g.add("Nikos.1")
tprint(g,1)
x = g.connect("Nikos.1","Nikos.2")
tprint(g,2,"connected N.1 N.2: "+str(x))
x = g.connect("Nikos.1","Edon.1")
tprint(g,3,"connected N.1 E.1: "+str(x))
x = g.subnets()
tprint(g,4,"subnets: "+str(x))
g.clean("Nikos.1")
tprint(g,5,"Cleaning Nikos.1")
g.add("Nikos.3")
tprint(g,6,"Add Nikos.3")
A = g.toArray()
for (s,_) in A:
    g.connect("Nikos.3",s)
tprint(g,7,"Connect all to Nikos.3")
x = g.subnets()
tprint(g,8,"subnets: "+str(x))```
#

Please help me as soon as possible

#

I would be very indebted to you and would even boost your server if allowed

oblique jewelBOT
#

@devout mist

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.