예제 #1
0
    class Iterator(Iterator):
        """
        Enumerates the nodes of a tree.
        """

        def __init__(self, tree):
            """
            (Tree.Iterator, Tree) -> None
            Constructs an iterator for the given tree.
            """
            super(Tree.Iterator, self).__init__(tree)
            self._stack = StackAsLinkedList()
	    if not tree.isEmpty:
		self._stack.push(tree)

        def next(self):
            """
            (Tree.Iterator) -> Object
            Returns the next node in the tree.
            """
            if self._stack.isEmpty:
		raise StopIteration
	    top = self._stack.pop()
	    i = top.degree - 1
	    while i >= 0:
		subtree = top.getSubtree(i)
		if not subtree.isEmpty:
		    self._stack.push(subtree)
		i -= 1
            return top.key
예제 #2
0
파일: tree.py 프로젝트: noahpieta/juncBASE
    class Iterator(Iterator):
        """
        Enumerates the nodes of a tree.
        """
        def __init__(self, tree):
            """
            (Tree.Iterator, Tree) -> None
            Constructs an iterator for the given tree.
            """
            super(Tree.Iterator, self).__init__(tree)
            self._stack = StackAsLinkedList()
            if not tree.isEmpty:
                self._stack.push(tree)

        def next(self):
            """
            (Tree.Iterator) -> Object
            Returns the next node in the tree.
            """
            if self._stack.isEmpty:
                raise StopIteration
            top = self._stack.pop()
            i = top.degree - 1
            while i >= 0:
                subtree = top.getSubtree(i)
                if not subtree.isEmpty:
                    self._stack.push(subtree)
                i -= 1
            return top.key
예제 #3
0
 def __init__(self, tree):
     """
     (MWayTree.Iterator, MWayTree) -> None
     Constructs an iterator for the given M-way tree.
     """
     super(MWayTree.Iterator, self).__init__(tree)
     self._position = 0
     self._stack = StackAsLinkedList()
예제 #4
0
파일: tree.py 프로젝트: noahpieta/juncBASE
 def __init__(self, tree):
     """
     (Tree.Iterator, Tree) -> None
     Constructs an iterator for the given tree.
     """
     super(Tree.Iterator, self).__init__(tree)
     self._stack = StackAsLinkedList()
     if not tree.isEmpty:
         self._stack.push(tree)
예제 #5
0
파일: demo2.py 프로젝트: RDWang/python
 def main(*argv):
     "Demostration program number 2."
     print Demo2.main.__doc__
     StackAsArray.main(*argv)
     StackAsLinkedList.main(*argv)
     QueueAsArray.main(*argv)
     QueueAsLinkedList.main(*argv)
     DequeAsArray.main(*argv)
     DequeAsLinkedList.main(*argv)
     return 0
예제 #6
0
 def __init__(self, tree):
     """
     (MWayTree.Iterator, MWayTree) -> None
     Constructs an iterator for the given M-way tree.
     """
     super(MWayTree.Iterator, self).__init__(tree)
     self._position = 0
     self._stack = StackAsLinkedList()
예제 #7
0
        def __init__(self, tree):
            """
            (Tree.Iterator, Tree) -> None
            Constructs an iterator for the given tree.
            """
            super(Tree.Iterator, self).__init__(tree)
            self._stack = StackAsLinkedList()
	    if not tree.isEmpty:
		self._stack.push(tree)
예제 #8
0
 def calculator(input, output):
     """
     (File, File) -> None
     A very simple reverse-Polish calculator.
     """
     stack = StackAsLinkedList()
     for line in input.readlines():
         for word in line.split():
             if word == "+":
                 arg2 = stack.pop()
                 arg1 = stack.pop()
                 stack.push(arg1 + arg2)
             elif word == "*":
                 arg2 = stack.pop()
                 arg1 = stack.pop()
                 stack.push (arg1 * arg2)
             elif word == "=":
                 arg = stack.pop()
                 output.write(str(arg) + "\n")
             else:
                 stack.push(int(word))
예제 #9
0
    def parsePostfix(input):
        """
        (File) -> ExpresionTree
        Parses the given file into an expression tree.
        """
        stack = StackAsLinkedList()

        for line in input.readlines():
            for word in line.split():
                if word == "+" or word == "-" \
                        or word == "*" or word == "/":
                    result = ExpressionTree(word)
                    result.attachRight(stack.pop())
                    result.attachLeft(stack.pop())
                    stack.push(result)
                else:
                    stack.push(ExpressionTree(word))
        return stack.pop()
예제 #10
0
    class Iterator(Iterator):
        """
        Enumerates the objects in an M-way tree.
        """

        def __init__(self, tree):
            """
            (MWayTree.Iterator, MWayTree) -> None
            Constructs an iterator for the given M-way tree.
            """
            super(MWayTree.Iterator, self).__init__(tree)
            self._position = 0
            self._stack = StackAsLinkedList()
        
        def next(self):
            """
            (MWayTree.Iterator) -> Object
            Returns the next object in this M-way tree.
            """
            if self._stack.isEmpty:
                if not self._container.isEmpty:
                    self._stack.push(self._container)
                self._position = 1
            else:
                self._position = self._position + 1
                top = self._stack.top
                if self._position == top.degree:
                    pop = self._stack.pop()
                    i = pop.degree - 1
                    while i >= 0:
                        subtree = pop.getSubtree(i)
                        if not subtree.isEmpty:
                            self._stack.push(subtree)
                        i = i - 1
                    self._position = 1
                if self._stack.isEmpty:
                    raise StopIteration
            return self._stack.top.getKey(self._position)
예제 #11
0
    class Iterator(Iterator):
        """
        Enumerates the objects in an M-way tree.
        """
        def __init__(self, tree):
            """
            (MWayTree.Iterator, MWayTree) -> None
            Constructs an iterator for the given M-way tree.
            """
            super(MWayTree.Iterator, self).__init__(tree)
            self._position = 0
            self._stack = StackAsLinkedList()

        def next(self):
            """
            (MWayTree.Iterator) -> Object
            Returns the next object in this M-way tree.
            """
            if self._stack.isEmpty:
                if not self._container.isEmpty:
                    self._stack.push(self._container)
                self._position = 1
            else:
                self._position = self._position + 1
                top = self._stack.top
                if self._position == top.degree:
                    pop = self._stack.pop()
                    i = pop.degree - 1
                    while i >= 0:
                        subtree = pop.getSubtree(i)
                        if not subtree.isEmpty:
                            self._stack.push(subtree)
                        i = i - 1
                    self._position = 1
                if self._stack.isEmpty:
                    raise StopIteration
            return self._stack.top.getKey(self._position)
예제 #12
0
    def parsePostfix(input):
        """
        (File) -> ExpresionTree
        Parses the given file into an expression tree.
        """
        stack = StackAsLinkedList()

        for line in input.readlines():
            for word in line.split():
                if word == "+" or word == "-" \
                        or word == "*" or word == "/":
                    result = ExpressionTree(word)
                    result.attachRight(stack.pop())
                    result.attachLeft(stack.pop())
                    stack.push(result)
                else:
                    stack.push(ExpressionTree(word))
        return stack.pop()
class Algorithms(object):
  def __init__(self):
    self.stack = StackAsLinkedList()

  def isBalanced(self, input, output):
    dic = {"]":"[", "}":"{", ")":"("}

    for line in input.readlines():
      for word in line:
        if word in dic.values():
          self.stack.push(word)
          result = False
        elif word in dic.keys():
          if self.stack.getTop() == dic[word]:
            self.stack.pop()
            result = True
          else:
            self.stack.push(word)
            result = False
      output.write(str(result) + "\n")
class Algorithms(object):
    def __init__(self):
        self.stack = StackAsLinkedList()

    def calculator(self, input, output):
        operators = ["+", "-", "*", "/", "^"]
        for line in input.readlines():
            for word in line.split():
                if word in operators:
                    print "it is operator ", word
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push("(%s %s %s)" % (arg1, word, arg2))
                elif word == "=":
                    print "it is ="
                    arg = self.stack.pop()
                    output.write(arg + " = " + "\n")
                    print "ans is ", arg
                else:
                    print "it is number, push it"
                    # make it double
                    self.stack.push(word)
class Algorithms(object):

  def __init__(self):
    self.stack = StackAsLinkedList()

  def calculator(self, input, output):
    operators = ["+", "-", "*", "/", "^"]
    for line in input.readlines():
      for word in line.split():
        if word in operators:
          print "it is operator ", word
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push("(%s %s %s)" % (arg1, word, arg2))
        elif word == "=":
          print "it is ="
          arg = self.stack.pop()
          output.write(arg + " = " + "\n")
          print "ans is ", arg
        else:
          print "it is number, push it"
          # make it double
          self.stack.push(word)
class Algorithms(object):
    def __init__(self):
        self.stack = StackAsLinkedList()

    def calculator(self, input, output):
        for line in input.readlines():
            for word in line.split():
                if word == "+":
                    print "it is +"
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push(arg1 + arg2)
                elif word == "*":
                    print "it is *"
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push(arg1 * arg2)
                elif word == "-":
                    print "it is -"
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push(arg1 - arg2)
                elif word == "/":
                    print "it is /"
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push(arg1 / arg2)
                # exponential expression
                elif word == "^":
                    print "it is ^"
                    arg2 = self.stack.pop()
                    arg1 = self.stack.pop()
                    self.stack.push(arg1**arg2)
                elif word == "=":
                    print "it is ="
                    arg = self.stack.pop()
                    output.write(str(arg) + "\n")
                    print "ans is ", arg
                else:
                    print "it is number"
                    # make it double
                    self.stack.push(float(word))

    # remove all by calling purge method of linked list
    def clear(self):
        self.stack.purge()

    # use the iterator of linked list to access every remaining element
    def printall(self):
        for ele in self.stack:
            print ele
class Algorithms(object):

  def __init__(self):
    self.stack = StackAsLinkedList()

  def calculator(self, input, output):
    for line in input.readlines():
      # prefix is acrtually the reversed version of postfix
      line = line.split()
      operation = line[:-1]
      operation.reverse()
      operation.append(line[-1])
      for word in operation:
        if word == "+":
          print "it is +"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 + arg2)
        elif word == "*":
          print "it is *"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 * arg2)
        elif word == "-":
          print "it is -"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 - arg2)
        elif word == "/":
          print "it is /"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 / arg2)
        # exponential expression
        elif word == "^":
          print "it is ^"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 ** arg2)
        elif word == "=":
          print "it is ="
          arg = self.stack.pop()
          output.write(str(arg) + "\n")
          print "ans is ", arg
        else:
          print "it is number"
          # make it double
          self.stack.push(float(word))	
	'''
 def __init__(self):
   self.stack = StackAsLinkedList()
 def __init__(self, numOfStack):
     self.stacks = [StackAsLinkedList() for i in range(numOfStack)]
class Algorithms(object):

  def __init__(self):
    self.stack = StackAsLinkedList()

  def calculator(self, input, output):
    for line in input.readlines():
      for word in line.split():
        if word == "+":
          print "it is +"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 + arg2)
        elif word == "*":
          print "it is *"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 * arg2)
        elif word == "-":
          print "it is -"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 - arg2)
        elif word == "/":
          print "it is /"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 / arg2)
        # exponential expression
        elif word == "^":
          print "it is ^"
          arg2 = self.stack.pop()
          arg1 = self.stack.pop()
          self.stack.push(arg1 ** arg2)
        elif word == "=":
          print "it is ="
          arg = self.stack.pop()
          output.write(str(arg) + "\n")
          print "ans is ", arg
        else:
          print "it is number"
          # make it double
          self.stack.push(float(word))

  # remove all by calling purge method of linked list
  def clear(self):
    self.stack.purge()

  # use the iterator of linked list to access every remaining element
  def printall(self):
    for ele in self.stack:
      print ele
예제 #21
0
 def calculator(input, output):
     """
     (File, File) -> None
     A very simple reverse-Polish calculator.
     """
     stack = StackAsLinkedList()
     for line in input.readlines():
         for word in line.split():
             if word == "+":
                 arg2 = stack.pop()
                 arg1 = stack.pop()
                 stack.push(arg1 + arg2)
             elif word == "*":
                 arg2 = stack.pop()
                 arg1 = stack.pop()
                 stack.push(arg1 * arg2)
             elif word == "=":
                 arg = stack.pop()
                 output.write(str(arg) + "\n")
             else:
                 stack.push(int(word))
class Algorithms(object):

  def __init__(self):
    self.stack = StackAsLinkedList()

  def calculator(self, input, output):
    operators = ["+", "-"]
    priors = ["*", "/"]
    for line in input.readlines():
      flag = 0
      size = 0
      for word in line.split():
        '''
        match_left = re.search(r'\(', word)
        match_right = re.search(r'\)', word)
        if match_left:
          self.stack.push(word[1:])
          flag = 1
        if match_right:
          self.stack.push(word[:-1])
          '''
        if word in operators:
          print "it is an operator: ", word
          while size >= 3:
            arg2 = self.stack.pop()
            op = self.stack.pop()
            arg1 = self.stack.pop()
            size -= 3
            self.stack.push("%s %s %s" % (arg1, arg2, op))
            size += 1
            print "push ", arg1, arg2, op
          self.stack.push(word)
          print "pushing ", word
          size += 1
          flag = 0
        elif word in priors:
          print "it is a prior: ", word
          # indicating there are a pair of arg1 * arg2 in stack
          # waiting for poping out
          if flag == 1 and size >= 3:
            arg2 = self.stack.pop()
            op = self.stack.pop()
            arg1 = self.stack.pop()
            size -= 3
            self.stack.push("%s %s %s" % (arg1, arg2, op))
            size += 1
            print "push ", arg1, arg2, op
          # push the operator itself
          self.stack.push(word)
          print "pushing ", word
          size += 1
          flag = 1
        elif word == "=":
          print "it is ="
          while size >= 3:
            arg2 = self.stack.pop()
            op = self.stack.pop()
            arg1 = self.stack.pop()
            size -= 3
            self.stack.push("%s %s %s" % (arg1, arg2, op))
            size += 1
            print "push ", arg1, arg2, op
          output.write("%s %s %s = \n" % (arg1, arg2, op))
          print "writing into files", arg1, arg2, op
        else:
          print "it is number: ", word
          self.stack.push(word)
          print "pushing ", word
          size += 1