Python Forum

Full Version: Class Stack as Container Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python and have a question regarding stacks as a container class. My class stack looks like this:

class Stack:
     def __init__(self):
         self.items = []

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

     def pop(self):
         return self.items.pop()
Now I want to use class stack as a container class without inheriting it.

class Taskmanger:
    def __init__(self):                 
        self.task = {} 
                  
    def newTask(self,t,p): 
         
    def popTaskwithlowestPriority(self):
I don't know how to do this. Maybe something like this:

stack = Stack("")
but where?
The task comes from a book but the solutions are no longer available as the website no longer exists. Now I would like to know how it works.

Example how the programm should work:

task = Taskmanager()
task.newTask("Clean the bathroom",1)
task.newTask("buy Milk",5)
task.newTask("watch TV",1)
task.popTaskwithlowestPriority()
"Clean the bathroom"
task.popTaskwithlowestPriority()
"watch TV"
The container class for the tasks of a certain priority
shall be the class Stack implemented above.
Thanks in advance for all the help!
Quote:I don't know how to do this. Maybe something like this:
just use stack = Stack()

once instantiated, use like:
stack.push(whatever)
ff = stack.pop()
(Dec-29-2020, 10:38 PM)Larz60+ Wrote: [ -> ]
Quote:I don't know how to do this. Maybe something like this:
just use stack = Stack()

once instantiated, use like:
stack.push(whatever)
ff = stack.pop()

But how? If I do it like this it's says:
class Taskmanger:
    def __init__(self):                 
        self.task = {} 
                   
    def newTask(self,t,p):
        stack = Stack()
        if(p in self.task):
            x = self.task[int(p)]
            x.append(t)
            self.task[int(p)] = x
        else:
            self.task[int(p)] = stack.push(t)

    def popTaskwithlowestPriority(self):
Error Outcome:
'NoneType' object has no attribute 'append'

And now this:
task = Taskmanager()
task.newTask("Clean the bathroom",1)
task.newTask("buy Milk",5)
task.newTask("watch TV",1)
{5: None, 1: None, 2: None}
But the Outcome should be this!!!!!!!!:
{1:["Clean the bathroom","watch TV"],5:["buy Milk"]}
Please show complete unaltered error traceback.
You only have one append statement, and it is appending to x. Perhaps x is not what you think, that your assignment in line 8 is not giving you something to append to.
You should create the stack in __init__, push tasks in new_task(), and pop tasks in pop*(). Your TaskManager is essentially a stack with some additional functionality.

Are you sure you want a stack? A stack does not look like a good fit for a "TaskManager". A stack is is a LIFO Last In First Out. The first task you add is the last you will complete. I would think a Queue (dequeue) is a better fit (First In First Out). The best fit may be a plain old list because it can be a stack or a queue and it is much easier to work with for things like pop_highest_priority_task().

I would also create a Task class for the TaskManager to manage. The task manager doesn't have to be much more than an __init__ method that sets the task name and priority. I would also write a __repr__() method to make it easy to print tasks.