This also checks to make sure that more people don't get off the stop than were on + got on that stop. This assumes no passengers were on the bus initially.
pubfnriders_left(bus_stops:&[(u32,u32)])->Result<u32,&str>{bus_stops.iter().fold(Ok(0),|sum,(on,off)|{letnew_sum=sum?+on;ifoff>&new_sum{Err("Somehow more people left the bus than got on")}else{Ok(new_sum-off)}})}#[cfg(test)]modtests{usecrate::*;#[test]fnit_returns_zero_for_an_empty_array(){assert_eq!(riders_left(&[]),Ok(0))}#[test]fnit_for_a_semi_complicated_array_example(){assert_eq!(riders_left(&[(10,0),(5,9),(6,6),(2,6)]),Ok(2));}#[test]fnit_for_a_semi_complicated_vec_example(){assert_eq!(riders_left(&vec![(10,0),(5,9),(6,6),(2,6)]),Ok(2));}#[test]fnit_for_a_broken_example(){assert_eq!(riders_left(&vec![(10,0),(5,9),(6,6),(2,12)]),Err("Somehow more people left the bus than got on"));}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust Solution
This also checks to make sure that more people don't get off the stop than were on + got on that stop. This assumes no passengers were on the bus initially.