Advertisement
vlad0

Sample Exam - Genome Decoder

Feb 3rd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace _01.GenomeDecoder
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] dimensions = Console.ReadLine().Split(' ');
  14.             int line = int.Parse(dimensions[0]);
  15.             int space = int.Parse(dimensions[1]);
  16.             string genome = Console.ReadLine();
  17.             string pattern = @"(\d*)(\w)";
  18.             List<int> count = new List<int>();
  19.             List<string> letter = new List<string>();
  20.             bool finished = false;
  21.  
  22.             foreach (Match match in Regex.Matches(genome,pattern))
  23.             {
  24.                 int countLetter=0;
  25.                 if (!int.TryParse(match.Groups[1].Value, out countLetter))
  26.                 {
  27.                     countLetter = 1;
  28.                 }
  29.                 string currentLetter = match.Groups[2].Value;
  30.                 count.Add(countLetter);
  31.                 letter.Add(currentLetter);
  32.                
  33.             }
  34.        
  35.             int printedLetter = 0;
  36.             int lineCount = 1;
  37.             int totalSum = count.Sum();
  38.             int totalLines = totalSum / line;
  39.             if (totalSum%line != 0)
  40.             {
  41.                 totalLines++;
  42.             }
  43.             int padding = totalLines.ToString().Length;
  44.             string realPadding = "{0," + padding + "} ";
  45.             StringBuilder output = new StringBuilder();
  46.             string format = String.Format(realPadding, lineCount);
  47.            
  48.             output.Append(format);
  49.             realPadding = "\n" + realPadding;
  50.             lineCount++;
  51.             int length = count.Count;
  52.  
  53.             for (int i = 0; i < length; i++)
  54.             {
  55.                 int letterLength = count[i];
  56.                 for (int j = 0; j < letterLength; j++)
  57.                 {
  58.                     output.Append(letter[i]);
  59.                     printedLetter++;
  60.                     if (i == count.Count - 1 && j == count[i] - 1)
  61.                     {
  62.                         finished = true;
  63.  
  64.                     }
  65.                     if(printedLetter%line == 0 && finished == false)
  66.                     {                    
  67.                             output.Append(String.Format(realPadding, lineCount));
  68.                      
  69.                         lineCount++;
  70.                         printedLetter = 0;
  71.                     }
  72.                     else if (printedLetter % space == 0 && finished == false)
  73.                     {
  74.                         output.Append(' ');
  75.                     }
  76.                    
  77.                 }
  78.                
  79.             }
  80.             Console.WriteLine(output);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement