Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

class error

$
0
0
class BoundedQueue:
    
    # Constructor, which creates a new empty queue, with user-specified capacity:
    def __init__(self, capacity):
        self.__items=[]
                
    # Adds a new item to the back of the queue, and returns nothing:
    def enqueue(self, item):
        self.__items.append(item)
    
    # Removes and returns the front-most item in the queue.  
    # Returns nothing if the queue is empty.
    def dequeue(self):
        if len(self.__items)!=0:
            return self.__items.pop(0)        
        
    # Returns the front-most item in the queue, and DOES NOT change the queue.  
    def peek(self):
        if len(self.__items)==0:
            return None
        return self.__items[0]        
    
    # Returns True if the queue is empty, and False otherwise:
    def is_empty(self):
        return len(self.__items) == 0
      
    # Returns True if the queue is full, and False otherwise:
    def is_full(self):
        return True
    
    # Returns the number of items in the queue:
    def size(self):
        return len(self.__items)
        
    # Returns the capacity of the queue:
    def capacity(self):
        self.__item=[]
    
    # Removes all items from the queue, and sets the size to 0.
    # clear() should not change the capacity
    def clear(self):
        self.__items=[]
        
    # Returns a string representation of the queue:
    def __str__(self):
        return("Memebers:{0} \nSize:{1}".format(self.__items,len(self.__items)))

#use this function to test your Bounded Queue implementation    

def test():
    is_pass = True

    try:
        string_queue = BoundedQueue('wrong type')
    except Exception as e:
        is_pass = True
    else:
        is_pass = False
    assert is_pass == True, "fail the test"
    
    try:
        string_queue = BoundedQueue(-1)
    except Exception as e:
        is_pass = True
    else:
        is_pass = False  
    assert is_pass == True, "fail the test" 
        
    string_queue = BoundedQueue(3)
    
    is_pass = (string_queue.size() == 0)
    assert is_pass == True, "fail the test"
    
    is_pass = (string_queue.capacity() == 3)
    assert is_pass == True, "fail the test" 
    
    is_pass = (string_queue.is_empty())
    assert is_pass == True, "fail the test"
    
    string_queue.enqueue("Hello")
    string_queue.enqueue("World")
    
    is_pass = (string_queue.size() == 2)
    assert is_pass == True, "fail the test"
    
    is_pass = (string_queue.peek() == "Hello")
    assert is_pass == True, "fail the test"

    is_pass = (string_queue.capacity() == 3)
    assert is_pass == True, "fail the test"
    
    is_pass = (string_queue.peek() == "Hello" and string_queue.size() == 2)
    assert is_pass == True, "fail the test"

    
    string_queue.enqueue("python")
    
    is_pass = (string_queue.is_full())
    assert is_pass == True, "fail the test" 
    
    try:
        string_queue.enqueue("rules")
    except Exception as e:
        is_pass = True
    else:
        is_pass = False
    assert is_pass == True, "fail the test"
    
    string_queue.clear()
    
    is_pass = (string_queue.capacity() == 3)
    assert is_pass == True, "fail the test" 
    
    try:
        string_queue.dequeue()
    except Exception as e:
        is_pass = True
    else:
        is_pass = False
    assert is_pass == True, "fail the test" 
        
    try:
        string_queue.peek()
    except Exception as e:
        is_pass = True
    else:
        is_pass = False
    assert is_pass == True, "fail the test"
    
    int_queue = BoundedQueue(2000)
    for i in range(0, 1000):
        int_queue.enqueue(i)      
    correctOrder = True
    for i in range(0, 200):
        if int_queue.dequeue() != i:
            correctOrder = False
            
    is_pass = correctOrder
    assert is_pass == True, "fail the test" 
    
    is_pass = (int_queue.size() == 800)
    assert is_pass == True, "fail the test" 
 
    is_pass = (int_queue.capacity() == 2000)
    assert is_pass == True, "fail the test"    

    is_pass = (int_queue.peek() == 200)
    assert is_pass == True, "fail the test"
    
    if is_pass == True:
        print ("=========== cong, your implementation passes the test ============")


#let's test it
if __name__ == '__main__':
    test()


So I have everything except: def __init__(self, capacity):, def is_full(self):, def capacity(self):
For def__init__(self, capacity) I need to raise an exception if capacity is negative and if its not a integer,
Ive tried a bunch of methods but there all wrong, the program keeps crashing on line 59, the def test(): is given to test my class so the problem is from the class, can i get some help.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>