Giter Club home page Giter Club logo

30-reinforcement-syntax's Introduction

The code

Copy this code into a Ruby file, try running it, and then read the source code to see what it's doing.

def check_syntax(str)
  openables = {
    "(" => ")",
    "[" => "]",
    "{" => "}"
  }
  open = []
  opening_chars = openables.keys
  closing_chars = openables.values

  str.each_char do |char|

    if opening_chars.include?(char) # opening bracket

      open << char

    elsif closing_chars.include?(char) # closing bracket

      required_char = openables[open.last]

      if char == required_char # it's the right kind of closing bracket

        open.pop

      else 

        if open.any? # it's the wrong kind of closing bracket
          puts "* You have a syntax error: there is an unexpected #{char}"
        else # there's nothing to close
          puts "* You have a syntax error: there is an unexpected #{char} where there is nothing to close."
        end

        return false

      end

    end
  end

  if open.any?
    required_char = openables[open.last]
    puts "* You have a syntax error: the string ended without a closing #{required_char}"
  end

  return open.empty? 
end

puts check_syntax("(this)[] is some text")
puts "*****"
puts check_syntax("(this)] is some text")
puts "*****"
puts check_syntax("[(this] is some text")
puts "*****"
puts check_syntax("[this][ is some text")
puts "*****"
puts check_syntax("[this] is some text")

The output should currently look like this:

true
*****
* You have a syntax error: there is an unexpected ] where there is nothing to close.
false
*****
* You have a syntax error: there is an unexpected ]
false
*****
* You have a syntax error: the string ended without a closing ]
false
*****
true

Exercise 1

Modify the code to improve the error messages it gives, specifying what character was missing. This should be your new output:

puts check_syntax("(this)[] is some text")
true
puts check_syntax("(this)] is some text")
* You have a syntax error: there is an unexpected ] where there is nothing to close.
false
New, improved error message:

puts check_syntax("[(this] is some text")
* You have a syntax error: there is an unexpected ] where there should be a )
false
puts check_syntax("[this][ is some text")
* You have a syntax error: the string ended without a closing ]
false

Exercise 2

Modify the code to make it work for angled brackets (< and >) as well.

The new output for these strings containing angled brackets should look like this:

puts check_syntax("<html> (this)[] is some text</html>")
true
puts check_syntax("<html> (this)] is some text</html>")
* You have a syntax error: there is an unexpected ] where there is nothing to close.
false
puts check_syntax("<html> [(this] is some text</html>")
* You have a syntax error: there is an unexpected ] where there should be a )
false
puts check_syntax("<html> [this][ is some text</html")
* You have a syntax error: the string ended without a closing ]
false
puts check_syntax("<html> [this] is some text</html")
* You have a syntax error: the string ended without a closing >
false

30-reinforcement-syntax's People

Contributors

lanphantastic avatar

Watchers

James Cloos 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.