#๐ Help with data structures
51 messages ยท Page 1 of 1 (latest)
@stable dome
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.
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()
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.
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
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
# 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
that is straight up just not a queue, because the dequeue() function of a queue should take a constant amount of time no matter how many things are in it
what does that mean? sorry I am a little bit dumb in this
Is this a LIFO or FIFO queue?
fifo
lifo is stack isn't it?
yes.
as in (loosely), queue.dequeue() should take roughly the same amount of time, no matter if there are 10 things in the queue or 1000000000
list.pop(0) doesn't satisfy this
^ list.pop(0) supposedly has to rebuild the list to rearrange the items so 1 -> 0
so you mean if I dequeue 10 things or 100 things both should take the same time, lets say 3 seconds?
using a linked list might help
no, I mean that if you do queue.dequeue() once, it should take 3 seconds whether there are 10 items inside of queue, or 10 billion
I only studied arrays till now then I moved to stacks, queue, circular queue after which it is linked list according to the structure I was following
i would still consider it a queue, when someone says "queue" I think of behavior not time complexity
but that doesn't make sense mathematically, unless you just dump all of it in an instance like emptying a box right? like if you remove 10 items you would take less time when you remove 10 billion
no, not remove 10 items vs. remove 10 billion items
it's remove 1 item, from a queue that has 10 items vs 10 billion
they're not talking about removing
ah yes that too, do I need to study asymptomic notations and time complexities, are they important? I kinda jumped over them since I was a little impatient
ohhhh makes sense
so you only remove one item at a time?
yes, the purpose learning how to implement data structures is to allow you to do things efficiently
ohh so I was stupid to skip over them, I will try and learn them, how many should I learn
?
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
meanwhile self.queue.pop() (no argument, remove from end) is O(1) because it doesn't have to shift the list.
wait logically speaking if we have to take out just one item whether it is list or dequeing shouldn't they take the same time??? or is it that list goes over everything before doing so hence it takes more time but deque has two counters so it takes less time( the Front and Rear counters)
but self.queue.insert(0, ...) is not O(1) because it has to shift the list the other way.
it's not really a matter of how many, I would find a curriculum on DS&A somewhere
yea I gotta study time complexity I guess it's hard to understand without it
do you know where can I get one?
no, after list.pop(0), it has to move the item in index 1 to index 0, index 2 to index 1, index 3 to index 2, ...
check the pins in #algos-and-data-structs
IMO linked list is the simplest of the data structures.
ohhh makes sense right now I got it so in queue the Rear counter just simply moves ahead making it more efficient?
thank you
sort of, rear might not be a counter but a linked list node, otherwise yes
well thats what they wrote where I read it from programmiz.com
they might not be using the linked list implementation then
but yes thank you @river rose I was scratching my head over it from past 2 days
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.