0 Replies - 1902 Views - Last Post: 05 May 2012 - 01:29 PM

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 834
  • View blog
  • Posts: 2,264
  • Joined: 23-December 08

Stack Class

Posted 05 May 2012 - 01:29 PM

Description: It's very straightforwardThis is a more featured stack class that also uses proper naming conventions in accordance with the abstract data type "Stack"
__author__ = "atraub"
__date__= "5/5/2012"


class Stack:
    """Implements a basic stack class in Python using a list"""
    def __init__(self, *elements):
        """intializes a stack, elements allow you to include elements in stack constructor"""
        #allows a list or tuple to be converted into a stack
        if len(elements) == 1:
            try:
                test = iter(elements[0]) #check if the only element is iterable
                self.__stack = list(elements[0])#if it is, make it into a stack
            except TypeError as e:
                self.__stack = elements#if not, put it in the stack
        else:
            self.__stack = list(elements)
            
    def push(self,element):
        """add element to the end of the stack"""
        self.__stack.append(element)
            
    def pop(self):
        """remove and return last element from stack"""
        return self.__stack.pop()
        
    def peek(self):
        """view top element of stack"""
        return self.__stack[-1]

    def isEmpty(self):
        """returns true if stack is empty"""
        return (len(self.__stack)==0)

    def reverse(self):
        """reverses the stack"""
        self.__stack.reverse()

    def count(self,value):
        """counts the number of occurences of a specified value in a stack"""
        return self.__stack.count(value)

    def extend(self, iterable):
        """allows you to add an iterable to the end of stack"""
        self.__stack.extend(iterable)

    def index(self,value):
        """finds the index of a specified value"""
        return self.__stack.index(value)

    def __len__(self):
        """returns the length of the stack"""
        return len(self.__stack)

    def __str__(self):
        """returns a string representation of a stack for printing"""
        return str(self.__stack)

    def __repr__(self):
        """creates a straing representation of a stack"""
        return repr(self.__stack)

    def __add__(self,iterable):
        """overloads the + operator for adding stacks to iterables or vice versa"""
        return Stack(self.__stack.__add__(list(iterable)))

    def __iter__(self):
        """gets an iterator for a stack"""
        return self.__stack.__iter__()

    def __contains__(self,element):
        """check if element exists in stack, overloads the 'in' operator"""
        return self.__stack.__contains__(element)



Is This A Good Question/Topic? 0
  • +

Replies To: Stack Class

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 834
  • View blog
  • Posts: 2,264
  • Joined: 23-December 08

Re: Stack Class

Posted 05 May 2012 - 01:29 PM

Description: if you create a stack with no parameters, it's empty. myStack = Stack() if you create a stack with an iterable value as its parameter, it will convert that iterable to a stack in the default python way. myList = [1,2,3,4,5] myStack = Stack(myList) you can also create a stack with comma separated values as the parameter. myStack = Stack("I","like","potato","chips")This is a full featured stack class that also uses proper naming conventions in accordance with the abstract data type "Stack" as well as proper error reporting.
__author__ = "atraub"
__date__= "5/9/2012"


class Stack:
    """Implements a basic stack class in Python using a list"""
    def __init__(self, *elements):
        """intializes a stack, elements allow you to include elements in stack constructor"""
        #allows a list or tuple to be converted into a stack
        if len(elements) == 1:
            try:
                test = iter(elements[0]) #check if the only element is iterable
                self.__stack = list(elements[0])#if it is, make it into a stack
            except TypeError as e:
                self.__stack = list(elements)#if not, put it in the stack
        else:
            self.__stack = list(elements)

    def push(self,element):
        """add element to the end of the stack"""
        self.__stack.append(element)
            
    def pop(self):
        """remove and return last element from stack"""
        if len(self.__stack) == 0:
            raise IndexError("pop from empty stack")
        return self.__stack.pop()
        
    def peek(self):
        """view top element of stack"""
        if len(self.__stack) == 0:
            raise IndexError("can't view top element of empty stack")
        return self.__stack[-1]

    def isEmpty(self):
        """returns true if stack is empty"""
        return len(self.__stack) == 0

    def hasMore(self):
        """returns true if stack is not empty"""
        return len(self.__stack) != 0

    def clear(self):
        """empties the stack"""
        self.__stack = []

    def __len__(self):
        """returns the length of the stack"""
        return len(self.__stack)

    def __str__(self):
        """returns a string representation of a stack for printing"""
        return str(self.__stack)

    def __repr__(self):
        """creates a string representation of a stack"""
        return repr(self.__stack)

    def __add__(self,iterable):
        """overloads the + operator for adding iterables to stacks"""
        return Stack(*(self.__stack + (list(iterable))))

    def __iter__(self):
        """gets an iterator for a stack"""
        return iter(self.__stack)



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1