5 kyu

All that is open must be closed...

700 of 1,516ChristianECooper

Description:

Background

We all know about "balancing parentheses" (plus brackets, braces and chevrons) and even balancing characters that are identical.

Read that last sentence again, I balanced different characters and identical characters twice and you didn't even notice... :)

Kata

Your challenge in this kata is to write a piece of code to validate that a supplied string is balanced.

You must determine if all that is open is then closed, and nothing is closed which is not already open!

You will be given a string to validate, and a second string, where each pair of characters defines an opening and closing sequence that needs balancing.

You may assume that the second string always has an even number of characters.

Example

# In this case '(' opens a section, and ')' closes a section
is_balanced("(Sensei says yes!)", "()")       # => True
is_balanced("(Sensei says no!", "()")         # => False

# In this case '(' and '[' open a section, while ')' and ']' close a section
is_balanced("(Sensei [says] yes!)", "()[]")   # => True
is_balanced("(Sensei [says) no!]", "()[]")    # => False

# In this case a single quote (') both opens and closes a section
is_balanced("Sensei says 'yes'!", "''")       # => True
is_balanced("Sensei say's no!", "''")         # => False
// In this case '(' opens a section, and ')' closes a section
isBalanced("(Sensei says yes!)", "()")       // => True
isBalanced("(Sensei says no!", "()")         // => False

// In this case '(' and '[' open a section, while ')' and ']' close a section
isBalanced("(Sensei [says] yes!)", "()[]")   // => True
isBalanced("(Sensei [says) no!]", "()[]")    // => False

// In this case a single quote (') both opens and closes a section
isBalanced("Sensei says 'yes'!", "''")       // => True
isBalanced("Sensei say's no!", "''")         // => False
// In this case '(' opens a section, and ')' closes a section
Kata.IsBalanced("(Sensei says yes!)", "()");       // => True
Kata.IsBalanced("(Sensei says no!", "()");         // => False

// In this case '(' and '[' open a section, while ')' and ']' close a section
Kata.IsBalanced("(Sensei [says] yes!)", "()[]");   // => True
Kata.IsBalanced("(Sensei [says) no!]", "()[]");    // => False

// In this case a single quote (') both opens and closes a section
Kata.IsBalanced("Sensei says 'yes'!", "''");       // => True
Kata.IsBalanced("Sensei say's no!", "''");         // => False
# In this case '(' opens a section, and ')' closes a section
isBalanced("(Sensei says yes!)", "()")       # => True
isBalanced("(Sensei says no!", "()")         # => False

# In this case '(' and '[' open a section, while ')' and ']' close a section
isBalanced("(Sensei [says] yes!)", "()[]")   # => True
isBalanced("(Sensei [says) no!]", "()[]")    # => False

# In this case a single quote (') both opens and closes a section
isBalanced("Sensei says 'yes'!", "''")       # => True
isBalanced("Sensei say's no!", "''")         # => False
Stacks
Parsing
Algorithms
Regular Expressions

Stats:

CreatedMay 28, 2015
PublishedMay 29, 2015
Warriors Trained5061
Total Skips867
Total Code Submissions18676
Total Times Completed1516
Python Completions700
JavaScript Completions661
CoffeeScript Completions14
C# Completions188
Total Stars158
% of votes with a positive feedback rating91% of 278
Total "Very Satisfied" Votes240
Total "Somewhat Satisfied" Votes28
Total "Not Satisfied" Votes10
Ad
Contributors
  • ChristianECooper Avatar
  • jhoffner Avatar
  • MMMAAANNN Avatar
  • myjinxin2015 Avatar
  • user5036852 Avatar
  • Avanta Avatar
  • hobovsky Avatar
Ad