the child function does not overwrite the parent function

So, after trials and errors and googling, here’s my answer. Please feel free to comment what can be modified!

class Stack:

    def __init__(self):
        self.__list= []
 
    def isEmpty(self):
        return self.__list == []

    def size(self):
        return len(self.__list)

    def clear(self):
        self.__list.clear()

    def push(self, item):
        self.__list.append(item)

    def pop(self): # popTail
        if self.isEmpty():
            return None
        else:
            return self.__list.pop()

    def get(self):
        if self.isEmpty():
            return None
        else:
            return self.__list[-1]

    def __str__(self):
        output = '<'
        for i in range( len(self.__list) ):
            item = self.__list[i]
            if i < len(self.__list)-1 :
                output += f'{str(item)}, '
            else:
                output += f'{str(item)}'
        output += '>'
        return output

class Stack3(Stack):
    def __init__(self,list):
        super().__init__()
        self.__list = []
        
    def push(self, item):
        super().push(item)
        self.__list.insert(0,item)
        
    def isEmpty(self):
        super().isEmpty()
        return self.__list == []
        
    def pop(self):
        super().pop()
        if self.isEmpty():
            return None
        else:
            return self.__list.pop(0)
        
    def __str__(self):
        output = '<'
        for i in range( len(self.__list) ):
            item = self.__list[i]
            if i < len(self.__list)-1 :
                output += f'{str(item)}, '
            else:
                output += f'{str(item)}'
        output += '>'
        return output
    
# main programme    
s= Stack3(Stack())
print(s.pop())

for i in range(1,6):
    s.push(i)

print('Content of stack =',s)
print('Item at top=',s.get())
print('Size=', s.size())
while not s.isEmpty():
    print(s.pop())
    print(s)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top