Advertisement
Danny_Berova

02.Diamond Problem

Jul 30th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 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.                 int carat = 0;
  31.                 if (currentDiamond.Length > 2)
  32.                 {
  33.                     foreach (var ch in currentDiamond)
  34.                     {
  35.                         if (ch > 47 && ch < 58)
  36.                         {
  37.                             carat += ch - 48;
  38.                         }
  39.                     }
  40.  
  41.                     values.Add(carat);
  42.                 }
  43.  
  44.                 line = line.Substring(endIndex + 1);
  45.             }
  46.  
  47.             if (values.Count == 0)
  48.             {
  49.                 Console.WriteLine("Better luck next time");
  50.             }
  51.             else
  52.             {
  53.                 foreach (var carats in values)
  54.                 {
  55.                     Console.WriteLine($"Found {carats} carat diamond");
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement