Skip to content

Commit 3f95325

Browse files
committed
refactor exercise e to use integers instead of strings, stop using args, stop using functions
1 parent 260b366 commit 3f95325

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

Diff for: exercise/e_control_flow/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "d_control_flow_strings"
2+
name = "e_control_flow"
33
version = "0.1.0"
44
edition = "2021"
55

Diff for: exercise/e_control_flow/src/main.rs

+53-43
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
11
// Silence some warnings so they don't distract from the exercise.
2-
#![allow(dead_code, unused_mut, unused_variables)]
2+
#![allow(unused_mut, unused_variables)]
33

44
fn main() {
5-
// This collects any command-line arguments into a vector of Strings.
6-
// For example:
5+
// 1. Use an unconditional `loop` to count how many times we can double `bunnies` until there
6+
// are over 500 bunnies. (Hint: The answer is 8 times)
77
//
8-
// cargo run apple banana
9-
//
10-
// ...produces an 'args' variable with the equivalent of:
11-
//
12-
// vec!["apple".to_string(), "banana".to_string()]
13-
let args: Vec<String> = std::env::args().skip(1).collect();
14-
15-
// This consumes the `args` vector to iterate through each String
16-
for arg in args {
17-
// 1a. Your task: handle the command-line arguments!
18-
//
19-
// - If arg is "sum", then call the sum() function
20-
// - If arg is "double", then call the double() function
21-
// - If arg is anything else, then call the count() function, passing "arg" to it.
22-
23-
// 1b. Now try passing "sum", "double" and "bananas" to the program by adding your argument
24-
// after "cargo run". For example "cargo run sum"
25-
}
26-
}
27-
28-
fn sum() {
29-
let mut sum = 0;
30-
// 2. Use a "for loop" to iterate through integers from 7 to 23 *inclusive* using a range
31-
// and add them all together (increment the `sum` variable). Hint: You should get 255
32-
// Run it with `cargo run sum`
33-
34-
println!("The sum is {}", sum);
35-
}
8+
// Inside the loop:
9+
// - Add 1 to `count`
10+
// - Multiply `bunnies` by 2
11+
// - If `bunnies` is larger than 500, break out of the loop.
3612

37-
fn double() {
3813
let mut count = 0;
39-
let mut x = 1;
40-
// 3. Use a "while loop" to count how many times you can double the value of `x` (multiply `x`
41-
// by 2) until `x` is larger than 500. Increment `count` each time through the loop. Run it
42-
// with `cargo run double` Hint: The answer is 9 times.
14+
let mut bunnies = 2;
15+
16+
// (write your `loop` here)
4317

4418
println!(
45-
"You can double x {} times until x is larger than 500",
19+
"Bunnies doubled {} times before there were more than 500",
4620
count
4721
);
48-
}
4922

50-
fn count(arg: String) {
51-
// Challenge: Use an unconditional loop (`loop`) to print `arg` 8 times, and then break.
52-
// You will need to count your loops, somehow. Run it with `cargo run bananas`
23+
// 2. Use a `for` loop to iterate through integers from 7 to 23 *inclusive* using a range
24+
// and add them all together (add each value to the `sum` variable). Hint: You should get 255
25+
26+
let mut sum = 0;
27+
28+
// (write the `for` loop here)
29+
30+
println!("The sum is {}", sum);
31+
32+
// 3. Use a `while` loop to create a vector containing the first 12 multiples of 5.
33+
//
34+
// The loop should continue until `fives.len()` returns 12.
35+
//
36+
// Each time through the loop, call `fives.push( ... )` to push `current_five` onto the vector,
37+
// and then add 5 to `current_five`.
38+
39+
let mut fives: Vec<i32> = vec![];
40+
let mut current_five = 5;
41+
42+
// (write the `while` loop here)
43+
44+
println!("Here are the first 12 multiples of 5: {:?}", fives);
45+
46+
// 4. Use `if`, `else if` and `else` inside the `for` loop below to do the following:
5347
//
54-
// print!("{} ", arg); // Execute this line 8 times, and then break. `print!` doesn't add a newline.
48+
// - If the number is 0, then add 7 to `total`
49+
// - If the number is 1 or 2 then add 30 to `total`
50+
// - If the number is anything else, subtract 5 from `total`
51+
//
52+
// Hint: The total should be 52
5553

56-
println!(); // This will output just a newline at the end for cleanliness.
54+
let mut total = 0;
55+
let numbers = vec![0, 1, 2, 3, 4, 5];
56+
for number in numbers {
57+
// (write your `if/else` expression here)
58+
}
59+
60+
println!("The total is {}", total);
61+
62+
// Challenge: Change the implementation of your answers to #1-#3 as follows:
63+
//
64+
// - Change #1 to use `while`
65+
// - Change #2 to use `loop`
66+
// - Change #3 to use `for` and a range (multiply the range value by 5 inside your loop before
5767
}

0 commit comments

Comments
 (0)