

import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MODEL_FOR_PRETRAINING_MAPPING,
     FunnelBaseModel,
     FunnelForMaskedLM,
     FunnelForMultipleChoice,
     FunnelForPreTraining,
     FunnelForQuestionAnswering,
     FunnelForSequenceClassification,
     FunnelForTokenClassification,
     FunnelModel,
 )



class 						a_			:



      """simple docstring"""







      def __init__( self	:				str     ,snake_case	:				Tuple     ,snake_case	:				List[str]=13     ,snake_case	:				Any=7     ,snake_case	:				List[Any]=True     ,snake_case	:				str=True     ,snake_case	:				List[Any]=True     ,snake_case	:				Tuple=True     ,snake_case	:				str=99     ,snake_case	:				List[str]=[1, 1, 2]     ,snake_case	:				List[Any]=1     ,snake_case	:				Optional[int]=32     ,snake_case	:				Optional[Any]=4     ,snake_case	:				Dict=8     ,snake_case	:				List[str]=37     ,snake_case	:				str="gelu_new"     ,snake_case	:				int=0.1     ,snake_case	:				List[Any]=0.1     ,snake_case	:				List[str]=0.0     ,snake_case	:				str=512     ,snake_case	:				Optional[Any]=3     ,snake_case	:				Tuple=0.02     ,snake_case	:				Any=3     ,snake_case	:				Dict=4     ,snake_case	:				Tuple=None     ,snake_case	:				int=False     ,):
           SCREAMING_SNAKE_CASE 		=parent
           SCREAMING_SNAKE_CASE 		=batch_size
           SCREAMING_SNAKE_CASE 		=seq_length
           SCREAMING_SNAKE_CASE 		=is_training
           SCREAMING_SNAKE_CASE 		=use_input_mask
           SCREAMING_SNAKE_CASE 		=use_token_type_ids
           SCREAMING_SNAKE_CASE 		=use_labels
           SCREAMING_SNAKE_CASE 		=vocab_size
           SCREAMING_SNAKE_CASE 		=block_sizes
           SCREAMING_SNAKE_CASE 		=num_decoder_layers
           SCREAMING_SNAKE_CASE 		=d_model
           SCREAMING_SNAKE_CASE 		=n_head
           SCREAMING_SNAKE_CASE 		=d_head
           SCREAMING_SNAKE_CASE 		=d_inner
           SCREAMING_SNAKE_CASE 		=hidden_act
           SCREAMING_SNAKE_CASE 		=hidden_dropout
           SCREAMING_SNAKE_CASE 		=attention_dropout
           SCREAMING_SNAKE_CASE 		=activation_dropout
           SCREAMING_SNAKE_CASE 		=max_position_embeddings
           SCREAMING_SNAKE_CASE 		=type_vocab_size
           SCREAMING_SNAKE_CASE 		=2
           SCREAMING_SNAKE_CASE 		=num_labels
           SCREAMING_SNAKE_CASE 		=num_choices
           SCREAMING_SNAKE_CASE 		=scope
           SCREAMING_SNAKE_CASE 		=initializer_std

           # Used in the tests to check the size of the first attention layer
           SCREAMING_SNAKE_CASE 		=n_head
           # Used in the tests to check the size of the first hidden state
           SCREAMING_SNAKE_CASE 		=self.d_model
           # Used in the tests to check the number of output hidden states/attentions
           SCREAMING_SNAKE_CASE 		=sum(self.block_sizes							) + (0 if base else self.num_decoder_layers)
           # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
           # the last hidden state of the first block (which is the first hidden state of the decoder).
           if not base:
                SCREAMING_SNAKE_CASE 		=self.num_hidden_layers + 2
      def 			_lowerCAmelCase	( self	:				Optional[int]							):
           SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size							)

           SCREAMING_SNAKE_CASE 		=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,vocab_size=2							)

           SCREAMING_SNAKE_CASE 		=None
           if self.use_token_type_ids:
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.type_vocab_size							)

           SCREAMING_SNAKE_CASE 		=None
           SCREAMING_SNAKE_CASE 		=None
           SCREAMING_SNAKE_CASE 		=None
           if self.use_labels:
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size]     ,self.type_sequence_label_size							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size]     ,self.num_choices							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,1							)

           SCREAMING_SNAKE_CASE 		=self.get_config()

           return (
               config,
               input_ids,
               token_type_ids,
               input_mask,
               sequence_labels,
               token_labels,
               choice_labels,
               fake_token_labels,
           )
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           return FunnelConfig(
               vocab_size=self.vocab_size     ,block_sizes=self.block_sizes     ,num_decoder_layers=self.num_decoder_layers     ,d_model=self.d_model     ,n_head=self.n_head     ,d_head=self.d_head     ,d_inner=self.d_inner     ,hidden_act=self.hidden_act     ,hidden_dropout=self.hidden_dropout     ,attention_dropout=self.attention_dropout     ,activation_dropout=self.activation_dropout     ,max_position_embeddings=self.max_position_embeddings     ,type_vocab_size=self.type_vocab_size     ,initializer_std=self.initializer_std     ,)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]     ,snake_case	:				Optional[Any]     ,snake_case	:				List[Any]     ,snake_case	:				Optional[Any]     ,snake_case	:				Any     ,snake_case	:				Any     ,snake_case	:				Optional[int]     ,snake_case	:				Any     ,snake_case	:				List[Any]     ,):
           SCREAMING_SNAKE_CASE 		=FunnelModel(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.d_model)							)

           SCREAMING_SNAKE_CASE 		=False
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.d_model)							)

           SCREAMING_SNAKE_CASE 		=False
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.d_model)							)
      def 			_lowerCAmelCase	( self	:				Dict     ,snake_case	:				Optional[int]     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				Any     ,snake_case	:				List[Any]     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				str     ,):
           SCREAMING_SNAKE_CASE 		=FunnelBaseModel(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, 2, self.d_model)							)

           SCREAMING_SNAKE_CASE 		=False
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, 3, self.d_model)							)

           SCREAMING_SNAKE_CASE 		=False
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, 2, self.d_model)							)
      def 			_lowerCAmelCase	( self	:				List[str]     ,snake_case	:				Any     ,snake_case	:				Tuple     ,snake_case	:				List[str]     ,snake_case	:				Optional[int]     ,snake_case	:				int     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				Union[str, Any]     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=FunnelForPreTraining(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length)							)
      def 			_lowerCAmelCase	( self	:				List[str]     ,snake_case	:				Union[str, Any]     ,snake_case	:				Optional[Any]     ,snake_case	:				int     ,snake_case	:				str     ,snake_case	:				int     ,snake_case	:				Tuple     ,snake_case	:				List[Any]     ,snake_case	:				Optional[int]     ,):
           SCREAMING_SNAKE_CASE 		=FunnelForMaskedLM(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)							)
      def 			_lowerCAmelCase	( self	:				Dict     ,snake_case	:				Optional[int]     ,snake_case	:				int     ,snake_case	:				Tuple     ,snake_case	:				Optional[int]     ,snake_case	:				List[str]     ,snake_case	:				str     ,snake_case	:				List[str]     ,snake_case	:				Dict     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=FunnelForSequenceClassification(snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_labels)							)
      def 			_lowerCAmelCase	( self	:				Dict     ,snake_case	:				Dict     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				List[Any]     ,snake_case	:				Tuple     ,snake_case	:				List[Any]     ,snake_case	:				Tuple     ,snake_case	:				str     ,):
           SCREAMING_SNAKE_CASE 		=self.num_choices
           SCREAMING_SNAKE_CASE 		=FunnelForMultipleChoice(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=input_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=token_type_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=input_mask.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case     ,)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_choices)							)
      def 			_lowerCAmelCase	( self	:				Any     ,snake_case	:				Union[str, Any]     ,snake_case	:				Tuple     ,snake_case	:				Optional[Any]     ,snake_case	:				Any     ,snake_case	:				Any     ,snake_case	:				str     ,snake_case	:				str     ,snake_case	:				List[str]     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=FunnelForTokenClassification(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels)							)
      def 			_lowerCAmelCase	( self	:				int     ,snake_case	:				str     ,snake_case	:				Dict     ,snake_case	:				Dict     ,snake_case	:				Any     ,snake_case	:				str     ,snake_case	:				Union[str, Any]     ,snake_case	:				List[Any]     ,snake_case	:				Any     ,):
           SCREAMING_SNAKE_CASE 		=FunnelForQuestionAnswering(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,start_positions=snake_case     ,end_positions=snake_case     ,)
           self.parent.assertEqual(result.start_logits.shape     ,(self.batch_size, self.seq_length)							)
           self.parent.assertEqual(result.end_logits.shape     ,(self.batch_size, self.seq_length)							)


      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=self.prepare_config_and_inputs()
           (
               SCREAMING_SNAKE_CASE							
           ) 		=config_and_inputs
           SCREAMING_SNAKE_CASE 		={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict







@require_torch
class 						a_			(				A__					,     A__					,     unittest.TestCase	):



      """simple docstring"""







      __UpperCAmelCase     						= False
      __UpperCAmelCase     						= False
      __UpperCAmelCase     						= (
          (
              FunnelModel,
              FunnelForMaskedLM,
              FunnelForPreTraining,
              FunnelForQuestionAnswering,
              FunnelForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      __UpperCAmelCase     						= (
          {
              'feature-extraction': (FunnelBaseModel, FunnelModel),
              'fill-mask': FunnelForMaskedLM,
              'question-answering': FunnelForQuestionAnswering,
              'text-classification': FunnelForSequenceClassification,
              'token-classification': FunnelForTokenClassification,
              'zero-shot': FunnelForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      def 			_lowerCAmelCase	( self	:				str     ,snake_case	:				Optional[Any]     ,snake_case	:				Any     ,snake_case	:				Optional[int]=False							):
           SCREAMING_SNAKE_CASE 		=super()._prepare_for_class(snake_case     ,snake_case     ,return_labels=snake_case							)

           if return_labels:
                if model_class in get_values(snake_case							):
                     SCREAMING_SNAKE_CASE 		=torch.zeros(
                         (self.model_tester.batch_size, self.model_tester.seq_length)     ,dtype=torch.long     ,device=snake_case							)
           return inputs_dict
      def 			_lowerCAmelCase	( self	:				Tuple							):
           SCREAMING_SNAKE_CASE 		=FunnelModelTester(self							)
           SCREAMING_SNAKE_CASE 		=ConfigTester(self     ,config_class=snake_case							)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           self.config_tester.run_common_tests()
      def 			_lowerCAmelCase	( self	:				Any							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*snake_case							)
      def 			_lowerCAmelCase	( self	:				str							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Tuple							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*snake_case							)


      def 			_lowerCAmelCase	( self	:				Tuple     ,snake_case	:				List[str]							):

           if hasattr(snake_case     ,'weight'							) and module.weight is not None:
                module.weight.data.fill_(3							)
           if hasattr(snake_case     ,'bias'							) and module.bias is not None:
                module.bias.data.fill_(3							)

           for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                if hasattr(snake_case     ,snake_case							) and getattr(snake_case     ,snake_case							) is not None:
                     SCREAMING_SNAKE_CASE 		=getattr(snake_case     ,snake_case							)
                     weight.data.fill_(3							)







@require_torch
class 						a_			(				A__					,     unittest.TestCase	):



      """simple docstring"""







      __UpperCAmelCase     						= False
      __UpperCAmelCase     						= False
      __UpperCAmelCase     						= (
          (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
      )
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=FunnelModelTester(self     ,base=snake_case							)
           SCREAMING_SNAKE_CASE 		=ConfigTester(self     ,config_class=snake_case							)
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           self.config_tester.run_common_tests()
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_base_model(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Optional[int]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*snake_case							)
      def 			_lowerCAmelCase	( self	:				List[str]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Tuple							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_common()
           SCREAMING_SNAKE_CASE 		=True

           for model_class in self.all_model_classes:
                if model_class.__name__ == "FunnelBaseModel":
                     continue
                SCREAMING_SNAKE_CASE 		=model_class(snake_case							)
                model.to(snake_case							)
                model.train()
                SCREAMING_SNAKE_CASE 		=self._prepare_for_class(snake_case     ,snake_case     ,return_labels=snake_case							)
                SCREAMING_SNAKE_CASE 		=model(**snake_case							).loss
                loss.backward()


      def 			_lowerCAmelCase	( self	:				Optional[Any]     ,snake_case	:				Any							):

           if hasattr(snake_case     ,'weight'							) and module.weight is not None:
                module.weight.data.fill_(3							)
           if hasattr(snake_case     ,'bias'							) and module.bias is not None:
                module.bias.data.fill_(3							)

           for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                if hasattr(snake_case     ,snake_case							) and getattr(snake_case     ,snake_case							) is not None:
                     SCREAMING_SNAKE_CASE 		=getattr(snake_case     ,snake_case							)
                     weight.data.fill_(3							)







@require_torch
@require_sentencepiece
@require_tokenizers
class 						a_			(				unittest.TestCase	):



      """simple docstring"""







      def 			_lowerCAmelCase	( self	:				List[str]							):
           SCREAMING_SNAKE_CASE 		=13
           SCREAMING_SNAKE_CASE 		=7
           SCREAMING_SNAKE_CASE 		=torch.arange(0     ,batch_size * sequence_length							).long().reshape(snake_case     ,snake_case							)
           SCREAMING_SNAKE_CASE 		=[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
           SCREAMING_SNAKE_CASE 		=torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]							)

           SCREAMING_SNAKE_CASE 		=FunnelModel.from_pretrained('sgugger/funnel-random-tiny'							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,token_type_ids=snake_case							)[0].abs()

           SCREAMING_SNAKE_CASE 		=torch.tensor(2344.8352							)
           SCREAMING_SNAKE_CASE 		=torch.tensor(0.8_052							)
           self.assertTrue(torch.allclose(output.sum()     ,snake_case     ,atol=1e-4							)							)
           self.assertTrue(torch.allclose(output.mean()     ,snake_case     ,atol=1e-4							)							)

           SCREAMING_SNAKE_CASE 		=torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case							)[0].abs()

           SCREAMING_SNAKE_CASE 		=torch.tensor(2343.8425							)
           SCREAMING_SNAKE_CASE 		=torch.tensor(0.8_049							)
           self.assertTrue(torch.allclose(output.sum()     ,snake_case     ,atol=1e-4							)							)
           self.assertTrue(torch.allclose(output.mean()     ,snake_case     ,atol=1e-4							)							)


      @slow
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=FunnelTokenizer.from_pretrained('huggingface/funnel-small'							)
           SCREAMING_SNAKE_CASE 		=FunnelModel.from_pretrained('huggingface/funnel-small'							)
           SCREAMING_SNAKE_CASE 		=tokenizer('Hello! I am the Funnel Transformer model.'     ,return_tensors='pt'							)
           SCREAMING_SNAKE_CASE 		=model(**snake_case							)[0]

           SCREAMING_SNAKE_CASE 		=torch.tensor(235.7246							)
           SCREAMING_SNAKE_CASE 		=torch.tensor(0.0_256							)
           self.assertTrue(torch.allclose(output.sum()     ,snake_case     ,atol=1e-4							)							)
           self.assertTrue(torch.allclose(output.mean()     ,snake_case     ,atol=1e-4							)							)


def       a__      (					a  ,   a  ,   a  ,   a  ,   a  ,   a  ,   )		->	tuple[list[list[int]], list[list[str]]]:
       A_							:       Optional[Any]	      =				list(a							)
       A_							:       int	      =				list(a							)
       A_							:       Union[str, Any]	      =				len(a							)
       A_							:       Any	      =				len(a							)

       A_							:       Union[str, Any]	      =				[
           [0 for _ in range(len_destination_seq + 1							)] for _ in range(len_source_seq + 1							)
       ]
       A_							:       Optional[int]	      =				[
           ['''0''' for _ in range(len_destination_seq + 1							)] for _ in range(len_source_seq + 1							)
       ]

       for i in range(1  ,   len_source_seq + 1							):
              A_							:       List[Any]	      =				i * delete_cost
              A_							:       List[Any]	      =				f"""D{source_seq[i - 1]:c}"""

       for i in range(1  ,   len_destination_seq + 1							):
              A_							:       Optional[Any]	      =				i * insert_cost
              A_							:       str	      =				f"""I{destination_seq[i - 1]:c}"""

       for i in range(1  ,   len_source_seq + 1							):
              for j in range(1  ,   len_destination_seq + 1							):
                     if source_seq[i - 1] == destination_seq[j - 1]:
                            A_							:       List[Any]	      =				costs[i - 1][j - 1] + copy_cost
                            A_							:       Tuple	      =				f"""C{source_seq[i - 1]:c}"""
                     else:
                            A_							:       Union[str, Any]	      =				costs[i - 1][j - 1] + replace_cost
                            A_							:       Dict	      =				f"""R{source_seq[i - 1]:c}""" + str(destination_seq[j - 1]							)

                     if costs[i - 1][j] + delete_cost < costs[i][j]:
                            A_							:       Dict	      =				costs[i - 1][j] + delete_cost
                            A_							:       Optional[int]	      =				f"""D{source_seq[i - 1]:c}"""

                     if costs[i][j - 1] + insert_cost < costs[i][j]:
                            A_							:       List[Any]	      =				costs[i][j - 1] + insert_cost
                            A_							:       Tuple	      =				f"""I{destination_seq[j - 1]:c}"""

       return costs, ops


def       a__      (					a  ,   a  ,   a							)		->	list[str]:
       if i == 0 and j == 0:
              return []
       else:
              if ops[i][j][0] == "C" or ops[i][j][0] == "R":
                     A_							:       Tuple	      =				assemble_transformation(a  ,   i - 1  ,   j - 1							)
                     seq.append(ops[i][j]							)
                     return seq
              elif ops[i][j][0] == "D":
                     A_							:       Any	      =				assemble_transformation(a  ,   i - 1  ,   a							)
                     seq.append(ops[i][j]							)
                     return seq
              else:
                     A_							:       Tuple	      =				assemble_transformation(a  ,   a  ,   j - 1							)
                     seq.append(ops[i][j]							)
                     return seq


if __name__ == "__main__":
    _lowerCAmelCase  ,			_lowerCAmelCase        =       compute_transform_tables('Python', 'Algorithms', -1, 1, 2, 2)

    _lowerCAmelCase        =       len(operations)
    _lowerCAmelCase        =       len(operations[0])
    _lowerCAmelCase        =       assemble_transformation(operations, m - 1, n - 1)

    _lowerCAmelCase        =       list('Python')
    _lowerCAmelCase        =       0
    _lowerCAmelCase        =       0

    with open('min_cost.txt', 'w') as file:
        for op in sequence:
            print(''.join(string))

            if op[0] == "C":
                file.write('%-16s' % 'Copy %c' % op[1])
                file.write('\t\t\t' + ''.join(string))
                file.write('\r\n')

                cost -= 1
            elif op[0] == "R":
                _lowerCAmelCase        =       op[2]

                file.write('%-16s' % ('Replace %c' % op[1] + ' with ' + str(op[2])))
                file.write('\t\t' + ''.join(string))
                file.write('\r\n')

                cost += 1
            elif op[0] == "D":
                string.pop(i)

                file.write('%-16s' % 'Delete %c' % op[1])
                file.write('\t\t\t' + ''.join(string))
                file.write('\r\n')

                cost += 2
            else:
                string.insert(i, op[1])

                file.write('%-16s' % 'Insert %c' % op[1])
                file.write('\t\t\t' + ''.join(string))
                file.write('\r\n')

                cost += 2

            i += 1

        print(''.join(string))
        print('Cost: ', cost)

        file.write('\r\nMinimum cost: ' + str(cost))


