Advertisement
YORDAN2347

KaminoFactory

Jan 21st, 2021
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace KaminoFactory
  5. {
  6.     class Program
  7.     {
  8.         static (int, int) SubSequenceAndStartIndex(int[] dna)
  9.         {
  10.             int leftMost = 0;
  11.             int subSequenceMaxInArr = 0;
  12.             for (int i = 0; i < dna.Length; i++)
  13.             {
  14.                 if(dna[i] == 1)
  15.                 {
  16.                     int subSequnece = 1;
  17.                     for (int j = i + 1; j < dna.Length; j++)
  18.                     {
  19.                         if (dna[i] == dna[j])
  20.                             subSequnece++;
  21.                         else
  22.                             break;
  23.                     }
  24.                     if (subSequnece > subSequenceMaxInArr)
  25.                     {
  26.                         subSequenceMaxInArr = subSequnece;
  27.                         leftMost = i;
  28.                     }
  29.                 }
  30.             }
  31.  
  32.             return (leftMost, subSequenceMaxInArr);
  33.         }
  34.  
  35.         private static int DnaSum(int[] dna)
  36.         {
  37.             int counter = 0;
  38.             for (int i = 0; i < dna.Length; i++)
  39.             {
  40.                 if(dna[i] == 1)
  41.                 {
  42.                     counter++;
  43.                 }
  44.             }
  45.             return counter;
  46.         }
  47.  
  48.         // 2.1. split by "!" (one or several).
  49.         private static int[] readDna(string input)
  50.         {
  51.             int[] dna = input.Split("!", StringSplitOptions.RemoveEmptyEntries)
  52.                     .Select(int.Parse)
  53.                     .ToArray();
  54.             return dna;
  55.         }
  56.  
  57.         static void Main(string[] args)
  58.         {
  59.             // 1. read Dna Lenght
  60.             int dnaLenght = int.Parse(Console.ReadLine());
  61.  
  62.             // 2. make vars for: best subSequence, bestCounter, best index, BestSequneceSum, bestDNA
  63.             int bestSubSequence = 0;
  64.             int bestStartIndex = 0;
  65.             int bestSum = 0;
  66.             int bestSample = 0;
  67.             int currentSample = 0;
  68.             int[] bestDna = new int[dnaLenght];
  69.  
  70.             // 3. While(input != Clone Them) ---> read DNA Sequence
  71.             string input = "";
  72.             while((input = Console.ReadLine()) != "Clone them!")
  73.             {                
  74.                 int[] dna = readDna(input);
  75.                 currentSample++;
  76.  
  77.                 // 3.1. Define subSequence, startIndex, dnaSum
  78.                 int startIndex = SubSequenceAndStartIndex(dna).Item1;
  79.                 int subSequence = SubSequenceAndStartIndex(dna).Item2;
  80.                 int dnaSum = DnaSum(dna);
  81.                
  82.  
  83.                 // 3.2. Compare Sequnece by the above vars
  84.                 if ((subSequence > bestSubSequence) ||
  85.                     (subSequence == bestSubSequence && startIndex < bestStartIndex) ||
  86.                     (subSequence == bestSubSequence && startIndex == bestStartIndex  && dnaSum > bestSum))
  87.                 {
  88.                     bestSubSequence = subSequence;
  89.                     bestStartIndex = startIndex;
  90.                     bestSum = dnaSum;
  91.                     bestDna = dna;
  92.                     bestSample = currentSample;
  93.                 }
  94.             }
  95.             // 3. Print
  96.             Console.WriteLine($"Best DNA sample {bestSample} with sum: {bestSum}.");
  97.             printArray(bestDna);
  98.         }
  99.  
  100.         private static void printArray(int[] bestDna)
  101.         {
  102.             for (int i = 0; i < bestDna.Length; i++)
  103.                 Console.Write($"{bestDna[i]} ");
  104.         }
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement