Advertisement
Danny_Berova

02.Diamond Problem

Jul 31st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 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
  21.                     || endIndex == -1
  22.                     || line.Length == 0
  23.                     || startIndex > endIndex)
  24.                 {
  25.                     break;
  26.                 }
  27.                
  28.                 string currentDiamond = line
  29.                     .Substring(startIndex, endIndex + 1 - startIndex);
  30.                
  31.                 int carat = 0;
  32.                 if (currentDiamond.Contains('<'))
  33.                 {
  34.                     int newStartIndex = currentDiamond.LastIndexOf('<');
  35.                     currentDiamond = currentDiamond.Substring(newStartIndex);
  36.                 }
  37.                
  38.                if (currentDiamond.Length > 3)
  39.                {
  40.                     foreach (var ch in currentDiamond)
  41.                     {
  42.                         if (ch > 47 && ch < 58)
  43.                         {
  44.                             carat += ch - 48;
  45.                         }
  46.                     }
  47.  
  48.                     values.Add(carat);
  49.                 }
  50.  
  51.                 line = line.Substring(endIndex + 1);
  52.             }
  53.  
  54.             if (values.Count == 0)
  55.             {
  56.                 Console.WriteLine("Better luck next time");
  57.             }
  58.             else
  59.             {
  60.                 foreach (var carats in values)
  61.                 {
  62.                     Console.WriteLine($"Found {carats} carat diamond");
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement