import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
  if is_torch_available():
    import torch
  if is_tf_available():
    import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


UpperCAmelCase				 =   logging.get_logger(__name__)

UpperCAmelCase				 =   {
    """vocab_file""": """vocab.json""",
    """merges_file""": """merges.txt""",
}

UpperCAmelCase				 =   {
    """vocab_file""": {
        """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt""",
    },
}

UpperCAmelCase				 =   {
    """Salesforce/codegen-350M-mono""": 2048,
}







@lru_cache()
def 						UpperCAmelCase							(		)						->						List[str]:
     a	:     Any						      =    (
         list(range(ord('''!'''							)    ,   ord('''~'''							) + 1							)							) + list(range(ord('''¡'''							)    ,   ord('''¬'''							) + 1							)							) + list(range(ord('''®'''							)    ,   ord('''ÿ'''							) + 1							)							)
     )
     a	:     List[str]						      =    bs[:]
     a	:     Optional[Any]						      =    0
     for b in range(2**8							):
          if b not in bs:
               bs.append(lowerCAmelCase_							)
               cs.append(2**8 + n							)
               n += 1
     a	:     List[Any]						      =    [chr(lowerCAmelCase_							) for n in cs]
     return dict(zip(lowerCAmelCase_    ,   lowerCAmelCase_							)							)







def 						UpperCAmelCase							(		lowerCAmelCase_							)						->						Any:
     a	:     Dict						      =    set()
     a	:     Any						      =    word[0]
     for char in word[1:]:
          pairs.add((prev_char, char)							)
          a	:     List[Any]						      =    char
     return pairs






class 				_SCREAMING_SNAKE_CASE( snake_case__						):
   UpperCamelCase_					:    List[str]			      = VOCAB_FILES_NAMES
   UpperCamelCase_					:    List[Any]			      = PRETRAINED_VOCAB_FILES_MAP
   UpperCamelCase_					:    List[Any]			      = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   UpperCamelCase_					:    Dict			      = ['input_ids', 'attention_mask']
   def __init__(     self				,  lowerCAmelCase_				,  lowerCAmelCase_				,  lowerCAmelCase_="replace"				,  lowerCAmelCase_="<|endoftext|>"				,  lowerCAmelCase_="<|endoftext|>"				,  lowerCAmelCase_="<|endoftext|>"				,  lowerCAmelCase_=None				,  lowerCAmelCase_=False				,  lowerCAmelCase_=False				,  **lowerCAmelCase_				,  )						->str:
        a	:     List[str]						      =    AddedToken(lowerCAmelCase_				,  lstrip=lowerCAmelCase_				,  rstrip=lowerCAmelCase_     ) if isinstance(lowerCAmelCase_				,  lowerCAmelCase_     ) else bos_token
        a	:     Union[str, Any]						      =    AddedToken(lowerCAmelCase_				,  lstrip=lowerCAmelCase_				,  rstrip=lowerCAmelCase_     ) if isinstance(lowerCAmelCase_				,  lowerCAmelCase_     ) else eos_token
        a	:     Dict						      =    AddedToken(lowerCAmelCase_				,  lstrip=lowerCAmelCase_				,  rstrip=lowerCAmelCase_     ) if isinstance(lowerCAmelCase_				,  lowerCAmelCase_     ) else unk_token
        a	:     int						      =    AddedToken(lowerCAmelCase_				,  lstrip=lowerCAmelCase_				,  rstrip=lowerCAmelCase_     ) if isinstance(lowerCAmelCase_				,  lowerCAmelCase_     ) else pad_token
        super().__init__(
            errors=lowerCAmelCase_				,  unk_token=lowerCAmelCase_				,  bos_token=lowerCAmelCase_				,  eos_token=lowerCAmelCase_				,  pad_token=lowerCAmelCase_				,  add_prefix_space=lowerCAmelCase_				,  add_bos_token=lowerCAmelCase_				,  **lowerCAmelCase_				,  )
        a	:     Optional[int]						      =    add_bos_token

        with open(lowerCAmelCase_				,  encoding='''utf-8'''     ) as vocab_handle:
             a	:     Optional[Any]						      =    json.load(lowerCAmelCase_     )
        a	:     Any						      =    {v: k for k, v in self.encoder.items()}
        a	:     List[str]						      =    errors  # how to handle errors in decoding
        a	:     Optional[Any]						      =    bytes_to_unicode()
        a	:     str						      =    {v: k for k, v in self.byte_encoder.items()}
        with open(lowerCAmelCase_				,  encoding='''utf-8'''     ) as merges_handle:
             a	:     int						      =    merges_handle.read().split('''\n'''     )[1:-1]
        a	:     str						      =    [tuple(merge.split()     ) for merge in bpe_merges]
        a	:     Dict						      =    dict(zip(lowerCAmelCase_				,  range(len(lowerCAmelCase_     )     )     )     )
        a	:     Optional[int]						      =    {}
        a	:     Optional[Any]						      =    add_prefix_space

        # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
        a	:     Any						      =    re.compile(r'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'''     )
   @property
   def _lowercase(     self     )						->List[str]:
        return len(self.encoder     )
   def _lowercase(     self     )						->Union[str, Any]:
        return dict(self.encoder				,  **self.added_tokens_encoder     )
   def _lowercase(     self				,  lowerCAmelCase_     )						->str:
        if token in self.cache:
             return self.cache[token]
        a	:     str						      =    tuple(lowerCAmelCase_     )
        a	:     Union[str, Any]						      =    get_pairs(lowerCAmelCase_     )

        if not pairs:
             return token

        while True:
             a	:     Optional[int]						      =    min(lowerCAmelCase_				,  key=lambda lowerCAmelCase_     : self.bpe_ranks.get(lowerCAmelCase_				,  float('''inf'''     )     )     )
             if bigram not in self.bpe_ranks:
                  break
             a      ,				a	:     List[str]						      =    bigram
             a	:     str						      =    []
             a	:     int						      =    0
             while i < len(lowerCAmelCase_     ):
                  try:
                       a	:     Tuple						      =    word.index(lowerCAmelCase_				,  lowerCAmelCase_     )
                  except ValueError:
                       new_word.extend(word[i:]     )
                       break
                  else:
                       new_word.extend(word[i:j]     )
                       a	:     Dict						      =    j

                  if word[i] == first and i < len(lowerCAmelCase_     ) - 1 and word[i + 1] == second:
                       new_word.append(first + second     )
                       i += 2
                  else:
                       new_word.append(word[i]     )
                       i += 1
             a	:     List[str]						      =    tuple(lowerCAmelCase_     )
             a	:     Tuple						      =    new_word
             if len(lowerCAmelCase_     ) == 1:
                  break
             else:
                  a	:     Optional[Any]						      =    get_pairs(lowerCAmelCase_     )
        a	:     Tuple						      =    ''' '''.join(lowerCAmelCase_     )
        a	:     List[Any]						      =    word
        return word
   def _lowercase(     self				,  lowerCAmelCase_				,  lowerCAmelCase_=None     )						->Dict:
        if self.add_bos_token:
             a	:     Any						      =    [self.bos_token_id]
        else:
             a	:     List[str]						      =    []

        a	:     Union[str, Any]						      =    bos_token_ids + token_ids_a

        if token_ids_a is None:
             return output

        return output + bos_token_ids + token_ids_a
   def _lowercase(     self				,  lowerCAmelCase_     )						->str:
        a	:     Dict						      =    []
        for token in re.findall(self.pat				,  lowerCAmelCase_     ):
             a	:     Optional[Any]						      =    ''''''.join(
                 self.byte_encoder[b] for b in token.encode('''utf-8'''     )     )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
             bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase_     ).split(''' '''     )     )
        return bpe_tokens
   def _lowercase(     self				,  lowerCAmelCase_     )						->List[str]:
        return self.encoder.get(lowerCAmelCase_				,  self.encoder.get(self.unk_token     )     )
   def _lowercase(     self				,  lowerCAmelCase_     )						->Dict:
        return self.decoder.get(lowerCAmelCase_     )
   def _lowercase(     self				,  lowerCAmelCase_     )						->Any:
        a	:     str						      =    ''''''.join(lowerCAmelCase_     )
        a	:     int						      =    bytearray([self.byte_decoder[c] for c in text]     ).decode('''utf-8'''				,  errors=self.errors     )
        return text
   def _lowercase(     self				,  lowerCAmelCase_				,  lowerCAmelCase_ = None     )						->Tuple[str]:
        if not os.path.isdir(lowerCAmelCase_     ):
             logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""     )
             return
        a	:     Optional[int]						      =    os.path.join(
            lowerCAmelCase_				,  (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']     )
        a	:     Any						      =    os.path.join(
            lowerCAmelCase_				,  (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']     )

        with open(lowerCAmelCase_				,  '''w'''				,  encoding='''utf-8'''     ) as f:
             f.write(json.dumps(self.encoder				,  indent=2				,  sort_keys=lowerCAmelCase_				,  ensure_ascii=lowerCAmelCase_     ) + '''\n'''     )

        a	:     Any						      =    0
        with open(lowerCAmelCase_				,  '''w'''				,  encoding='''utf-8'''     ) as writer:
             writer.write('''#version: 0.2\n'''     )
             for bpe_tokens, token_index in sorted(self.bpe_ranks.items()				,  key=lambda lowerCAmelCase_     : kv[1]     ):
                  if index != token_index:
                       logger.warning(
                           F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
                           ''' Please check that the tokenizer is not corrupted!'''     )
                       a	:     Any						      =    token_index
                  writer.write(''' '''.join(lowerCAmelCase_     ) + '''\n'''     )
                  index += 1

        return vocab_file, merge_file
   def _lowercase(     self				,  lowerCAmelCase_				,  lowerCAmelCase_=False				,  **lowerCAmelCase_     )						->List[Any]:
        a	:     List[Any]						      =    kwargs.pop('''add_prefix_space'''				,  self.add_prefix_space     )
        if is_split_into_words or add_prefix_space:
             a	:     Tuple						      =    ''' ''' + text
        return (text, kwargs)
   def _lowercase(     self				,  lowerCAmelCase_				,  lowerCAmelCase_ = False				,  lowerCAmelCase_ = None				,  lowerCAmelCase_ = None				,  **lowerCAmelCase_				,  )						->str:
        a	:     Dict						      =    super()._decode(
            token_ids=lowerCAmelCase_				,  skip_special_tokens=lowerCAmelCase_				,  clean_up_tokenization_spaces=lowerCAmelCase_				,  **lowerCAmelCase_				,  )

        if truncate_before_pattern is not None and len(lowerCAmelCase_     ) > 0:
             a	:     Dict						      =    self.truncate(lowerCAmelCase_				,  lowerCAmelCase_     )

        return decoded_text


   def _lowercase(     self				,  lowerCAmelCase_				,  lowerCAmelCase_     )						->List[str]:
        def find_re(lowerCAmelCase_				,  lowerCAmelCase_				,  lowerCAmelCase_     ):
             a	:     Tuple						      =    pattern.search(lowerCAmelCase_				,  lowerCAmelCase_     )
             return m.start() if m else -1

        a	:     Optional[Any]						      =    [re.compile(lowerCAmelCase_				,  re.MULTILINE     ) for pattern in truncate_before_pattern]

        a	:     Union[str, Any]						      =    list(re.finditer('''^print'''				,  lowerCAmelCase_				,  re.MULTILINE     )     )

        if len(lowerCAmelCase_     ) > 1:
             a	:     List[str]						      =    completion[: prints[1].start()]

        a	:     Any						      =    list(re.finditer('''^def'''				,  lowerCAmelCase_				,  re.MULTILINE     )     )

        if len(lowerCAmelCase_     ) > 1:
             a	:     Optional[int]						      =    completion[: defs[1].start()]

        a	:     Tuple						      =    0

        a	:     str						      =    [
            pos for pos in [find_re(lowerCAmelCase_				,  lowerCAmelCase_				,  lowerCAmelCase_     ) for terminal in terminals] if pos != -1
        ]

        if len(lowerCAmelCase_     ) > 0:
             return completion[: min(lowerCAmelCase_     )]
        else:
             return completion
import math







def 						UpperCAmelCase							(		lowerCAmelCase_							)						->						int:
     return sum(int(lowerCAmelCase_							) for digit in str(lowerCAmelCase_							)							)







def 						UpperCAmelCase							(		lowerCAmelCase_ = 3_0							)						->						int:
     a	:     Optional[int]						      =    []
     for digit in range(2    ,   1_0_0							):
          for power in range(2    ,   1_0_0							):
               a	:     List[str]						      =    int(math.pow(lowerCAmelCase_    ,   lowerCAmelCase_							)							)
               if digit == digit_sum(lowerCAmelCase_							):
                    digit_to_powers.append(lowerCAmelCase_							)

     digit_to_powers.sort()
     return digit_to_powers[n - 1]


if __name__ == "__main__":
  print(solution())