Final: Sensor Port
Lesson 48 of 48 in Coddy's Ruby for PHP 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.
PHP reference program. Match its behavior using the required Ruby APIs.
<?php
class Above {
private $limit;
public function __construct($limit) { $this->limit = $limit; }
public function accepts($value) { return $value > $this->limit; }
}
class Sensor {
private $policy;
private $accepted = 0;
private $invalid = 0;
private $total = 0;
public function __construct($policy) { $this->policy = $policy; }
public function feed($token) {
if (preg_match('/\A[+-]?[0-9]+\z/', $token) !== 1) { $this->invalid++; return; }
$value = (float)$token;
if (abs($value) > 1000000) $this->invalid++;
elseif ($this->policy->accepts((int)$value)) { $this->accepted++; $this->total += (int)$value; }
}
public function summary() { return $this->accepted . " " . $this->invalid . " " . $this->total; }
}
$sensor = new Sensor(new Above((int)fgets(STDIN)));
while (($line = fgets(STDIN)) !== false) $sensor->feed(rtrim($line, "\r\n"));
echo $sensor->summary(), "\n";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 PHP 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