Advertisement
elena1234

KaminoFactory-withDictionaryAndRegex

Nov 27th, 2020 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6. namespace KaminoFactory
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int arrayLength = int.Parse(Console.ReadLine());
  13.             string input = Console.ReadLine();
  14.             int[] DNKArray = new int[arrayLength];
  15.             var dictDNKWithMaxSequenceLengthAndIndexAndSum = new Dictionary<string, List<int>>();
  16.             int DNKCounter = 0;
  17.  
  18.             while (input != "Clone them!")
  19.             {
  20.                 DNKArray = input.Split("!", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  21.                 string DNK = string.Join("", DNKArray);
  22.                 int sumDNK = DNKArray.Sum();
  23.                 DNKCounter++;
  24.  
  25.                 Regex regexForSeqiencesWith1 = new Regex(@"1+");
  26.                 MatchCollection matchEverySequencesWith1 = regexForSeqiencesWith1.Matches(DNK);
  27.                 int maxSequencesLength = 0;
  28.                 int startIndexMaxSequence = 0;
  29.                 foreach (Match sequence in matchEverySequencesWith1)// to find the maxSequence and startIndexMaxSequence
  30.                 {
  31.                     int sequenceLength = sequence.Length;
  32.                     if (sequenceLength > maxSequencesLength)
  33.                     {
  34.                         maxSequencesLength = sequenceLength;
  35.                         startIndexMaxSequence = sequence.Index;
  36.                     }
  37.                 }
  38.  
  39.                 if (dictDNKWithMaxSequenceLengthAndIndexAndSum.ContainsKey(DNK) == false)//save all DNK with maxSequencesLength, startIndexMaxSequence, sumDNK, DNKCounter
  40.                 {
  41.                     dictDNKWithMaxSequenceLengthAndIndexAndSum[DNK] = new List<int>();
  42.                     dictDNKWithMaxSequenceLengthAndIndexAndSum[DNK].Add(maxSequencesLength);//0
  43.                     dictDNKWithMaxSequenceLengthAndIndexAndSum[DNK].Add(startIndexMaxSequence);//1
  44.                     dictDNKWithMaxSequenceLengthAndIndexAndSum[DNK].Add(sumDNK);//2
  45.                     dictDNKWithMaxSequenceLengthAndIndexAndSum[DNK].Add(DNKCounter);//3
  46.                 }
  47.  
  48.                 input = Console.ReadLine();
  49.             }
  50.  
  51.             SortedAndPrintTheResult(dictDNKWithMaxSequenceLengthAndIndexAndSum);
  52.         }
  53.  
  54.         private static void SortedAndPrintTheResult(Dictionary<string, List<int>> dictDNKWithMaxSequenceLengthAndIndexAndSum)
  55.         {
  56.             var sortedDictionary = dictDNKWithMaxSequenceLengthAndIndexAndSum.OrderByDescending(x => x.Value[0]).ThenBy(x => x.Value[1]).ThenByDescending(x => x.Value[2]);
  57.             var bestDNK = sortedDictionary.Take(1).Select(x => x.Key).First();
  58.             int bestDNKCounter = sortedDictionary.Take(1).Select(x => x.Value[3]).First();
  59.             int bestDNKSum = sortedDictionary.Take(1).Select(x => x.Value[2]).First();
  60.             Console.WriteLine($"Best DNA sample {bestDNKCounter} with sum: {bestDNKSum}.");
  61.             Console.WriteLine(string.Join(" ", bestDNK.ToCharArray()));
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement