code: 
  
  
  
  
  
  class 				_lowerCamelCase :
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase   )       ->   int:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				str        =       data
          __snake_case   :				List[str]        =       None
          __snake_case   :				int        =       None
  
  
  
  
  
  
  
        def __str__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return F"""{self.data}"""
  
  
  
  
  class 				_lowerCamelCase :
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self   )       ->   Optional[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[int]        =       None
          __snake_case   :				List[Any]        =       None
  
  
  
  
  
  
  
        def __iter__( self   )       ->   Optional[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Dict        =       self.head
          while node:
            yield node.data
            __snake_case   :				Tuple        =       node.next
  
  
  
  
  
  
  
        def __str__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return "->".join([str(UpperCAmelCase   ) for item in self]   )
  
  
  
  
  
  
  
        def __len__( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return sum(1 for _ in self   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   List[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          self.insert_at_nth(0				,						UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          self.insert_at_nth(len(self   )				,						UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   Union[str, Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				int        =       len(self   )
  
          if not 0 <= index <= length:
            raise IndexError("list index out of range"   )
          __snake_case   :				Union[str, Any]        =       Node(UpperCAmelCase   )
          if self.head is None:
            __snake_case   :				Dict        =       new_node
          elif index == 0:
            __snake_case   :				Optional[Any]        =       new_node
            __snake_case   :				Optional[Any]        =       self.head
            __snake_case   :				Any        =       new_node
          elif index == length:
            __snake_case   :				Dict        =       new_node
            __snake_case   :				Dict        =       self.tail
            __snake_case   :				List[Any]        =       new_node
          else:
            __snake_case   :				List[str]        =       self.head
            for _ in range(0				,						UpperCAmelCase   ):
              __snake_case   :				Union[str, Any]        =       temp.next
            __snake_case   :				Tuple        =       new_node
            __snake_case   :				Any        =       temp.previous
            __snake_case   :				Optional[int]        =       temp
            __snake_case   :				Optional[Any]        =       new_node
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.delete_at_nth(0   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.delete_at_nth(len(self   ) - 1   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[Any]        =       len(self   )
  
          if not 0 <= index <= length - 1:
            raise IndexError("list index out of range"   )
          __snake_case   :				Union[str, Any]        =       self.head  # default first node
          if length == 1:
            __snake_case   :				Optional[Any]        =       None
          elif index == 0:
            __snake_case   :				Optional[Any]        =       self.head.next
            __snake_case   :				Dict        =       None
          elif index == length - 1:
            __snake_case   :				Dict        =       self.tail
            __snake_case   :				List[str]        =       self.tail.previous
            __snake_case   :				Union[str, Any]        =       None
          else:
            __snake_case   :				Dict        =       self.head
            for _ in range(0				,						UpperCAmelCase   ):
              __snake_case   :				Union[str, Any]        =       temp.next
            __snake_case   :				int        =       temp
            __snake_case   :				Any        =       temp.previous
            __snake_case   :				Any        =       temp.next
          return delete_node.data
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[int]        =       self.head
  
          while current.data != data:  # Find the position to delete
            if current.next:
              __snake_case   :				str        =       current.next
            else:  # We have reached the end an no value matches
              raise ValueError("No data matching given value"   )
  
          if current == self.head:
            self.delete_head()
  
          elif current == self.tail:
            self.delete_tail()
  
          else:  # Before: 1 <--> 2(current) <--> 3
            __snake_case   :				Union[str, Any]        =       current.next  # 1 --> 3
            __snake_case   :				Optional[Any]        =       current.previous  # 1 <--> 3
          return data
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return len(self   ) == 0
  
  
  
  
  
  
  
  def       lowerCAmelCase__(	) ->    None:
    __snake_case   :				Optional[Any]        =       DoublyLinkedList()
    assert linked_list.is_empty() is True
    assert str(lowercase				) == ""
  
    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(lowercase				) == i
      linked_list.insert_at_nth(lowercase   ,     i + 1				)
    assert str(lowercase				) == "->".join(str(lowercase				) for i in range(1   ,     11				)				)
  
    linked_list.insert_at_head(0				)
    linked_list.insert_at_tail(11				)
    assert str(lowercase				) == "->".join(str(lowercase				) for i in range(0   ,     12				)				)
  
    assert linked_list.delete_head() == 0
    assert linked_list.delete_at_nth(9				) == 10
    assert linked_list.delete_tail() == 11
    assert len(lowercase				) == 9
    assert str(lowercase				) == "->".join(str(lowercase				) for i in range(1   ,     10				)				)
  
  
  if __name__ == "__main__":
        from doctest import testmod
  
        testmod()
  
  
style_context: 
  
  
  
  
  
  from __future__ import annotations
  
  from collections.abc import Callable
  from typing import Generic, TypeVar
  
  _UpperCamelCase       = TypeVar('''T''')
  _UpperCamelCase       = TypeVar('''U''')
  
  
  
  
  class 				_lowerCamelCase (    Generic[T, U]    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Any        =       key
          __snake_case   :				Tuple        =       val
          __snake_case   :				int        =       0
          __snake_case   :				DoubleLinkedListNode[T, U] | None        =       None
          __snake_case   :				DoubleLinkedListNode[T, U] | None        =       None
  
  
  
  
  
  
  
        def __repr__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return "Node: key: {}, val: {}, freq: {}, has next: {}, has prev: {}".format(
              self.key				,						self.val				,						self.freq				,						self.next is not None				,						self.prev is not None   )
  
  
  
  
  class 				_lowerCamelCase (    Generic[T, U]    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self   )       ->   None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				DoubleLinkedListNode[T, U]        =       DoubleLinkedListNode(UpperCAmelCase				,						UpperCAmelCase   )
          __snake_case   :				DoubleLinkedListNode[T, U]        =       DoubleLinkedListNode(UpperCAmelCase				,						UpperCAmelCase   )
          __snake_case    ,   __snake_case   :				Any        =       self.rear, self.head
  
  
  
  
  
  
  
        def __repr__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Union[str, Any]        =       ["DoubleLinkedList"]
          __snake_case   :				List[Any]        =       self.head
          while node.next is not None:
            rep.append(str(UpperCAmelCase   )   )
            __snake_case   :				List[Any]        =       node.next
          rep.append(str(self.rear   )   )
          return ",\n    ".join(UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Dict        =       self.rear.prev
  
          # All nodes other than self.head are guaranteed to have non-None previous
          assert previous is not None
  
          __snake_case   :				int        =       node
          __snake_case   :				List[str]        =       previous
          __snake_case   :				Optional[int]        =       node
          __snake_case   :				int        =       self.rear
          node.freq += 1
          self._position_node(UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          while node.prev is not None and node.prev.freq > node.freq:
            # swap node with previous node
            __snake_case   :				List[Any]        =       node.prev
  
            __snake_case   :				Dict        =       previous_node.prev
            __snake_case   :				Union[str, Any]        =       node.prev
            __snake_case   :				Union[str, Any]        =       previous_node
            __snake_case   :				int        =       node
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   DoubleLinkedListNode[T, U] | None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if node.prev is None or node.next is None:
            return None
  
          __snake_case   :				Optional[int]        =       node.next
          __snake_case   :				Optional[Any]        =       node.prev
          __snake_case   :				Optional[int]        =       None
          __snake_case   :				Any        =       None
          return node
  
  
  
  
  class 				_lowerCamelCase (    Generic[T, U]    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        UpperCAmelCase_						:      dict[Callable[[T], U], LFUCache[T, U]]       ={}
  
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				DoubleLinkedList[T, U]        =       DoubleLinkedList()
          __snake_case   :				str        =       capacity
          __snake_case   :				str        =       0
          __snake_case   :				Optional[int]        =       0
          __snake_case   :				Any        =       0
          __snake_case   :				dict[T, DoubleLinkedListNode[T, U]]        =       {}
  
  
  
  
  
  
  
        def __repr__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return (
              F"""CacheInfo(hits={self.hits}, misses={self.miss}, """
              F"""capacity={self.capacity}, current_size={self.num_keys})"""
          )
  
  
  
  
  
  
  
        def __contains__( self				,						UpperCAmelCase   )       ->   bool:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return key in self.cache
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   U | None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if key in self.cache:
            self.hits += 1
            __snake_case   :				DoubleLinkedListNode[T, U]        =       self.cache[key]
            __snake_case   :				Union[str, Any]        =       self.list.remove(self.cache[key]   )
            assert node == value_node
  
            # node is guaranteed not None because it is in self.cache
            assert node is not None
            self.list.add(UpperCAmelCase   )
            return node.val
          self.miss += 1
          return None
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   None:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if key not in self.cache:
            if self.num_keys >= self.capacity:
              # delete first node when over capacity
              __snake_case   :				Optional[Any]        =       self.list.head.next
  
              # guaranteed to have a non-None first node when num_keys > 0
              # explain to type checker via assertions
              assert first_node is not None
              assert first_node.key is not None
              assert self.list.remove(UpperCAmelCase   ) is not None
              # first_node guaranteed to be in list
  
              del self.cache[first_node.key]
              self.num_keys -= 1
            __snake_case   :				Union[str, Any]        =       DoubleLinkedListNode(UpperCAmelCase				,						UpperCAmelCase   )
            self.list.add(self.cache[key]   )
            self.num_keys += 1
  
          else:
            __snake_case   :				Union[str, Any]        =       self.list.remove(self.cache[key]   )
            assert node is not None  # node guaranteed to be in list
            __snake_case   :				Optional[Any]        =       value
            self.list.add(UpperCAmelCase   )
  
  
  
  
  
  
  
        @classmethod
        def 						UpperCAmelCase		( cls				,						UpperCAmelCase = 128   )       ->   Callable[[Callable[[T], U]], Callable[..., U]]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          def cache_decorator_inner(UpperCAmelCase   ) -> Callable[..., U]:
            def cache_decorator_wrapper(*UpperCAmelCase   ) -> U:
              if func not in cls.decorator_function_to_instance_map:
                __snake_case   :				str        =       LFUCache(UpperCAmelCase   )
  
              __snake_case   :				Dict        =       cls.decorator_function_to_instance_map[func].get(args[0]   )
              if result is None:
                __snake_case   :				Optional[int]        =       func(*UpperCAmelCase   )
                cls.decorator_function_to_instance_map[func].put(args[0]				,						UpperCAmelCase   )
              return result
  
            def cache_info() -> LFUCache[T, U]:
              return cls.decorator_function_to_instance_map[func]
  
            setattr(UpperCAmelCase				,						"cache_info"				,						UpperCAmelCase   )  # noqa: B010
  
            return cache_decorator_wrapper
  
          return cache_decorator_inner
  
  
  if __name__ == "__main__":
       import doctest
  
       doctest.testmod()
  
  