Advertisement
darrellp

Untitled

Dec 22nd, 2021
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1.  
  2. enum State
  3. {
  4.     Skip,
  5.     Num
  6. }
  7.  
  8. void Main()
  9. {
  10.     CheckNumbers("Testing").Dump("Should be true");
  11.     CheckNumbers("11 day of sunshine, 22 days of rain, 34 days of gray sky, then the days number 45").Dump("Should be true");
  12.     CheckNumbers("11 day of sunshine, 22 days of rain, 34 days of gray sky, then the days number 33").Dump("Should be false");
  13.     CheckNumbers("11 day of sunshine, 22 days of rain, 11 days of gray sky, then the days number 45").Dump("Should be false");
  14. }
  15.  
  16. bool CheckNumbers(string input)
  17. {
  18.     var curState = State.Skip;
  19.     var value = 0;
  20.     var prevValue = -1;
  21.  
  22.     foreach (var ch in input)
  23.     {
  24.         switch (curState)
  25.         {
  26.             case State.Skip:
  27.                 if (char.IsDigit(ch))
  28.                 {
  29.                     value = ch - '0';
  30.                     curState = State.Num;
  31.                 }
  32.                 break;
  33.  
  34.             case State.Num:
  35.                 if (char.IsDigit(ch))
  36.                 {
  37.                     value = value * 10 + ch - '0';
  38.                 }
  39.                 else
  40.                 {
  41.                     if (value <= prevValue)
  42.                     {
  43.                         return false;
  44.                     }
  45.                     curState = State.Skip;
  46.                     prevValue = value;
  47.                 }
  48.                 break;
  49.         }
  50.     }
  51.     return prevValue == -1 || curState == State.Skip || value > prevValue;
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement