#๐Ÿ”’ Help with data structures

51 messages ยท Page 1 of 1 (latest)

waxen fiberBOT
#

@stable dome

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.

stable dome
#

Hey so I was learning data sturctures and I encountered an issue while learning it

#
class Queue:
    def __init__(self):
        self.queue=[]
    def enqueue(self,*item):
        for i in item:
            self.queue.append(i)
        return self.queue
    def dequeue(self):
        if len(self.queue)<1:
            print("Empty Queue")
        else: 
           return self.queue.pop(0)
    def display(self):
        print(self.queue)
        print(self.queue[0])
    def size(Self):
        print (len(Self.queue))
que = Queue()
que.enqueue("flint","steel","paper","weight")
que.display()
que.dequeue()
que.display()
waxen fiberBOT
#

Hey @stable dome!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
stable dome
#

I studied that in queue there is a drawback that you cannot access the indices 0 and 1's after dequeing but here I can

#

and it's happening with things like stack all of them operate like lists which is very confusing to me

river rose
#

but the issue is same with things like stack @Purplys they all seem to run like lists for me and I can't wrap my head around it am I learning it in a wrong way??
Think of stacks & queues more like a checklist of requirements that something has to fulfill for it to be called one

stable dome
#
# Queue implementation in Python

class Queue:

    def __init__(self):
        self.queue = []

    # Add an element
    def enqueue(self, item):
        self.queue.append(item)

    # Remove an element
    def dequeue(self):
        if len(self.queue) < 1:
            return None
        return self.queue.pop(0)

    # Display  the queue
    def display(self):
        print(self.queue)

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


q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)

q.display()

q.dequeue()

print("After removing an element")
q.display()

this was the code I based mine around

river rose
stable dome
clever minnow
#

Is this a LIFO or FIFO queue?

stable dome
stable dome
clever minnow
#

yes.

river rose
river rose
clever minnow
#

^ list.pop(0) supposedly has to rebuild the list to rearrange the items so 1 -> 0

stable dome
clever minnow
#

using a linked list might help

river rose
stable dome
unkempt swift
stable dome
river rose
unkempt swift
#

they're not talking about removing

stable dome
stable dome
#

so you only remove one item at a time?

unkempt swift
#

yes, the purpose learning how to implement data structures is to allow you to do things efficiently

stable dome
#

doesn't that mean if I do queue.deque() it fixes the issue?

#

@river rose

stable dome
#

?

river rose
# stable dome so you only remove one item at a time?

yes, what I meant was, the time that removing 1 item, from a queue of 10 vs. 10 billion, should take roughly the same amount of time
list.pop(0) doesn't satisfy that, it'll take significantly longer if list has a lot of things in it, thus

    def dequeue(self):
        if len(self.queue) < 1:
            return None
        return self.queue.pop(0)
#                   ^ takes longer the more things in `queue`
```the entire `.dequeue()` function will take longer the more things in the queue
clever minnow
#

meanwhile self.queue.pop() (no argument, remove from end) is O(1) because it doesn't have to shift the list.

stable dome
clever minnow
#

but self.queue.insert(0, ...) is not O(1) because it has to shift the list the other way.

unkempt swift
stable dome
stable dome
river rose
unkempt swift
#

check the pins in #algos-and-data-structs

clever minnow
#

IMO linked list is the simplest of the data structures.

stable dome
stable dome
river rose
stable dome
river rose
stable dome
#

but yes thank you @river rose I was scratching my head over it from past 2 days

waxen fiberBOT
#
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.