






from typing import Any


class 					_snake_case						:






	def __init__(		self					,UpperCamelCase       )						->    List[str]:
					snake_case__      :Dict         =   data
					snake_case__      :Any         =   None






	def __repr__(		self       )						->    str:
					return f'Node({self.data})'




class 					_snake_case						:






	def __init__(		self       )						->    Optional[Any]:
					snake_case__      :str         =   None






	def __iter__(		self       )						->    Any:
					snake_case__      :Optional[Any]         =   self.head
					while node:
									yield node.data
									snake_case__      :Dict         =   node.next






	def __len__(		self       )						->    int:
					return sum(1 for _ in self       )






	def __repr__(		self       )						->    str:
					return "->".join([str(UpperCamelCase       ) for item in self]       )






	def __getitem__(		self					,UpperCamelCase       )						->    Any:
					if not 0 <= index < len(self       ):
									raise ValueError("list index out of range."       )
					for i, node in enumerate(self       ):
									if i == index:
													return node
					return None






	def __setitem__(		self					,UpperCamelCase					,UpperCamelCase       )						->    None:
					if not 0 <= index < len(self       ):
									raise ValueError("list index out of range."       )
					snake_case__      :Any         =   self.head
					for _ in range(UpperCamelCase       ):
									snake_case__      :Any         =   current.next
					snake_case__      :Optional[int]         =   data






	def    lowerCAmelCase_							(		self					,UpperCamelCase       )						->    None:
					self.insert_nth(len(self       )					,UpperCamelCase       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase       )						->    None:
					self.insert_nth(0					,UpperCamelCase       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase       )						->    None:
					if not 0 <= index <= len(self       ):
									raise IndexError("list index out of range"       )
					snake_case__      :Tuple         =   Node(UpperCamelCase       )
					if self.head is None:
									snake_case__      :Dict         =   new_node
					elif index == 0:
									snake_case__      :Union[str, Any]         =   self.head  # link new_node to head
									snake_case__      :str         =   new_node
					else:
									snake_case__      :int         =   self.head
									for _ in range(index - 1       ):
													snake_case__      :Tuple         =   temp.next
									snake_case__      :List[Any]         =   temp.next
									snake_case__      :str         =   new_node






	def    lowerCAmelCase_							(		self       )						->    None:  # print every node data
					print(self       )






	def    lowerCAmelCase_							(		self       )						->    Any:
					return self.delete_nth(0       )






	def    lowerCAmelCase_							(		self       )						->    Any:  # delete from tail
					return self.delete_nth(len(self       ) - 1       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase = 0       )						->    Any:
					if not 0 <= index <= len(self       ) - 1:  # test if index is valid
									raise IndexError("List index out of range."       )
					snake_case__      :Optional[Any]         =   self.head  # default first node
					if index == 0:
									snake_case__      :Dict         =   self.head.next
					else:
									snake_case__      :Union[str, Any]         =   self.head
									for _ in range(index - 1       ):
													snake_case__      :Dict         =   temp.next
									snake_case__      :Tuple         =   temp.next
									snake_case__      :List[Any]         =   temp.next.next
					return delete_node.data






	def    lowerCAmelCase_							(		self       )						->    bool:
					return self.head is None






	def    lowerCAmelCase_							(		self       )						->    None:
					snake_case__      :Dict         =   None
					snake_case__      :Union[str, Any]         =   self.head

					while current:
									# Store the current node's next node.
									snake_case__      :Any         =   current.next
									# Make the current node's next point backwards
									snake_case__      :Any         =   prev
									# Make the previous node be the current node
									snake_case__      :int         =   current
									# Make the current node the next node (to progress iteration)
									snake_case__      :Optional[int]         =   next_node
					# Return prev in order to put the head at the end
					snake_case__      :Tuple         =   prev





def 					lowercase_ (    )     -> None:





				'''simple docstring'''


				snake_case__      :List[Any]         =   LinkedList()
				assert linked_list.is_empty() is True
				assert str(__snake_case      ) == ""

				try:
								linked_list.delete_head()
								raise AssertionError  # This should not happen.
				except IndexError:
								assert True  # This should happen.

				try:
								linked_list.delete_tail()
								raise AssertionError  # This should not happen.
				except IndexError:
								assert True  # This should happen.

				for i in range(10      ):
								assert len(__snake_case      ) == i
								linked_list.insert_nth(__snake_case	,	i + 1      )
				assert str(__snake_case      ) == "->".join(str(__snake_case      ) for i in range(1	,	11      )      )

				linked_list.insert_head(0      )
				linked_list.insert_tail(11      )
				assert str(__snake_case      ) == "->".join(str(__snake_case      ) for i in range(0	,	12      )      )

				assert linked_list.delete_head() == 0
				assert linked_list.delete_nth(9      ) == 10
				assert linked_list.delete_tail() == 11
				assert len(__snake_case      ) == 9
				assert str(__snake_case      ) == "->".join(str(__snake_case      ) for i in range(1	,	10      )      )

				assert all(linked_list[i] == i + 1 for i in range(0	,	9      )      ) is True

				for i in range(0	,	9      ):
								snake_case__      :int         =   -i
				assert all(linked_list[i] == -i for i in range(0	,	9      )      ) is True

				linked_list.reverse()
				assert str(__snake_case      ) == "->".join(str(__snake_case      ) for i in range(-8	,	1      )      )





