Final: Sensor Port
Lesson 48 of 48 in Coddy's Ruby for Lua Developers course.
Challenge
EasyInput bounds: at most 1000 token lines after the first line. The first-line limit is an integer between -1000000 and 1000000 inclusive. Token validity and accepted-value bounds follow the parser contract above.
Port the reference program. Above(limit)#accept?(n) accepts n strictly above limit. Sensor(policy)#feed(token) parses an optional sign followed by one or more ASCII digits, with no whitespace or other characters, and accepts only integers from -1000000 to 1000000. Invalid tokens increment invalid; valid tokens accepted by policy increment accepted and add to total. Rejected valid tokens change neither count. summary returns accepted invalid total separated by spaces. Input first line is the limit; each remaining line is one token with its line ending removed. All three counters start at zero. Custom policy objects must work.
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, above.rb, sensor.rb. main.rb holds the supplied input/output code and cannot be edited.
Lua reference program. Match its behavior using the required Ruby APIs.
local Above = {}
Above.__index = Above
function Above.new(limit) return setmetatable({limit = limit}, Above) end
function Above:accepts(value) return value > self.limit end
local Sensor = {}
Sensor.__index = Sensor
function Sensor.new(policy)
return setmetatable({policy = policy, accepted = 0, invalid = 0, total = 0}, Sensor)
end
function Sensor:feed(token)
if not string.match(token, "^[+-]?[0-9]+$") then
self.invalid = self.invalid + 1
return
end
local value = tonumber(token)
if value == nil or math.abs(value) > 1000000 then
self.invalid = self.invalid + 1
elseif self.policy:accepts(value) then
self.accepted = self.accepted + 1
self.total = self.total + value
end
end
function Sensor:summary()
return string.format("%.0f %.0f %.0f", self.accepted, self.invalid, self.total)
end
local sensor = Sensor.new(Above.new(tonumber(io.read("*l"))))
for line in io.lines() do sensor:feed((string.gsub(line, "\r$", ""))) end
print(sensor:summary())Try it yourself
require_relative 'exercise'
require_relative 'above'
require_relative 'sensor'
def check(value)
raise 'Public API check failed' unless value
end
check(Above.new(4).accept?(5) && !Above.new(4).accept?(4))
policy = Object.new
def policy.accept?(n); n.even?; end
s = Sensor.new(policy)
["2", "-4", "3", "x", "4\n", "1000001"].each { |t| s.feed(t) }
check(s.summary == "2 3 -2" && s.summary == "2 3 -2")
puts solve(STDIN.read)
All lessons in Ruby for Lua Developers
1Start with Ruby
Run Ruby FilesOutput and CommentsNames and ObjectsInteger ArithmeticTruth and Boolean LogicString OperationsSymbols and KeysRecap: Packing Slip2Flow and Methods
Conditional ValuesCase and RangesLoops and Early ExitMethods and ReturnsDefaults and KeywordsRecap: Sensor Run5Objects and State
Modules as NamespacesClasses and InstancesReaders and WritersValidated ConstructionRecap: Wallet8Lending Desk Project
Lending Desk: CatalogLending Desk: BorrowPractice on your own: Online Ruby compiler