Menu
CoddyTech

Recap: Limited Store

Lesson 42 of 48 in Coddy's Ruby for PHP Developers course.

challenge icon

Challenge

Easy

Define Store(capacity), with put(key,value), get(key), and size. put returns false for a new key when full, but updates existing keys even when full. get returns nil if absent. solve input starts with capacity then key=value pairs; return size|sorted key:value pairs.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb, store.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
require_relative 'store'
def check(value)
  raise 'Public API check failed' unless value
end
s = Store.new(1)
check(s.get("missing").nil? && s.put("a", 0) && !s.put("b", 2))
check(s.put("a", 3) && s.get("a") == 3 && s.size == 1 && s.get("b").nil?)

puts solve(STDIN.read)

All lessons in Ruby for PHP Developers

Practice on your own: Online Ruby compiler