def 					lowercase_ (    )     -> None:





				'''simple docstring'''


				snake_case__      :str         =   [
				    -9,
				    1_00,
				    Node(77_34_51_12      ),
				    "dlrow olleH",
				    7,
				    55_55,
				    0,
				    -1_9_2.5_5_5_5_5,
				    "Hello, world!",
				    7_7.9,
				    Node(10      ),
				    None,
				    None,
				    1_2.2_0,
				]
				snake_case__      :Any         =   LinkedList()

				for i in test_input:
								linked_list.insert_tail(__snake_case      )

				# Check if it's empty or not
				assert linked_list.is_empty() is False
				assert (
				    str(__snake_case      ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->"
				    "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2"
				)

				# Delete the head
				snake_case__      :Optional[int]         =   linked_list.delete_head()
				assert result == -9
				assert (
				    str(__snake_case      ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->"
				    "Hello, world!->77.9->Node(10)->None->None->12.2"
				)

				# Delete the tail
				snake_case__      :Tuple         =   linked_list.delete_tail()
				assert result == 1_2.2
				assert (
				    str(__snake_case      ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->"
				    "Hello, world!->77.9->Node(10)->None->None"
				)

				# Delete a node in specific location in linked list
				snake_case__      :List[str]         =   linked_list.delete_nth(10      )
				assert result is None
				assert (
				    str(__snake_case      ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->"
				    "Hello, world!->77.9->Node(10)->None"
				)

				# Add a Node instance to its head
				linked_list.insert_head(Node("Hello again, world!"      )      )
				assert (
				    str(__snake_case      )
				    == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->"
				    "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None"
				)

				# Add None to its tail
				linked_list.insert_tail(__snake_case      )
				assert (
				    str(__snake_case      )
				    == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->"
				    "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None"
				)

				# Reverse the linked list
				linked_list.reverse()
				assert (
				    str(__snake_case      )
				    == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->"
				    "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)"
				)





def 					lowercase_ (    )     -> List[str]:





				'''simple docstring'''


				from doctest import testmod

				testmod()

				snake_case__      :str         =   LinkedList()
				linked_list.insert_head(input("Inserting 1st at head "      ).strip()      )
				linked_list.insert_head(input("Inserting 2nd at head "      ).strip()      )
				print("\nPrint list:"      )
				linked_list.print_list()
				linked_list.insert_tail(input("\nInserting 1st at tail "      ).strip()      )
				linked_list.insert_tail(input("Inserting 2nd at tail "      ).strip()      )
				print("\nPrint list:"      )
				linked_list.print_list()
				print("\nDelete head"      )
				linked_list.delete_head()
				print("Delete tail"      )
				linked_list.delete_tail()
				print("\nPrint list:"      )
				linked_list.print_list()
				print("\nReverse linked list"      )
				linked_list.reverse()
				print("\nPrint list:"      )
				linked_list.print_list()
				print("\nString representation of linked list:"      )
				print(__snake_case      )
				print("\nReading/changing Node data using indexing:"      )
				print(F'Element at Position 1: {linked_list[1]}'      )
				snake_case__      :Union[str, Any]         =   input("Enter New Value: "      ).strip()
				print("New list:"      )
				print(__snake_case      )
				print(F'length of linked_list is : {len(__snake_case      )}'      )


if __name__ == "__main__":
	main()







import os
import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from huggingface_hub.file_download import http_get
from requests.exceptions import HTTPError

from transformers import (
    AlbertTokenizer,
    AutoTokenizer,
    BertTokenizer,
    BertTokenizerFast,
    GPTaTokenizerFast,
    is_tokenizers_available,
)
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_tokenizers
from transformers.tokenization_utils import Trie


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_tokenization import CustomTokenizer  # noqa E402


if is_tokenizers_available():
	from test_module.custom_tokenization_fast import CustomTokenizerFast


class 					_snake_case						(			unittest.TestCase ):






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					# A mock response for an HTTP head request to emulate server down
					snake_case__      :Tuple         =   mock.Mock()
					snake_case__      :List[str]         =   500
					snake_case__      :Any         =   {}
					snake_case__      :Union[str, Any]         =   HTTPError
					snake_case__      :Tuple         =   {}

					# Download this model to make sure it's in the cache.
					snake_case__      :Any         =   BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert"       )

					# Under the mock environment we get a 500 error when trying to reach the tokenizer.
					with mock.patch("requests.Session.request"					,return_value=UpperCamelCase       ) as mock_head:
									snake_case__      :Dict         =   BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert"       )
									# This check we did call the fake head request
									mock_head.assert_called()






	@require_tokenizers
	def    lowerCAmelCase_							(		self       )						->    Dict:
					# A mock response for an HTTP head request to emulate server down
					snake_case__      :Union[str, Any]         =   mock.Mock()
					snake_case__      :int         =   500
					snake_case__      :Any         =   {}
					snake_case__      :Dict         =   HTTPError
					snake_case__      :List[Any]         =   {}

					# Download this model to make sure it's in the cache.
					snake_case__      :Optional[int]         =   GPTaTokenizerFast.from_pretrained("gpt2"       )

					# Under the mock environment we get a 500 error when trying to reach the tokenizer.
					with mock.patch("requests.Session.request"					,return_value=UpperCamelCase       ) as mock_head:
									snake_case__      :Any         =   GPTaTokenizerFast.from_pretrained("gpt2"       )
									# This check we did call the fake head request
									mock_head.assert_called()






	def    lowerCAmelCase_							(		self       )						->    int:
					# This test is for deprecated behavior and can be removed in v5
					try:
									snake_case__      :Union[str, Any]         =   tempfile.mktemp()
									with open(UpperCamelCase					,"wb"       ) as f:
													http_get("https://huggingface.co/albert-base-v1/resolve/main/spiece.model"					,UpperCamelCase       )

									snake_case__      :Tuple         =   AlbertTokenizer.from_pretrained(UpperCamelCase       )
					finally:
									os.remove(UpperCamelCase       )

					# Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in
					# the current folder and have the right name.
					if os.path.isfile("tokenizer.json"       ):
									# We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it.
									return
					try:
									with open("tokenizer.json"					,"wb"       ) as f:
													http_get("https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json"					,UpperCamelCase       )
									snake_case__      :Dict         =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"       )
									# The tiny random BERT has a vocab size of 1024, tiny gpt2 as a vocab size of 1000
									self.assertEqual(tokenizer.vocab_size					,1_000       )
									# Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file.

					finally:
									os.remove("tokenizer.json"       )






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					# This test is for deprecated behavior and can be removed in v5
					snake_case__      :Union[str, Any]         =   AlbertTokenizer.from_pretrained("https://huggingface.co/albert-base-v1/resolve/main/spiece.model"       )




@is_staging_test
class 					_snake_case						(			unittest.TestCase ):
	_A          =   ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou']






	@classmethod
	def    lowerCAmelCase_							(		cls       )						->    Optional[int]:
					snake_case__      :List[str]         =   TOKEN
					HfFolder.save_token(UpperCamelCase       )






	@classmethod
	def    lowerCAmelCase_							(		cls       )						->    Union[str, Any]:
					try:
									delete_repo(token=cls._token					,repo_id="test-tokenizer"       )
					except HTTPError:
									pass

					try:
									delete_repo(token=cls._token					,repo_id="valid_org/test-tokenizer-org"       )
					except HTTPError:
									pass

					try:
									delete_repo(token=cls._token					,repo_id="test-dynamic-tokenizer"       )
					except HTTPError:
									pass






	def    lowerCAmelCase_							(		self       )						->    Optional[Any]:
					with tempfile.TemporaryDirectory() as tmp_dir:
									snake_case__      :List[str]         =   os.path.join(UpperCamelCase					,"vocab.txt"       )
									with open(UpperCamelCase					,"w"					,encoding="utf-8"       ) as vocab_writer:
													vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]       )       )
									snake_case__      :str         =   BertTokenizer(UpperCamelCase       )

					tokenizer.push_to_hub("test-tokenizer"					,use_auth_token=self._token       )
					snake_case__      :Dict         =   BertTokenizer.from_pretrained(f'{USER}/test-tokenizer'       )
					self.assertDictEqual(new_tokenizer.vocab					,tokenizer.vocab       )

					# Reset repo
					delete_repo(token=self._token					,repo_id="test-tokenizer"       )

					# Push to hub via save_pretrained
					with tempfile.TemporaryDirectory() as tmp_dir:
									tokenizer.save_pretrained(UpperCamelCase					,repo_id="test-tokenizer"					,push_to_hub=UpperCamelCase					,use_auth_token=self._token       )

					snake_case__      :List[str]         =   BertTokenizer.from_pretrained(f'{USER}/test-tokenizer'       )
					self.assertDictEqual(new_tokenizer.vocab					,tokenizer.vocab       )






	def    lowerCAmelCase_							(		self       )						->    Optional[int]:
					with tempfile.TemporaryDirectory() as tmp_dir:
									snake_case__      :List[Any]         =   os.path.join(UpperCamelCase					,"vocab.txt"       )
									with open(UpperCamelCase					,"w"					,encoding="utf-8"       ) as vocab_writer:
													vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]       )       )
									snake_case__      :Any         =   BertTokenizer(UpperCamelCase       )

					tokenizer.push_to_hub("valid_org/test-tokenizer-org"					,use_auth_token=self._token       )
					snake_case__      :Any         =   BertTokenizer.from_pretrained("valid_org/test-tokenizer-org"       )
					self.assertDictEqual(new_tokenizer.vocab					,tokenizer.vocab       )

					# Reset repo
					delete_repo(token=self._token					,repo_id="valid_org/test-tokenizer-org"       )

					# Push to hub via save_pretrained
					with tempfile.TemporaryDirectory() as tmp_dir:
									tokenizer.save_pretrained(
									    UpperCamelCase					,repo_id="valid_org/test-tokenizer-org"					,push_to_hub=UpperCamelCase					,use_auth_token=self._token       )

					snake_case__      :Union[str, Any]         =   BertTokenizer.from_pretrained("valid_org/test-tokenizer-org"       )
					self.assertDictEqual(new_tokenizer.vocab					,tokenizer.vocab       )






	@require_tokenizers
	def    lowerCAmelCase_							(		self       )						->    Any:
					CustomTokenizer.register_for_auto_class()
					with tempfile.TemporaryDirectory() as tmp_dir:
									snake_case__      :str         =   os.path.join(UpperCamelCase					,"vocab.txt"       )
									with open(UpperCamelCase					,"w"					,encoding="utf-8"       ) as vocab_writer:
													vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]       )       )
									snake_case__      :Optional[int]         =   CustomTokenizer(UpperCamelCase       )

					# No fast custom tokenizer
					tokenizer.push_to_hub("test-dynamic-tokenizer"					,use_auth_token=self._token       )

					snake_case__      :Union[str, Any]         =   AutoTokenizer.from_pretrained(f'{USER}/test-dynamic-tokenizer'					,trust_remote_code=UpperCamelCase       )
					# Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module
					self.assertEqual(tokenizer.__class__.__name__					,"CustomTokenizer"       )

					# Fast and slow custom tokenizer
					CustomTokenizerFast.register_for_auto_class()
					with tempfile.TemporaryDirectory() as tmp_dir:
									snake_case__      :int         =   os.path.join(UpperCamelCase					,"vocab.txt"       )
									with open(UpperCamelCase					,"w"					,encoding="utf-8"       ) as vocab_writer:
													vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]       )       )

									snake_case__      :Tuple         =   BertTokenizerFast.from_pretrained(UpperCamelCase       )
									bert_tokenizer.save_pretrained(UpperCamelCase       )
									snake_case__      :List[Any]         =   CustomTokenizerFast.from_pretrained(UpperCamelCase       )

					tokenizer.push_to_hub("test-dynamic-tokenizer"					,use_auth_token=self._token       )

					snake_case__      :List[Any]         =   AutoTokenizer.from_pretrained(f'{USER}/test-dynamic-tokenizer'					,trust_remote_code=UpperCamelCase       )
					# Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
					self.assertEqual(tokenizer.__class__.__name__					,"CustomTokenizerFast"       )
					snake_case__      :List[str]         =   AutoTokenizer.from_pretrained(
					    f'{USER}/test-dynamic-tokenizer'					,use_fast=UpperCamelCase					,trust_remote_code=UpperCamelCase       )
					# Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
					self.assertEqual(tokenizer.__class__.__name__					,"CustomTokenizer"       )




