Skip to content

Instantly share code, notes, and snippets.

@dayvsonlima
Created April 1, 2021 00:41
Show Gist options
  • Save dayvsonlima/b9598257758e3a902de87f01571c6c4f to your computer and use it in GitHub Desktop.
Save dayvsonlima/b9598257758e3a902de87f01571c6c4f to your computer and use it in GitHub Desktop.
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