Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace KaminoFactory
- {
- class Program
- {
- static (int, int) SubSequenceAndStartIndex(int[] dna)
- {
- int leftMost = 0;
- int subSequenceMaxInArr = 0;
- for (int i = 0; i < dna.Length; i++)
- {
- if(dna[i] == 1)
- {
- int subSequnece = 1;
- for (int j = i + 1; j < dna.Length; j++)
- {
- if (dna[i] == dna[j])
- subSequnece++;
- else
- break;
- }
- if (subSequnece > subSequenceMaxInArr)
- {
- subSequenceMaxInArr = subSequnece;
- leftMost = i;
- }
- }
- }
- return (leftMost, subSequenceMaxInArr);
- }
- private static int DnaSum(int[] dna)
- {
- int counter = 0;
- for (int i = 0; i < dna.Length; i++)
- {
- if(dna[i] == 1)
- {
- counter++;
- }
- }
- return counter;
- }
- // 2.1. split by "!" (one or several).
- private static int[] readDna(string input)
- {
- int[] dna = input.Split("!", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- return dna;
- }
- static void Main(string[] args)
- {
- // 1. read Dna Lenght
- int dnaLenght = int.Parse(Console.ReadLine());
- // 2. make vars for: best subSequence, bestCounter, best index, BestSequneceSum, bestDNA
- int bestSubSequence = 0;
- int bestStartIndex = 0;
- int bestSum = 0;
- int bestSample = 0;
- int currentSample = 0;
- int[] bestDna = new int[dnaLenght];
- // 3. While(input != Clone Them) ---> read DNA Sequence
- string input = "";
- while((input = Console.ReadLine()) != "Clone them!")
- {
- int[] dna = readDna(input);
- currentSample++;
- // 3.1. Define subSequence, startIndex, dnaSum
- int startIndex = SubSequenceAndStartIndex(dna).Item1;
- int subSequence = SubSequenceAndStartIndex(dna).Item2;
- int dnaSum = DnaSum(dna);
- // 3.2. Compare Sequnece by the above vars
- if ((subSequence > bestSubSequence) ||
- (subSequence == bestSubSequence && startIndex < bestStartIndex) ||
- (subSequence == bestSubSequence && startIndex == bestStartIndex && dnaSum > bestSum))
- {
- bestSubSequence = subSequence;
- bestStartIndex = startIndex;
- bestSum = dnaSum;
- bestDna = dna;
- bestSample = currentSample;
- }
- }
- // 3. Print
- Console.WriteLine($"Best DNA sample {bestSample} with sum: {bestSum}.");
- printArray(bestDna);
- }
- private static void printArray(int[] bestDna)
- {
- for (int i = 0; i < bestDna.Length; i++)
- Console.Write($"{bestDna[i]} ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement