






"""simple docstring"""
def 				snake_case      (   A__	,A__						):
			if discount_rate < 0:
						raise ValueError("Discount rate cannot be negative"						)
			if not cash_flows:
						raise ValueError("Cash flows list cannot be empty"						)
			UpperCAmelCase_     : Any			     =					sum(
			    cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(A__						)						)
			return round(A__	,ndigits=2						)


if __name__ == "__main__":
			import doctest

			doctest.testmod()











"""simple docstring"""
from __future__ import annotations

import time

lowerCamelCase_					=      list[tuple[int, int]]

lowerCamelCase_					=      [
    [0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0],
    [1, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0],
]

lowerCamelCase_					=      [[-1, 0], [0, -1], [1, 0], [0, 1]]  # up, left, down, right







class 			UpperCamelCase_       :





	def __init__(      self					: Optional[int]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Node | None						)						->			Dict:
				UpperCAmelCase_     : Any			     =					pos_x
				UpperCAmelCase_     : str			     =					pos_y
				UpperCAmelCase_     : int			     =					(pos_y, pos_x)
				UpperCAmelCase_     : int			     =					goal_x
				UpperCAmelCase_     : Tuple			     =					goal_y
				UpperCAmelCase_     : Union[str, Any]			     =					parent







class 			UpperCamelCase_       :





	def __init__(      self					: List[Any]	,	lowerCAmelCase_					: tuple[int, int]	,	lowerCAmelCase_					: tuple[int, int]						)						->			Tuple:
				UpperCAmelCase_     : List[str]			     =					Node(start[1]	,	start[0]	,	goal[1]	,	goal[0]	,	lowerCAmelCase_						)
				UpperCAmelCase_     : int			     =					Node(goal[1]	,	goal[0]	,	goal[1]	,	goal[0]	,	lowerCAmelCase_						)

				UpperCAmelCase_     : Union[str, Any]			     =					[self.start]
				UpperCAmelCase_     : int			     =					False





	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			Path | None:
				while self.node_queue:
							UpperCAmelCase_     : str			     =					self.node_queue.pop(0						)

							if current_node.pos == self.target.pos:
										UpperCAmelCase_     : Optional[Any]			     =					True
										return self.retrace_path(lowerCAmelCase_						)

							UpperCAmelCase_     : Optional[Any]			     =					self.get_successors(lowerCAmelCase_						)

							for node in successors:
										self.node_queue.append(lowerCAmelCase_						)

				if not self.reached:
							return [self.start.pos]
				return None





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]	,	lowerCAmelCase_					: Node						)						->			list[Node]:
				UpperCAmelCase_     : List[str]			     =					[]
				for action in delta:
							UpperCAmelCase_     : List[Any]			     =					parent.pos_x + action[1]
							UpperCAmelCase_     : List[str]			     =					parent.pos_y + action[0]
							if not (0 <= pos_x <= len(grid[0]						) - 1 and 0 <= pos_y <= len(lowerCAmelCase_						) - 1):
										continue

							if grid[pos_y][pos_x] != 0:
										continue

							successors.append(
							    Node(lowerCAmelCase_	,	lowerCAmelCase_	,	self.target.pos_y	,	self.target.pos_x	,	lowerCAmelCase_						)						)
				return successors





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]	,	lowerCAmelCase_					: Node | None						)						->			Path:
				UpperCAmelCase_     : Union[str, Any]			     =					node
				UpperCAmelCase_     : Union[str, Any]			     =					[]
				while current_node is not None:
							path.append((current_node.pos_y, current_node.pos_x)						)
							UpperCAmelCase_     : Tuple			     =					current_node.parent
				path.reverse()
				return path







class 			UpperCamelCase_       :





	def __init__(      self					: str	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: Tuple						)						->			Union[str, Any]:
				UpperCAmelCase_     : Optional[int]			     =					BreadthFirstSearch(lowerCAmelCase_	,	lowerCAmelCase_						)
				UpperCAmelCase_     : str			     =					BreadthFirstSearch(lowerCAmelCase_	,	lowerCAmelCase_						)
				UpperCAmelCase_     : Optional[Any]			     =					False





	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			Path | None:
				while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue:
							UpperCAmelCase_     : int			     =					self.fwd_bfs.node_queue.pop(0						)
							UpperCAmelCase_     : Dict			     =					self.bwd_bfs.node_queue.pop(0						)

							if current_bwd_node.pos == current_fwd_node.pos:
										UpperCAmelCase_     : str			     =					True
										return self.retrace_bidirectional_path(
										    lowerCAmelCase_	,	lowerCAmelCase_						)

							UpperCAmelCase_     : str			     =					current_bwd_node
							UpperCAmelCase_     : List[str]			     =					current_fwd_node

							UpperCAmelCase_     : Tuple			     =					{
							    self.fwd_bfs: self.fwd_bfs.get_successors(lowerCAmelCase_						),
							    self.bwd_bfs: self.bwd_bfs.get_successors(lowerCAmelCase_						),
							}

							for bfs in [self.fwd_bfs, self.bwd_bfs]:
										for node in successors[bfs]:
													bfs.node_queue.append(lowerCAmelCase_						)

				if not self.reached:
							return [self.fwd_bfs.start.pos]
				return None





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]	,	lowerCAmelCase_					: Node	,	lowerCAmelCase_					: Node						)						->			Path:
				UpperCAmelCase_     : Optional[Any]			     =					self.fwd_bfs.retrace_path(lowerCAmelCase_						)
				UpperCAmelCase_     : Dict			     =					self.bwd_bfs.retrace_path(lowerCAmelCase_						)
				bwd_path.pop()
				bwd_path.reverse()
				UpperCAmelCase_     : str			     =					fwd_path + bwd_path
				return path


if __name__ == "__main__":
			# all coordinates are given in format [y,x]
			import doctest

			doctest.testmod()
			lowerCamelCase_					=      (0, 0)
			lowerCamelCase_					=      (len(grid) - 1, len(grid[0]) - 1)
			for elem in grid:
						print(elem)

			lowerCamelCase_					=      time.time()
			lowerCamelCase_					=      BreadthFirstSearch(init, goal)
			lowerCamelCase_					=      bfs.search()
			lowerCamelCase_					=      time.time() - start_bfs_time

			print('''Unidirectional BFS computation time : ''', bfs_time)

			lowerCamelCase_					=      time.time()
			lowerCamelCase_					=      BidirectionalBreadthFirstSearch(init, goal)
			lowerCamelCase_					=      bd_bfs.search()
			lowerCamelCase_					=      time.time() - start_bd_bfs_time

			print('''Bidirectional BFS computation time : ''', bd_bfs_time)



