Skip to content

Instantly share code, notes, and snippets.

@bcjordan
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save bcjordan/83eecd99669baf4e36e9 to your computer and use it in GitHub Desktop.

Select an option

Save bcjordan/83eecd99669baf4e36e9 to your computer and use it in GitHub Desktop.
Balanced delimiter solutions
  • Cameron McCallie in Javascript (gist)

  • Rohit Jamuar in Python (gist)

  • Shahriar Tajbakhsh in Python (gist)

  • Zachary Kanfer in Arc Lisp (blog with code), with a great writeup!

  • Kanat Bekt in Python (gist)

  • Mohammed El-Afifi in C++ (gist)

  • Jesse Sessler in Ruby (gist)

  • Fabian Foerg in Haskell (GitHub)

  • Mike Morton in Javascript (gist)

  • Roger Lamb in Scala (gist

  • Randall Van Why in Haskell (gist)

  • Adam Schoonmaker in Swift (first submission!) (gist), neat stuff like: func correctNextDelimiter(#prevDelim: String, #currentDelim: String) -> Bool {

  • Jean Lauliac in Javascript, the fancy non-; kind (gist)

  • Matthew Eckstein in C# (gist)

  • Péter Ferenczy in Java (gist)

  • Roman Faizullin in Java, (gist), who also included a nice summary of an algorithm:

    This solution for this problem is quite simple — just use a stack. The main algorithm is:

    1. Get a brace
    2. If it is opening brace, put it in the stack
    3. If it is closing brace, compare it with popping element in the stack. They should be the same type, if it’s not - result is false.
    4. If the are more braces, return to 1st step

    But, I foresaw two types of problems:

    • {{}{} (if opening bracket hasn’t got closing bracket, so the 5th step is to check emptiness of stack)
    • {}{}] (if closing bracket hasn’t got opening bracket, so there are no elements in the stack to compare so the program voids by runtime error. So, before popping elements in stack, I check for stack emptiness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment