Advertisement
Danny_Berova

02.Diamond Problem

Aug 1st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1.  
  2. namespace _02.DiamondProblem
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             string line = Console.ReadLine();
  13.             List<int> values = new List<int>();
  14.  
  15.             while (true)
  16.             {
  17.                 int startIndex = line.IndexOf('<');
  18.                 int endIndex = line.IndexOf('>');
  19.  
  20.                 if (startIndex == -1 || endIndex == -1 || line.Length == 0)
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 if (startIndex > endIndex)
  26.                 {
  27.                     line = line.Substring(endIndex + 1);
  28.                     startIndex = line.IndexOf('<');
  29.                     endIndex = line.IndexOf('>');
  30.                     if (startIndex == -1 || endIndex == -1)
  31.                     {
  32.                         break;
  33.                     }
  34.                 }
  35.  
  36.                 string currentDiamond = line
  37.                     .Substring(startIndex, endIndex + 1 - startIndex);
  38.  
  39.                 int carat = 0;
  40.                 if (currentDiamond.Contains('<'))
  41.                 {
  42.                     int newStartIndex = currentDiamond.LastIndexOf('<');
  43.                     currentDiamond = currentDiamond.Substring(newStartIndex);
  44.                 }
  45.  
  46.                 if (currentDiamond.Length > 0)
  47.                 {
  48.                     foreach (var ch in currentDiamond)
  49.                     {
  50.                         if (ch > 47 && ch < 58)
  51.                         {
  52.                             carat += ch - 48;
  53.                         }
  54.                     }
  55.  
  56.                     values.Add(carat);
  57.                 }
  58.  
  59.                 line = line.Substring(endIndex + 1);
  60.             }
  61.  
  62.             if (values.Count == 0)
  63.             {
  64.                 Console.WriteLine("Better luck next time");
  65.             }
  66.             else
  67.             {
  68.                 foreach (var carats in values)
  69.                 {
  70.                     Console.WriteLine($"Found {carats} carat diamond");
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement