Created
April 1, 2021 00:41
-
-
Save dayvsonlima/b9598257758e3a902de87f01571c6c4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def set_command(store, inputs) | |
store[inputs[1]] = inputs[2] | |
store | |
end | |
def get_command(store, inputs) | |
puts store[inputs[1]] | |
end | |
def unset_command(store, inputs) | |
store.delete(inputs[1]) | |
store | |
end | |
def numequalto_command(store, inputs) | |
store.keys.each_with_index do |value, index| | |
if store[value] == inputs[1] | |
puts value | |
return | |
end | |
end | |
puts 0 | |
end | |
store = {} | |
stores = [store] | |
current_store = 0 | |
loop do | |
command = STDIN.gets | |
inputs = command.strip.split(' ') | |
case inputs[0] | |
when 'SET' | |
stores[current_store] = set_command(stores[current_store], inputs) | |
when 'GET' | |
get_command(stores[current_store], inputs) | |
when 'UNSET' | |
stores[current_store] = unset_command(stores[current_store], inputs) | |
when 'NUMEQUALTO' | |
numequalto_command(stores[current_store], inputs) | |
when 'BEGIN' | |
stores.push(stores[current_store].dup) | |
current_store+=1 | |
when 'ROLLBACK' | |
if current_store>0 | |
stores.pop | |
current_store-=1 | |
end | |
when 'COMMIT' | |
stores = [stores[current_store]] | |
when 'END' | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment