Code Monkey home page Code Monkey logo

phase-4-data-structures-balancing-parentheses's Introduction

Balancing Parentheses Lab

Learning Goals

  • Use the Stack implementation we built in the previous lab to solve a problem
  • Write an algorithm to check whether the parentheses in a string are balanced

Introduction

In this lab, you'll use the Stack class you built in the previous lab to write an algorithm that takes a string as input and checks to see whether the parentheses in the string are balanced.

A single pair of parentheses is defined as balanced if it consists of an opening parenthesis followed by a matching closing parenthesis. A string containing some number of ( and ) characters will be considered balanced if it includes only balanced pairs:

  • Balanced: (), (()())
  • Not balanced: ())(), )

You will be using the Stack implementation we developed in an earlier lesson to help you solve this lab. We have included the code in lib/stack.rb. We have also required that file in lib/balancing_parentheses.rb, where you will be coding your solution.

Instructions

Write a balancing_parentheses method in the lib/balancing_parentheses.rb file that:

  1. Takes as input a string consisting of some number of ( and ) characters
  2. Creates an instance of the Stack class
  3. Using the stack, checks the string to determine whether the parentheses inside it are balanced
  4. Returns the number of additional parentheses that would need to be added to the string to make it balanced

Here are some examples of the expected return value for your method for various inputs:

Input: `()`
Output: 0

Input: `())()`
Output: 1

Input: `(()()(()`
Output: 2

Note: There are a number of ways to solve this problem, but for this lab you need to use a stack (specifically, an instance of our Stack class) in the solution.

phase-4-data-structures-balancing-parentheses's People

Contributors

ihollander avatar lizbur10 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.