Menu
CoddyTech

Final: Sensor Port

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

challenge icon

Challenge

Easy

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.

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.

Go reference program. Match its behavior using the required Ruby APIs.

package main
import("bufio";"fmt";"io";"os";"strconv";"strings")
type Policy interface{Accepts(int)bool}
type Above struct{limit int}
func(a Above)Accepts(value int)bool{return value>a.limit}
type Sensor struct{policy Policy;accepted,invalid,total int64}
func parseToken(token string)(int,bool){
    i:=0;negative:=false
    if i<len(token)&&(token[i]=='+'||token[i]=='-'){negative=token[i]=='-';i++}
    if i==len(token){return 0,false}
    value:=0
    for ;i<len(token);i++{
        if token[i]<'0'||token[i]>'9'{return 0,false}
        digit:=int(token[i]-'0')
        if value>(1000000-digit)/10{return 0,false}
        value=value*10+digit
    }
    if negative{value=-value};return value,true
}
func(s *Sensor)Feed(token string){
    value,ok:=parseToken(token)
    if !ok{s.invalid++;return}
    if s.policy.Accepts(value){s.accepted++;s.total+=int64(value)}
}
func(s *Sensor)Summary()string{return fmt.Sprintf("%d %d %d",s.accepted,s.invalid,s.total)}
func main(){
    reader:=bufio.NewReader(os.Stdin)
    first,err:=reader.ReadString('\n')
    if err!=nil&&err!=io.EOF{panic(err)}
    limit,err:=strconv.Atoi(strings.TrimSpace(first));if err!=nil{panic(err)}
    sensor:=Sensor{policy:Above{limit}}
    for{
        line,readErr:=reader.ReadString('\n')
        if len(line)>0{sensor.Feed(strings.TrimSuffix(strings.TrimSuffix(line,"\n"),"\r"))}
        if readErr==io.EOF{break};if readErr!=nil{panic(readErr)}
    }
    fmt.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 Go Developers

Practice on your own: Online Ruby compiler