class 					_snake_case						(			unittest.TestCase ):






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					snake_case__      :int         =   Trie()
					trie.add("Hello 友達"       )
					self.assertEqual(trie.data					,{"H": {"e": {"l": {"l": {"o": {" ": {"友": {"達": {"": 1}}}}}}}}}       )
					trie.add("Hello"       )
					trie.data
					self.assertEqual(trie.data					,{"H": {"e": {"l": {"l": {"o": {"": 1, " ": {"友": {"達": {"": 1}}}}}}}}}       )






	def    lowerCAmelCase_							(		self       )						->    int:
					snake_case__      :List[str]         =   Trie()
					self.assertEqual(trie.split("[CLS] This is a extra_id_100"       )					,["[CLS] This is a extra_id_100"]       )
					trie.add("[CLS]"       )
					trie.add("extra_id_1"       )
					trie.add("extra_id_100"       )
					self.assertEqual(trie.split("[CLS] This is a extra_id_100"       )					,["[CLS]", " This is a ", "extra_id_100"]       )






	def    lowerCAmelCase_							(		self       )						->    str:
					snake_case__      :Optional[Any]         =   Trie()
					trie.add("A"       )
					self.assertEqual(trie.split("ABC"       )					,["A", "BC"]       )
					self.assertEqual(trie.split("BCA"       )					,["BC", "A"]       )






	def    lowerCAmelCase_							(		self       )						->    Dict:
					snake_case__      :Any         =   Trie()
					trie.add("TOKEN]"       )
					trie.add("[SPECIAL_TOKEN]"       )
					self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]"       )					,["This is something ", "[SPECIAL_TOKEN]"]       )






	def    lowerCAmelCase_							(		self       )						->    Tuple:
					snake_case__      :List[Any]         =   Trie()
					trie.add("A"       )
					trie.add("P"       )
					trie.add("[SPECIAL_TOKEN]"       )
					self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]"       )					,["This is something ", "[SPECIAL_TOKEN]"]       )






	def    lowerCAmelCase_							(		self       )						->    Tuple:
					snake_case__      :str         =   Trie()
					trie.add("AB"       )
					trie.add("B"       )
					trie.add("C"       )
					self.assertEqual(trie.split("ABC"       )					,["AB", "C"]       )






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					snake_case__      :Dict         =   Trie()
					trie.add("ABC"       )
					trie.add("B"       )
					trie.add("CD"       )
					self.assertEqual(trie.split("ABCD"       )					,["ABC", "D"]       )






	def    lowerCAmelCase_							(		self       )						->    int:
					# Even if the offsets are wrong, we necessarily output correct string
					# parts.
					snake_case__      :Optional[int]         =   Trie()
					snake_case__      :Union[str, Any]         =   trie.cut_text("ABC"					,[0, 0, 2, 1, 2, 3]       )
					self.assertEqual(UpperCamelCase					,["AB", "C"]       )