Browse Source

Merge pull request #695 from 01-edu/improve-changes

Improve exercise `changes`
content-update
augusto-mantilla 4 years ago committed by GitHub
parent
commit
283d54dc48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 61
      rust/tests/changes_test/src/main.rs
  2. 41
      subjects/changes/README.md

61
rust/tests/changes_test/src/main.rs

@ -1,41 +1,54 @@
/* /*
## error types ## changes
### Instructions ### Instructions
Make this code compile Imagine you are working in a software to control smart lights in a
house. You have access to an array of all the lights in a house.
Define the associated function `new` to the data structure `Light`
which creates a new light with the alias passed in the arguments and
a brightness of 0.
Define the function `change_brightness` that receives a Vec of lights,
an alias and a u8 value and sets the u8 value as the new brightness of the light
identified by the alias in the Vec of lights.
*/ */
use changes::*; use changes::*;
fn main() { fn main() {
let mut a = String::from("Hello"); // bedroom
let mut lights = vec![
println!("the value of `a` before changing {}", a); Light::new("living_room"),
Light::new("bedroom"),
add_excitement(&mut a); Light::new("rest_room"),
];
println!("The value of `a` after changing {}", a); println!("brightness = {}", lights[0].brightness);
change_brightness(&mut lights, "living_room", 200);
println!("new brightness = {}", lights[0].brightness);
} }
// fn add_excitement(s: &mut String) {
// s.push_str("!");
// }
#[test] #[test]
fn test_ascii() { fn test_unexistente_alias() {
let mut expected = "hello".to_string(); let mut lights = Vec::new();
add_excitement(&mut expected); for i in 0..5 {
assert_eq!("hello!", &expected); let alias = format!("light-{}", i);
let mut expected = "go on".to_string(); lights.push(Light::new(&alias));
add_excitement(&mut expected); }
assert_eq!("go on!", &expected); let copy = lights.clone();
change_brightness(&mut lights, "light-6", 100);
assert_eq!(copy, lights);
} }
#[test] #[test]
fn test_unicode() { fn test_alias() {
let mut expected = "↕".to_string(); let mut lights = Vec::new();
add_excitement(&mut expected); for i in 0..5 {
assert_eq!(expected, "↕!"); let alias = format!("light-{}", i);
lights.push(Light::new(&alias));
}
let alias = "light-3";
change_brightness(&mut lights, alias, 100);
assert_eq!(lights[3].brightness, 100);
} }

41
subjects/changes/README.md

@ -2,12 +2,27 @@
### Instructions ### Instructions
Define the function `add_excitement` that add the exclamation point at the end of every sentenced that is passed to the function without returning anything but just modifying the input. Imagine you are working in a software to control smart lights in a house. You have access to an array of all the lights in a house.
### Expected Functions Define the associated function `new` to the data structure `Light` which creates a new light with the alias passed in the arguments and a brightness of 0.
Define the function `change_brightness` that receives a Vec of lights, an alias and a u8 value and sets the u8 value as the new brightness of the light identified by the alias in the Vec of lights.
### Expected Functions and Structure
```rust ```rust
fn add_excitement(s: &) { #[derive(Debug, Eq, PartialEq, Clone)]
pub struct Light {
pub alias: String,
pub brightness: u8,
}
impl Light {
pub fn new(alias: &str) -> Self {
}
}
pub fn change_brightness(lights: &mut Vec<Light>, alias: &str, value: u8) {
} }
``` ```
@ -17,13 +32,15 @@ Here is an incomplete program to test your function
```rust ```rust
fn main() { fn main() {
let mut a = String::from("Hello"); // bedroom
let mut lights = vec![
println!("the value of `a` before changing {}", a); Light::new("living_room"),
Light::new("bedroom"),
add_excitement(a); Light::new("rest_room"),
];
println!("The value of `a` after changing {}", a); println!("brightness = {}", lights[0].brightness);
change_brightness(&mut lights, "living_room", 200);
println!("new brightness = {}", lights[0].brightness);
} }
``` ```
@ -31,7 +48,7 @@ And its expected output
```console ```console
student@ubuntu:~/[[ROOT]]/test$ cargo run student@ubuntu:~/[[ROOT]]/test$ cargo run
the value of `a` before changing Hello brightness = 0
The value of `a` after changing Hello! new brightness = 200
student@ubuntu:~/[[ROOT]]/test$ student@ubuntu:~/[[ROOT]]/test$
``` ```

Loading…
Cancel
Save