Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.48 KB | None | 0 0
  1. /**
  2.    * Exercise 2
  3.    */
  4.     def balance(chars: List[Char]): Boolean = {
  5.       def innerBalance(chars: List[Char], counter: Int) : Boolean = {
  6.         if (counter < 0) false
  7.         else if (chars.isEmpty) counter == 0
  8.         else {
  9.           if (chars.head == '(') innerBalance(chars.tail, counter + 1)
  10.           else if (chars.head == ')')  innerBalance(chars.tail, counter - 1)
  11.           else innerBalance(chars.tail, counter)
  12.         }
  13.       }
  14.       innerBalance(chars, 0)
  15.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement