Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2004, 07:19 PM
Zopyrus Zopyrus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Sweden
Posts: 20 Zopyrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 13 m 28 sec
Reputation Power: 0
Queue and Stack!

I have this homework to construct a queue and just wanted u guys to see and perhaps give me some tips on my way of creating a good queue.

First before I start i need to ask, to begin with, do I have to create a .py file with some "Class" and definitions functions in it. Then shall I create a queue.py and start making the queue system out of that? or? How do I start in making a queue system? oh man I'm getting so confused right now

Reply With Quote
  #2  
Old November 16th, 2004, 03:53 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Assuming that you want to create the queue and stack as modules - which is probably best - then you would have two files: queue.py and stack.py layed out something like this.

Code:
#!/usr/bin/env python

class ImplementationName: #Queue or Stack.
    def method1():              #Rename this ;).
        ...
    ...                                #etc.

if __name__ == '__main__':
    ...use the class here.


You shouldn't have much problems with this since Python is so high level and it's data-types generally have everything you need built in . Check out the __builtin__ list type below.

http://www.python.org/doc/2.3.4/lib/typesseq-mutable.html

If you have any questions just ask,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old November 16th, 2004, 06:45 AM
Zopyrus Zopyrus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Sweden
Posts: 20 Zopyrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 13 m 28 sec
Reputation Power: 0
Wicked! Thanks mate! That site has good stuff in it!

I got one question, I've started out like this:

Code:

queue = ["Eric", "John", "Micheal"]
queue.append("Apa")           # Apa Kommer
queue.append("Gooz")          # Gooz kommer
queue.pop(0)                  # Eric Går
queue.pop(0)                  # John Går
queue

print queue


Everything is correct here when I run the program, but how do I make the program give out a return function of isEmpty(): so that it returns true if the queue is empty and false if it is not?

Thanks,
Micheal

Reply With Quote
  #4  
Old November 16th, 2004, 05:30 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
You could either write one yourself, for example:

Code:
def isEmpty(queue):
    if queue:
        return False
    else:
        return True    

q = [1,2,3]
isEmpty(q)

q = []
isEmpty(q)



or, if this is going to be object oriented, you could extend the built in list class and add an 'isEmpty' method to it, for example:

Code:
>>> class Queue(list):
...     # Inherit from the list class, and extend it with a new method
... 	def isEmpty(self):
... 		if self:
... 			return False
... 		else:
... 			return True
... 
>>>		
>>> q = Queue()
>>> q.append(1)
>>> q.append(3)
>>> q
[1, 3]
>>> q.isEmpty()
False
>>> q.pop()
3
>>> q.pop()
1
>>> q.isEmpty()
True
>>> 


This is one of those programming examples that doesn't work very well with Python because, as you've found out, the built in lists also work like queues already. I'm going to guess though, that if you hand in just a demonstration of a list being a queue, that wont be quite what they are looking for.

I would expect they want you to write the contents of "append" and "pop" yourself - though I might be wrong.

Reply With Quote
  #5  
Old November 16th, 2004, 05:38 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Personally I would just add a method to my Queue class like this.

Code:
def isEmpty():
    return len(self.queueValues) > 0


Here, self.queueValues is a instance variable that stores the values for the queue; of type list. If you post the program/class I would be able to give you a more exact answer .

Mark.

Reply With Quote
  #6  
Old November 16th, 2004, 06:47 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,243 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 5 h 41 m 59 sec
Reputation Power: 265
One of the Python mottos is "Batteries Included" - my implementation of a Queue would be:

Code:
from Queue import Queue

this probably does far more than you need, since the Queue module in the Python library is designed for passing data between threads, and is fully thread aware.

There is no built in Stack class, but lists have a pop method and an append method, so they can can be used as a stack with no modification if you restrict yourself to only using those methods.

This is probably not what your tutor wants as an answer, but if I was an employer I would be unhappy if a developer wasted time implementing classes that were already included in the language.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Queue and Stack!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT