def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() """ "*** YOUR CODE HERE ***" start = problem.getStartState() #present state stack = util.Stack( ) #stack to hold present state and directions from Start state visited_set = [] # list containing all visited nodes start_dir = [] # Entire path from start to the goal path while (problem.isGoalState(start) != True): successors = problem.getSuccessors(start) start_dir_len = len(start_dir) for l, m, n in successors: temp = start_dir[0:start_dir_len] temp.append(m) stack.push((l, temp)) while True: start, start_dir = stack.pop( ) #Getting the successor to be explored and the directions from start node if start not in visited_set: #If the successor is in the visited set get the next successor visited_set.append(start) break return start_dir
def string_reverse(string): stack = Stack() start, start_ = 0, 0 reversed_str = "" while start < len(string): index = string[start] stack.push(index) start += 1 while start_ < len(string): reversed_str += stack.pop() start_ += 1 return reversed_str
def stack_4_vals(): stack = Stack() stack.push(3) stack.push(-7) stack.push('d') stack.push('r') return stack
if self.queue1: return self.queue1.popleft() else: return self.queue2.popleft() def top(self): """ :rtype: int """ if self.queue1: return self.queue1[0] else: return self.queue2[0] def empty(self): """ :rtype: bool """ return not self.queue1 and not self.queue2 if __name__ == "__main__": stack = MyStack() stack.push(1) stack.push(2) print(f'{stack.pop()}') print(f'{stack.pop()}')
self._content.append(v) self._current = self._current + 1 else: print('Stack Full!') def pop(self): if self._content: self._current = self._current - 1 return self._content.pop() else: print('Stack empty!') def show(self): print(self._content) def showRemainderSpace(self): print('Stack can still push ', self._size-self._current, 'elements.') if __name__ == '__main__': print('Please use me as a module.') stack = myStack(10) for i in range(1,6): stack.push(i) stack.show() stack.showRemainderSpace(); for i in range(0,4): stack.pop() print(stack.isEmpty()) stack.show(); stack.pop() print(stack.isEmpty())