zach_93
zach_93

Reputation: 17

create stack using 2 queues

Hey guys I am having trouble with my first assignment in my data structures class, and was hopping someone could provide some insight on what I am doing wrong.

I must create a stack that utilizes 2 queues for data storage.

import Queue_PlistLR as queueList

import Queue_PlistLR as queueList

class Stack_2Queues():

        def __init__(self, name, salary):
                self.items = []
                self.name = name
                self.salary = salary

        def isEmpty(self):
                return len(self.items) == 0        

        def push(self, e):
                self._data.append(e)

        def pop(self):
                if self.is_empty:
                        raiseEmpty("Stack is empty")
                return self._data.pop()
        def size(self):
                return len(self.items) 

        def to_String(self):
                str_i = ""
                for e in self.items:
                        str_i+=str(e)+" "
                return str_i.strip() 

        def length(self):

                return len()

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

        def dequeue(self):
                if self.isEmpty():
                        raise Empty('Queue is empty')
                return self.items.pop(0)        

        def employeeName(self):
                print("The employee is: ", self.name)

        def employeeSalary(self):

                print("Employees salary is", self.salary)        

s1 = Stack_2Queues = [["Ash", "Jen", "Mike", "Zach"], [45000, 32000, 74000, 960000]]

s1.employeeName()
s1.employeeSalary()

***I know this is wrong, for the most part. but my question is this. For the stack I want to create 2 queues, one for employeeName and the other for employeeSalary, using 1 stack.

Also when I try printing the name and salary from the stack (to see if it works) and I get "list has no attribute" error.***

So, here are my questions:

1) Excuse my illiteracy as this may sound stupid, but is my s1 variable the form of a stack?

2) Why am I getting the list has no attribute since I defined all methods, and called them?

Upvotes: 1

Views: 215

Answers (1)

hokedo
hokedo

Reputation: 156

The AttributeError is caused by this line:

s1 = Stack_2Queues = [["Ash", "Jen", "Mike", "Zach"], [45000, 32000, 74000, 960000]]

This is not how you initialize Stack_2Queues. What's actually going here is that you're assigning the list which contains two other lists to the variables s1 and Stack_2Queues(thus changing the reference which points to the class definition to the list)

In [1]: s1 = Stack_2Queues = [["Ash", "Jen", "Mike", "Zach"], [45000, 32000, 74000, 960000]]

In [2]: print(s1)
[['Ash', 'Jen', 'Mike', 'Zach'], [45000, 32000, 74000, 960000]]

In [3]: print(Stack_2Queues)
[['Ash', 'Jen', 'Mike', 'Zach'], [45000, 32000, 74000, 960000]]

Simple example to understand this:

In [4]: a = 5

In [5]: b = 5

This is equivalent to:

In [6]: a = b = 5

Change the line to this:

s1 = Stack_2Queues(["Ash", "Jen", "Mike", "Zach"], [45000, 32000, 74000, 960000])

It works for me after that:

('The employee is: ', ['Ash', 'Jen', 'Mike', 'Zach'])
('Employees salary is', [45000, 32000, 74000, 960000])

Also you imported the same module twice right at the top of the script which you haven't used.

If you try to call either push or pop on an instance of Stack_2Queues python will throw another AttributeError because Stack_2Queues does not have any _data attribute.

def push(self, e):
    self._data.append(e)

def pop(self):
    if self.is_empty:
        raiseEmpty("Stack is empty")
    return self._data.pop()

You should maybe change this part of the code or add self._data in the __init__ method.

This will also not work:

def length(self):
    return len()

Calling the built-in len function without any argument will throw an error. https://docs.python.org/3.6/library/functions.html#len It's probably redundant since you defined a size method and probably does the same thing.

Upvotes: 2

Related Questions