Advertisement
Miseryk

MercadoLibre Test

Jul 25th, 2020
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.74 KB | None | 0 0
  1. //https://drive.google.com/file/d/1agozx4V82KeY05FmCmyaMTWRezFYSl3M/view?usp=sharing
  2. //Time: 1h
  3. //New: ValidLetters, Consecutives, MaxRows, MaxColumns all customizable.
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8.  
  9. enum eDirections
  10. {
  11.     dHorizontal = 0,
  12.     dVertical,
  13.     dRightDiagonal,
  14.     dLeftDiagonal
  15. };
  16.  
  17. struct tPoints
  18. {
  19.     eDirections Direction;
  20.     char Letter;
  21.     size_t B_R; //Begin Row
  22.     size_t B_C; //Begin Column
  23.     size_t E_R; //End Row
  24.     size_t E_C; //End Column
  25. };
  26.  
  27. std::vector<tPoints> AllFoundPoints;
  28.  
  29. char NoSeqSign = '*';
  30. std::string ValidLetters = "ATCG";
  31. size_t Consecutives = 4;
  32. size_t MaxRows = 6;
  33. size_t MaxColumns = 6;
  34.  
  35. bool IsValid(std::string Sequence)
  36. {
  37.     for (size_t i = 0; i < Sequence.size(); i++)
  38.     {
  39.         for (size_t i2 = 0; i2 < ValidLetters.size(); i2++)
  40.         {
  41.             if (Sequence[i] == ValidLetters[i2])
  42.             {
  43.                 break;
  44.             }
  45.             else
  46.             {
  47.                 if (i2 == ValidLetters.size() - 1) return false;
  48.             }
  49.         }
  50.     }
  51.    
  52.     return true;
  53. }
  54.  
  55. void ShowFoundMatrix(std::vector<std::string> ADN)
  56. {
  57.     for (size_t i = 0; i < ADN.size(); i++)
  58.     {
  59.         for (size_t i2 = 0; i2 < ADN[i].size(); i2++)
  60.         {
  61.             ADN[i][i2] = NoSeqSign;
  62.         }
  63.     }
  64.    
  65.     for (size_t i = 0; i < AllFoundPoints.size(); i++)
  66.     {
  67.         for (size_t i2 = 0; i2 < Consecutives; i2++)
  68.         {
  69.             switch(AllFoundPoints[i].Direction)
  70.             {
  71.                 case eDirections::dHorizontal:
  72.                     ADN[AllFoundPoints[i].B_R][AllFoundPoints[i].B_C + i2] = AllFoundPoints[i].Letter;
  73.                    
  74.                     break;
  75.                 case eDirections::dVertical:
  76.                     ADN[AllFoundPoints[i].B_R + i2][AllFoundPoints[i].B_C] = AllFoundPoints[i].Letter;
  77.                    
  78.                     break;
  79.                 case eDirections::dRightDiagonal:
  80.                     ADN[AllFoundPoints[i].B_R + i2][AllFoundPoints[i].B_C + i2] = AllFoundPoints[i].Letter;
  81.                    
  82.                     break;
  83.                 case eDirections::dLeftDiagonal:
  84.                     ADN[AllFoundPoints[i].B_R + i2][AllFoundPoints[i].B_C - i2] = AllFoundPoints[i].Letter;
  85.                    
  86.                     break;
  87.             }
  88.         }
  89.     }
  90.    
  91.     for (size_t i = 0; i < ADN.size(); i++)
  92.     {
  93.         for (size_t i2 = 0; i2 < ADN[i].size(); i2++)
  94.         {
  95.             std::cout << (!i2 ? "" : "\t\t") << ADN[i][i2] << (i2 < ADN[i].size() - 1 ? "" : "\t\t");
  96.         }
  97.        
  98.         std::cout << std::endl;
  99.     }
  100. }
  101.  
  102. bool IsMutant(std::vector<std::string> ADN)
  103. {
  104.     int Sequences = 0;
  105.     bool AddHorizontal = false;
  106.     bool AddVertical = false;
  107.     bool RightDiagonal = false;
  108.     bool LeftDiagonal = false;
  109.     tPoints FoundPoint;
  110.    
  111.     if (ADN.size() != MaxRows)
  112.     {
  113.         std::cout << "ERROR de matriz: no tiene " << MaxRows << " filas." << std::endl;
  114.        
  115.         return false;
  116.     }
  117.    
  118.     for (size_t i = 0; i < ADN.size(); i++)
  119.     {
  120.         if (ADN[i].size() != MaxColumns)
  121.         {
  122.             std::cout << "ERROR de matriz: la fila " << (i + 1) <<  " no tiene " << MaxColumns << " columnas." << std::endl;
  123.            
  124.             return false;
  125.         }
  126.        
  127.         if (!IsValid(ADN[i]))
  128.         {
  129.             std::cout << "ERROR de secuencia: la fila " << (i + 1) <<  " no tiene una secuencia valida." << std::endl;
  130.            
  131.             return false;
  132.         }
  133.        
  134.         for (size_t i2 = 0; i2 < ADN[i].size(); i2++)
  135.         {
  136.             FoundPoint.Letter = ADN[i][i2];
  137.             FoundPoint.B_R = i;
  138.             FoundPoint.B_C = i2;
  139.            
  140.             //Horizontal
  141.             if (i2 + (Consecutives - 1) < ADN[i].size())
  142.             {
  143.                 if (ADN[i][i2] == ADN[i][i2 + (Consecutives - 1)])
  144.                 {
  145.                     FoundPoint.E_R = i;
  146.                     FoundPoint.E_C = i2 + (Consecutives - 1);
  147.                    
  148.                     AddHorizontal = true;
  149.                    
  150.                     for (size_t i3 = 1; i3 < (Consecutives - 1); i3++)
  151.                     {
  152.                         if (ADN[i][i2] != ADN[i][i2 + i3])
  153.                         {
  154.                             AddHorizontal = false;
  155.                             break;
  156.                         }
  157.                     }
  158.                    
  159.                     if (AddHorizontal)
  160.                     {
  161.                         FoundPoint.Direction = eDirections::dHorizontal;
  162.                        
  163.                         AllFoundPoints.push_back(FoundPoint);
  164.                         std::cout << "Secuencia Horizontal encontrada desde " << FoundPoint.B_R << "," << FoundPoint.B_C << "->" << FoundPoint.E_R << "," << FoundPoint.E_C << std::endl;
  165.                        
  166.                         AddHorizontal = false;
  167.                        
  168.                         Sequences++;
  169.                     }
  170.                 }
  171.             }
  172.            
  173.             //Vertical
  174.             if (i + (Consecutives - 1) < ADN.size())
  175.             {
  176.                 if (ADN[i][i2] == ADN[i + (Consecutives - 1)][i2])
  177.                 {
  178.                     FoundPoint.E_R = i + (Consecutives - 1);
  179.                     FoundPoint.E_C = i2;
  180.                    
  181.                     AddVertical = true;
  182.                    
  183.                     for (size_t i3 = 1; i3 < (Consecutives - 1); i3++)
  184.                     {
  185.                         if (ADN[i][i2] != ADN[i + i3][i2])
  186.                         {
  187.                             AddVertical = false;
  188.                             break;
  189.                         }
  190.                     }
  191.                    
  192.                     if (AddVertical)
  193.                     {
  194.                         FoundPoint.Direction = eDirections::dVertical;
  195.                        
  196.                         AllFoundPoints.push_back(FoundPoint);
  197.                         std::cout << "Secuencia Vertical encontrada desde " << FoundPoint.B_R << "," << FoundPoint.B_C << "->" << FoundPoint.E_R << "," << FoundPoint.E_C << std::endl;
  198.                        
  199.                         AddVertical = false;
  200.                        
  201.                         Sequences++;
  202.                     }
  203.                 }
  204.             }
  205.            
  206.             //Diagonal derecha
  207.             if (i2 + (Consecutives - 1) < ADN[i].size() && i + (Consecutives - 1) < ADN.size())
  208.             {
  209.                 if (ADN[i][i2] == ADN[i + (Consecutives - 1)][i2 + (Consecutives - 1)])
  210.                 {
  211.                     FoundPoint.E_R = i + (Consecutives - 1);
  212.                     FoundPoint.E_C = i2 + (Consecutives - 1);
  213.                    
  214.                     RightDiagonal = true;
  215.                    
  216.                     for (size_t i3 = 1; i3 < (Consecutives - 1); i3++)
  217.                     {
  218.                         if (ADN[i][i2] != ADN[i + i3][i2 + i3])
  219.                         {
  220.                             RightDiagonal = false;
  221.                             break;
  222.                         }
  223.                     }
  224.                    
  225.                     if (RightDiagonal)
  226.                     {
  227.                         FoundPoint.Direction = eDirections::dRightDiagonal;
  228.                        
  229.                         AllFoundPoints.push_back(FoundPoint);
  230.                         std::cout << "Secuencia Diagonal Derecha encontrada desde " << FoundPoint.B_R << "," << FoundPoint.B_C << "->" << FoundPoint.E_R << "," << FoundPoint.E_C << std::endl;
  231.                        
  232.                         RightDiagonal = false;
  233.                        
  234.                         Sequences++;
  235.                     }
  236.                 }
  237.             }
  238.            
  239.             //Diagonal izquierda
  240.             if ((int)i2 - (Consecutives - 1) >= 0 && (int)i + (Consecutives - 1) < ADN.size())
  241.             {
  242.                 if (ADN[i][i2] == ADN[i + (Consecutives - 1)][i2 - (Consecutives - 1)])
  243.                 {
  244.                     FoundPoint.E_R = i + (Consecutives - 1);
  245.                     FoundPoint.E_C = i2 - (Consecutives - 1);
  246.                    
  247.                     LeftDiagonal = true;
  248.                    
  249.                     for (size_t i3 = 1; i3 < (Consecutives - 1); i3++)
  250.                     {
  251.                         if (ADN[i][i2] != ADN[i + i3][i2 - i3])
  252.                         {
  253.                             LeftDiagonal = false;
  254.                             break;
  255.                         }
  256.                     }
  257.                    
  258.                     if (LeftDiagonal)
  259.                     {
  260.                         FoundPoint.Direction = eDirections::dLeftDiagonal;
  261.                        
  262.                         AllFoundPoints.push_back(FoundPoint);
  263.                         std::cout << "Secuencia Diagonal Izquierda encontrada desde " << FoundPoint.B_R << "," << FoundPoint.B_C << "->" << FoundPoint.E_R << "," << FoundPoint.E_C << std::endl;
  264.                        
  265.                         LeftDiagonal = false;
  266.                        
  267.                         Sequences++;
  268.                     }
  269.                 }
  270.             }
  271.         }
  272.     }
  273.    
  274.     return (Sequences > 1);
  275. }
  276.  
  277. int main()
  278. {
  279.     if (Consecutives < 2)
  280.     {
  281.         std::cout << "Tiene que haber una mínima consecutividad de 2." << std::endl;
  282.        
  283.         return 0;
  284.     }
  285.    
  286.     std::vector<std::string> Matrix;
  287.    
  288.     Matrix.push_back("ATGCGA");
  289.     Matrix.push_back("CAGTGC");
  290.     Matrix.push_back("TTATGT");
  291.     Matrix.push_back("AGAAGG");
  292.     Matrix.push_back("CCCCTA");
  293.     Matrix.push_back("TCACTG");
  294.    
  295.     std::cout << (IsMutant(Matrix) ? "ES MUTANTE" : "NO ES MUTANTE") << std::endl;
  296.  
  297.     ShowFoundMatrix(Matrix);
  298.    
  299.     return 0;
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement