You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-21 16:29:33 +02:00
Add solution for Health Statistics exercise (#1172)
This commit is contained in:
committed by
GitHub
parent
fc388570bf
commit
758639174a
@ -16,6 +16,10 @@ path = "day-1/luhn.rs"
|
|||||||
name = "book-library"
|
name = "book-library"
|
||||||
path = "day-2/book-library.rs"
|
path = "day-2/book-library.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "health-statistics"
|
||||||
|
path = "../../third_party/rust-on-exercism/health-statistics.rs"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "strings-iterators"
|
name = "strings-iterators"
|
||||||
path = "day-2/strings-iterators.rs"
|
path = "day-2/strings-iterators.rs"
|
||||||
|
@ -9,45 +9,41 @@ methods:
|
|||||||
// TODO: remove this when you're done with your implementation.
|
// TODO: remove this when you're done with your implementation.
|
||||||
#![allow(unused_variables, dead_code)]
|
#![allow(unused_variables, dead_code)]
|
||||||
|
|
||||||
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs}}
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:setup}}
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_new}}
|
||||||
fn main() {
|
todo!("Create a new User instance")
|
||||||
let bob = User::new(String::from("Bob"), 32, 155.2);
|
|
||||||
println!("I'm {} and my age is {}", bob.name(), bob.age());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_name}}
|
||||||
fn test_height() {
|
todo!("Return the user's name")
|
||||||
let bob = User::new(String::from("Bob"), 32, 155.2);
|
|
||||||
assert_eq!(bob.height(), 155.2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_age}}
|
||||||
fn test_set_age() {
|
todo!("Return the user's age")
|
||||||
let mut bob = User::new(String::from("Bob"), 32, 155.2);
|
|
||||||
assert_eq!(bob.age(), 32);
|
|
||||||
bob.set_age(33);
|
|
||||||
assert_eq!(bob.age(), 33);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_height}}
|
||||||
fn test_visit() {
|
todo!("Return the user's height")
|
||||||
let mut bob = User::new(String::from("Bob"), 32, 155.2);
|
|
||||||
assert_eq!(bob.doctor_visits(), 0);
|
|
||||||
let report = bob.visit_doctor(Measurements {
|
|
||||||
height: 156.1,
|
|
||||||
blood_pressure: (120, 80),
|
|
||||||
});
|
|
||||||
assert_eq!(report.patient_name, "Bob");
|
|
||||||
assert_eq!(report.visit_count, 1);
|
|
||||||
assert_eq!(report.blood_pressure_change, None);
|
|
||||||
|
|
||||||
let report = bob.visit_doctor(Measurements {
|
|
||||||
height: 156.1,
|
|
||||||
blood_pressure: (115, 76),
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(report.visit_count, 2);
|
|
||||||
assert_eq!(report.blood_pressure_change, Some((-5, -4)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_doctor_visits}}
|
||||||
|
todo!("Return the number of time the user has visited the doctor")
|
||||||
|
}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_set_age}}
|
||||||
|
todo!("Set the user's age")
|
||||||
|
}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_set_height}}
|
||||||
|
todo!("Set the user's height")
|
||||||
|
}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_visit_doctor}}
|
||||||
|
todo!("Update a user's statistics based on measurements from a visit to the doctor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:main}}
|
||||||
|
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:tests}}
|
||||||
```
|
```
|
||||||
|
@ -7,3 +7,11 @@
|
|||||||
```rust
|
```rust
|
||||||
{{#include book-library.rs:solution}}
|
{{#include book-library.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Health Statistics
|
||||||
|
|
||||||
|
([back to exercise](health-statistics.md))
|
||||||
|
|
||||||
|
```rust
|
||||||
|
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:solution}}
|
||||||
|
```
|
||||||
|
101
third_party/rust-on-exercism/health-statistics.rs
vendored
101
third_party/rust-on-exercism/health-statistics.rs
vendored
@ -1,3 +1,5 @@
|
|||||||
|
// ANCHOR: solution
|
||||||
|
// ANCHOR: setup
|
||||||
pub struct User {
|
pub struct User {
|
||||||
name: String,
|
name: String,
|
||||||
age: u32,
|
age: u32,
|
||||||
@ -19,35 +21,118 @@ pub struct HealthReport<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
|
// ANCHOR_END: setup
|
||||||
|
// ANCHOR: User_new
|
||||||
pub fn new(name: String, age: u32, height: f32) -> Self {
|
pub fn new(name: String, age: u32, height: f32) -> Self {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_new
|
||||||
|
Self {
|
||||||
|
name,
|
||||||
|
age,
|
||||||
|
height,
|
||||||
|
visit_count: 0,
|
||||||
|
last_blood_pressure: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_name
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_name
|
||||||
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_age
|
||||||
pub fn age(&self) -> u32 {
|
pub fn age(&self) -> u32 {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_age
|
||||||
|
self.age
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_height
|
||||||
pub fn height(&self) -> f32 {
|
pub fn height(&self) -> f32 {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_height
|
||||||
|
self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_doctor_visits
|
||||||
pub fn doctor_visits(&self) -> u32 {
|
pub fn doctor_visits(&self) -> u32 {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_doctor_visits
|
||||||
|
self.visit_count as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_set_age
|
||||||
pub fn set_age(&mut self, new_age: u32) {
|
pub fn set_age(&mut self, new_age: u32) {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_set_age
|
||||||
|
self.age = new_age
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_set_height
|
||||||
pub fn set_height(&mut self, new_height: f32) {
|
pub fn set_height(&mut self, new_height: f32) {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_set_height
|
||||||
|
self.height = new_height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: User_visit_doctor
|
||||||
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
|
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
|
||||||
unimplemented!()
|
// ANCHOR_END: User_visit_doctor
|
||||||
|
self.visit_count += 1;
|
||||||
|
let bp = measurements.blood_pressure;
|
||||||
|
let report = HealthReport {
|
||||||
|
patient_name: &self.name,
|
||||||
|
visit_count: self.visit_count as u32,
|
||||||
|
height_change: measurements.height - self.height,
|
||||||
|
blood_pressure_change: match self.last_blood_pressure {
|
||||||
|
Some(lbp) => Some((
|
||||||
|
bp.0 as i32 - lbp.0 as i32,
|
||||||
|
bp.1 as i32 - lbp.1 as i32
|
||||||
|
)),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.height = measurements.height;
|
||||||
|
self.last_blood_pressure = Some(bp);
|
||||||
|
report
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ANCHOR: main
|
||||||
|
fn main() {
|
||||||
|
let bob = User::new(String::from("Bob"), 32, 155.2);
|
||||||
|
println!("I'm {} and my age is {}", bob.name(), bob.age());
|
||||||
|
}
|
||||||
|
// ANCHOR_END: main
|
||||||
|
|
||||||
|
// ANCHOR: tests
|
||||||
|
#[test]
|
||||||
|
fn test_height() {
|
||||||
|
let bob = User::new(String::from("Bob"), 32, 155.2);
|
||||||
|
assert_eq!(bob.height(), 155.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_age() {
|
||||||
|
let mut bob = User::new(String::from("Bob"), 32, 155.2);
|
||||||
|
assert_eq!(bob.age(), 32);
|
||||||
|
bob.set_age(33);
|
||||||
|
assert_eq!(bob.age(), 33);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_visit() {
|
||||||
|
let mut bob = User::new(String::from("Bob"), 32, 155.2);
|
||||||
|
assert_eq!(bob.doctor_visits(), 0);
|
||||||
|
let report = bob.visit_doctor(Measurements {
|
||||||
|
height: 156.1,
|
||||||
|
blood_pressure: (120, 80),
|
||||||
|
});
|
||||||
|
assert_eq!(report.patient_name, "Bob");
|
||||||
|
assert_eq!(report.visit_count, 1);
|
||||||
|
assert_eq!(report.blood_pressure_change, None);
|
||||||
|
|
||||||
|
let report = bob.visit_doctor(Measurements {
|
||||||
|
height: 156.1,
|
||||||
|
blood_pressure: (115, 76),
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(report.visit_count, 2);
|
||||||
|
assert_eq!(report.blood_pressure_change, Some((-5, -4)));
|
||||||
|
}
|
||||||
|
// ANCHOR_END: tests
|
||||||
|
Reference in New Issue
Block a user