Python Forum
Class Stack as Container Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Stack as Container Class
#1
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!
Reply
#2
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()
Reply
#3
(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"]}
Reply
#4
Please show complete unaltered error traceback.
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have 'Failed: DID NOT RAISE <class 'Exception'>' williamKrakos 3 701 Feb-20-2024, 09:46 PM
Last Post: williamKrakos
  Making a Class Person ? Kessie1971 3 1,140 Apr-23-2023, 05:20 PM
Last Post: deanhystad
  Modifying Class File/ Name-Mangling CWillis105 4 2,851 Jun-11-2021, 06:42 PM
Last Post: snippsat
  Using the robot class - implement robot battles - Python OOP krnkrnkrn 1 1,898 May-17-2021, 09:19 PM
Last Post: deanhystad
  Class project nayo43 7 3,711 Mar-02-2021, 03:27 PM
Last Post: BashBedlam
  Help on displaying tostring on class. javesike1262 6 2,899 Jan-11-2021, 07:08 PM
Last Post: javesike1262
  HW on BarGraph Class Grey_space 1 1,357 Oct-28-2020, 08:13 PM
Last Post: deanhystad
  class homework mohan10216 2 1,747 Oct-28-2020, 06:28 PM
Last Post: jefsummers
  Completeing the Class baboon 1 1,800 Sep-30-2020, 02:52 PM
Last Post: jefsummers
  Creating Email Message Class in Python QuavoJ 1 2,155 Jul-20-2020, 08:30 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020