


"""simple docstring"""







from __future__ import annotations

from collections import deque




class  UpperCamelCase_	:

	"""simple docstring"""





	def __init__( self			:				str			,			UpperCAmelCase__			:				list[str]   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				[]
						self.adlist.append(
						    {"value": "", "next_states": [], "fail_state": 0, "output": []}   )

						for keyword in keywords:
											self.add_keyword(UpperCAmelCase__   )
						self.set_fail_transitions()




	def   UpperCAmelCase_      ( self			:				List[Any]			,			UpperCAmelCase__			:				int			,			UpperCAmelCase__			:				str   )       ->	int | None:
						for state in self.adlist[current_state]["next_states"]:
											if char == self.adlist[state]["value"]:
																return state
						return None




	def   UpperCAmelCase_      ( self			:				Optional[Any]			,			UpperCAmelCase__			:				str   )       ->	None:
						__SCREAMING_SNAKE_CASE						   =				0
						for character in keyword:
											__SCREAMING_SNAKE_CASE						   =				self.find_next_state(UpperCAmelCase__			,			UpperCAmelCase__   )
											if next_state is None:
																self.adlist.append(
																    {
																        "value": character,
																        "next_states": [],
																        "fail_state": 0,
																        "output": [],
																    }   )
																self.adlist[current_state]["next_states"].append(len(self.adlist   ) - 1   )
																__SCREAMING_SNAKE_CASE						   =				len(self.adlist   ) - 1
											else:
																__SCREAMING_SNAKE_CASE						   =				next_state
						self.adlist[current_state]["output"].append(UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	None:
						__SCREAMING_SNAKE_CASE						   =				deque()
						for node in self.adlist[0]["next_states"]:
											q.append(UpperCAmelCase__   )
											__SCREAMING_SNAKE_CASE						   =				0
						while q:
											__SCREAMING_SNAKE_CASE						   =				q.popleft()
											for child in self.adlist[r]["next_states"]:
																q.append(UpperCAmelCase__   )
																__SCREAMING_SNAKE_CASE						   =				self.adlist[r]["fail_state"]
																while (
																    self.find_next_state(UpperCAmelCase__			,			self.adlist[child]["value"]   ) is None
																    and state != 0
																):
																					__SCREAMING_SNAKE_CASE						   =				self.adlist[state]["fail_state"]
																__SCREAMING_SNAKE_CASE						   =				self.find_next_state(
																    UpperCAmelCase__			,			self.adlist[child]["value"]   )
																if self.adlist[child]["fail_state"] is None:
																					__SCREAMING_SNAKE_CASE						   =				0
																__SCREAMING_SNAKE_CASE						   =				(
																    self.adlist[child]["output"]
																    + self.adlist[self.adlist[child]["fail_state"]]["output"]
																)




	def   UpperCAmelCase_      ( self			:				int			,			UpperCAmelCase__			:				str   )       ->	dict[str, list[int]]:
						__SCREAMING_SNAKE_CASE						   =				{}  # returns a dict with keywords and list of its occurrences
						__SCREAMING_SNAKE_CASE						   =				0
						for i in range(len(UpperCAmelCase__   )   ):
											while (
											    self.find_next_state(UpperCAmelCase__			,			string[i]   ) is None
											    and current_state != 0
											):
																__SCREAMING_SNAKE_CASE						   =				self.adlist[current_state]["fail_state"]
											__SCREAMING_SNAKE_CASE						   =				self.find_next_state(UpperCAmelCase__			,			string[i]   )
											if next_state is None:
																__SCREAMING_SNAKE_CASE						   =				0
											else:
																__SCREAMING_SNAKE_CASE						   =				next_state
																for key in self.adlist[current_state]["output"]:
																					if key not in result:
																										__SCREAMING_SNAKE_CASE						   =				[]
																					result[key].append(i - len(UpperCAmelCase__   ) + 1   )
						return result


if __name__ == "__main__":
		import doctest

		doctest.testmod()




"""simple docstring"""







import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate




def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					return x + 2




class  UpperCamelCase_	( unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )

						__SCREAMING_SNAKE_CASE						   =				"x = y"
						__SCREAMING_SNAKE_CASE						   =				{"y": 5}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 5, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Dict   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"y = add_two(x)"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )

						# Won't work without the tool
						with CaptureStdout() as out:
											__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result is None
						assert "tried to execute add_two" in out.out




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )




	def   UpperCAmelCase_      ( self			:				str   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				int   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3\ny = 5"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"text = f'This is x: {x}.'"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == "This is x: 3."
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "text": "This is x: 3."}   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	Optional[int]:
						__SCREAMING_SNAKE_CASE						   =				"if x <= 3:\n    y = 2\nelse:\n    y = 5"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 2}   )

						__SCREAMING_SNAKE_CASE						   =				{"x": 8}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 8, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	str:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertListEqual(UpperCAmelCase__			,			[3, 5]   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"y = x"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 3}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]\ntest_list[1]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )

						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"x = 0\nfor i in range(3):\n    x = i"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"range": range}			,			state=UpperCAmelCase__   )
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 2, "i": 2}   )
