Skip to content

Instantly share code, notes, and snippets.

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

  • Save beck03076/b91aa2bba84113729dad to your computer and use it in GitHub Desktop.

Select an option

Save beck03076/b91aa2bba84113729dad to your computer and use it in GitHub Desktop.
This ruby program parses the given string of brackets and adds the missing brackets(opening/closing only "[" and "]" [changeble in the global variable to anything])
#========= Brackets Parser ==================
#
#---How to execute---
# Store the class Parse into a parse.rb file
# Execute by typing $ ruby parse.rb
# It will prompt for a value, enter the brackets, for example, "][]][[][[]][[["
# Press Enter, you will see the result
#--------------------
#
#---Logic Explained---
# Pre: Split the string of brackets into an Array
# 1 .First element is checked if it is a opening or closing bracket. If its a closing bracket,
# add it to the @notes and set @drop = 1 else @drop = 0
# 2. Iterate the array after the dropping the first element if @drop is 1 and push the opening bracket
# into @stack and pop it if you encounter an closing bracket. Note the pos where there is an unresolved
# closing bracket in between
# 3. After iteration, if the @stack size is > 0 , add those many closing brackets to the end
# 4. After iteration, if insert a opening bracket at the positions notes in @notes
# 5. Print the final array after joining it as a string
#---------------------
#
#---Checks Pending/forecasted---
# 1. If the string is empty
# 2. If the string has only brackets
#-------------------------------
#
#---Changed Expected---
# 1. The opening and closing brackets can be set to "(", ")" or "{" , "}".
# Just alter the global variable in the constructor. Currently, its "[", "]"
#----------------------
#
#---Test Cases Pending---
#------------------------
#============================================
class Parse
# the constructor of the class that initializes the instance variables
# (also splits the input string as an array )
def initialize(str)
@stack = []
@notes = []
@drop = 0
@arr = str.split('')
$open = "["
$close = "]"
end
# this method deals with the first element of the array
def check_first
@arr.first == $close ? (@notes << 0 ; @drop = 1) : nil
end
# this method iterates through every element(drops first if check_first sets @drop = 1)
# mainly, it pushes an opening bracket to a stack and pops one when it encouters a closing bracket
# also, notes the position of an unresolved closing bracket in between
def iterate
@arr.drop(@drop).each_with_index do |i,pos|
if i == $open
@stack.push(i)
elsif i == $close
if @stack.size != 0
@stack.pop
else
@notes << pos
end
end
end
end
# after the end of the iteration the number of remaining opening brackets in the stack will be counted
# and that many number of closing brackets will be added to the end
def stack_remains
@stack.size.times {
@arr << $close
}
end
# after the iteration the positions noted in @notes will get a opening bracket inserted in front
def note_remains
@notes.map {|k| @arr.insert(k.to_i,$open) }
end
# creates a string from the array
def output
p @arr.flatten.join('')
end
# executes every step one by one to give the output
def final
check_first
iterate
stack_remains
note_remains
output
end
end
# create an object for the class Parse and initialize with the input from the user
obj = Parse.new(gets.to_s.strip)
# execute all methods and find the result by calling final
obj.final
@saurabhhack123
Copy link

Awesome , it's perfectly fine ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment