Final: Sensor Port
Lesson 48 of 48 in Coddy's Ruby for Java Developers course.
Challenge
EasyPort 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.
Input bounds: at most 1000 data lines. Inventory codes contain only lowercase ASCII letters, and each inventory delta is between -1000000 and 1000000. For the sensor port, the first-line limit is between -1000000 and 1000000; token validity follows the parser contract above.
Java reference program. Match its behavior using the required Ruby APIs.
import java.io.*;
import java.math.BigInteger;
public class Main {
interface Policy { boolean accepts(int value); }
static class Above implements Policy {
final int limit;
Above(int limit) { this.limit = limit; }
public boolean accepts(int value) { return value > limit; }
}
static class Sensor {
final Policy policy;
BigInteger accepted = BigInteger.ZERO;
BigInteger invalid = BigInteger.ZERO;
BigInteger total = BigInteger.ZERO;
Sensor(Policy policy) { this.policy = policy; }
void feed(String token) {
if (!token.matches("[+-]?[0-9]+")) { invalid = invalid.add(BigInteger.ONE); return; }
BigInteger value = new BigInteger(token);
if (value.abs().compareTo(BigInteger.valueOf(1000000)) > 0) invalid = invalid.add(BigInteger.ONE);
else if (policy.accepts(value.intValue())) {
accepted = accepted.add(BigInteger.ONE);
total = total.add(value);
}
}
String summary() { return accepted + " " + invalid + " " + total; }
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Sensor sensor = new Sensor(new Above(Integer.parseInt(reader.readLine())));
String line;
while ((line = reader.readLine()) != null) sensor.feed(line);
System.out.println(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 Java 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