Last active
September 12, 2020 23:03
-
-
Save gonzariosm/d68d15c64db3df18248ff26a6e30e631 to your computer and use it in GitHub Desktop.
Hackerank Ruby Super Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operations = ['push 4', 'push 5', 'inc 2 1', 'pop', 'pop'] | |
def superStack(operations) | |
stack = [] | |
size = 0 | |
operations.each do |operation| | |
parameters = operation.split | |
if parameters[0] == "push" then | |
stack[size] = parameters[1].to_i | |
size+=1 | |
elsif parameters[0] == "pop" then | |
stack.pop | |
size-=1 | |
elsif parameters[0] == "inc" then | |
# need optimization | |
(0..parameters[1].to_i-1).each do |i| | |
stack[i] += parameters[2].to_i | |
end | |
end | |
if size == 0 then | |
puts "EMPTY" | |
else | |
puts stack[size - 1] | |
end | |
end | |
end | |
superStack(operations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment