Menu
CoddyTech

Recap: Record Readings

Lesson 15 of 48 in Coddy's Rust for Java Developers course.

challenge icon

Challenge

Easy

Report record-setting readings. Input is zero to thirty whitespace-separated signed integers between -1000 and 1000. Required API: pub fn records(values: &[i32]) -> Vec<i32> retains the first value and each later value strictly greater than every earlier value. Equal readings are not new records; empty input returns an empty vector. pub fn sum(values: &[i32]) -> i32 returns the sum of any provided slice, or zero for an empty slice. Neither API modifies its input. Output the record readings separated by commas on the first line, or empty; output sum=total for the retained readings on the second line. Negative first readings are valid records. Implement the public API in records.rs. The supplied exercise.rs parses valid input and calls that API. Return the specified text without a trailing newline; the locked driver adds one newline. Do not print from the API. All input values satisfy the stated bounds.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

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

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod records;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
let values = [-8, -8, -11, -3, -5, 7, 7, 9];
assert_eq!(records::records(&values), vec![-8, -3, 7, 9]);
assert_eq!(records::records(&[]), Vec::<i32>::new());
assert_eq!(records::records(&[-12]), vec![-12]);
assert_eq!(records::sum(&values), -12);
assert_eq!(records::sum(&[]), 0);
assert_eq!(records::sum(&[-12]), -12);
assert_eq!(records::records(&values[2..6]), vec![-11, -3, 7]);
assert_eq!(values, [-8, -8, -11, -3, -5, 7, 7, 9]);
assert_eq!(records::records(&values), vec![-8, -3, 7, 9]);
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}

All lessons in Rust for Java Developers

Practice on your own: Online Rust compiler