added balanceMatch moustache
This commit is contained in:
		| @@ -14,7 +14,23 @@ def balanceMatch(chars: List[Char]): Boolean = { | ||||
|   checkStep(chars, 0) | ||||
| } | ||||
|  | ||||
| balanceMatch("(if (x == 0) then max (1, x))".toList) | ||||
| balanceMatch("I told him (that it's not (yet) done). (But he wasn't listening)".toList) | ||||
| balanceMatch(")".toList) | ||||
| balanceMatch("())()".toList) | ||||
| def balanceMatch2(chars: List[Char]): Boolean = { | ||||
|   @tailrec | ||||
|   def checkStep(chars: List[Char], n: Int): Boolean = { | ||||
|     chars match { | ||||
|       case Nil => n == 0 | ||||
|       case _ if n < 0 => false | ||||
|       case '('::rest => checkStep(rest, n + 1) | ||||
|       case ')'::rest => checkStep(rest, n - 1) | ||||
|       case _::rest => checkStep(rest, n) | ||||
|     } | ||||
|   } | ||||
|   checkStep(chars, 0) | ||||
| } | ||||
|  | ||||
| balanceMatch2("(if (x == 0) then max (1, x))".toList) | ||||
| balanceMatch2("I told him (that it's not (yet) done). (But he wasn't listening)".toList) | ||||
| balanceMatch2(")".toList) | ||||
| balanceMatch2("())()".toList) | ||||
| balanceMatch2("())(()".toList) | ||||
| balanceMatch2("(".toList) | ||||
		Reference in New Issue
	
	Block a user