Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum State
- {
- Skip,
- Num
- }
- void Main()
- {
- CheckNumbers("Testing").Dump("Should be true");
- CheckNumbers("11 day of sunshine, 22 days of rain, 34 days of gray sky, then the days number 45").Dump("Should be true");
- CheckNumbers("11 day of sunshine, 22 days of rain, 34 days of gray sky, then the days number 33").Dump("Should be false");
- CheckNumbers("11 day of sunshine, 22 days of rain, 11 days of gray sky, then the days number 45").Dump("Should be false");
- }
- bool CheckNumbers(string input)
- {
- var curState = State.Skip;
- var value = 0;
- var prevValue = -1;
- foreach (var ch in input)
- {
- switch (curState)
- {
- case State.Skip:
- if (char.IsDigit(ch))
- {
- value = ch - '0';
- curState = State.Num;
- }
- break;
- case State.Num:
- if (char.IsDigit(ch))
- {
- value = value * 10 + ch - '0';
- }
- else
- {
- if (value <= prevValue)
- {
- return false;
- }
- curState = State.Skip;
- prevValue = value;
- }
- break;
- }
- }
- return prevValue == -1 || curState == State.Skip || value > prevValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement