-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor exercise e to use integers instead of strings, stop using ar…
…gs, stop using functions
- Loading branch information
Showing
2 changed files
with
54 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "d_control_flow_strings" | ||
name = "e_control_flow" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,67 @@ | ||
// Silence some warnings so they don't distract from the exercise. | ||
#![allow(dead_code, unused_mut, unused_variables)] | ||
#![allow(unused_mut, unused_variables)] | ||
|
||
fn main() { | ||
// This collects any command-line arguments into a vector of Strings. | ||
// For example: | ||
// 1. Use an unconditional `loop` to count how many times we can double `bunnies` until there | ||
// are over 500 bunnies. (Hint: The answer is 8 times) | ||
// | ||
// cargo run apple banana | ||
// | ||
// ...produces an 'args' variable with the equivalent of: | ||
// | ||
// vec!["apple".to_string(), "banana".to_string()] | ||
let args: Vec<String> = std::env::args().skip(1).collect(); | ||
|
||
// This consumes the `args` vector to iterate through each String | ||
for arg in args { | ||
// 1a. Your task: handle the command-line arguments! | ||
// | ||
// - If arg is "sum", then call the sum() function | ||
// - If arg is "double", then call the double() function | ||
// - If arg is anything else, then call the count() function, passing "arg" to it. | ||
|
||
// 1b. Now try passing "sum", "double" and "bananas" to the program by adding your argument | ||
// after "cargo run". For example "cargo run sum" | ||
} | ||
} | ||
|
||
fn sum() { | ||
let mut sum = 0; | ||
// 2. Use a "for loop" to iterate through integers from 7 to 23 *inclusive* using a range | ||
// and add them all together (increment the `sum` variable). Hint: You should get 255 | ||
// Run it with `cargo run sum` | ||
|
||
println!("The sum is {}", sum); | ||
} | ||
// Inside the loop: | ||
// - Add 1 to `count` | ||
// - Multiply `bunnies` by 2 | ||
// - If `bunnies` is larger than 500, break out of the loop. | ||
|
||
fn double() { | ||
let mut count = 0; | ||
let mut x = 1; | ||
// 3. Use a "while loop" to count how many times you can double the value of `x` (multiply `x` | ||
// by 2) until `x` is larger than 500. Increment `count` each time through the loop. Run it | ||
// with `cargo run double` Hint: The answer is 9 times. | ||
let mut bunnies = 2; | ||
|
||
// (write your `loop` here) | ||
|
||
println!( | ||
"You can double x {} times until x is larger than 500", | ||
"Bunnies doubled {} times before there were more than 500", | ||
count | ||
); | ||
} | ||
|
||
fn count(arg: String) { | ||
// Challenge: Use an unconditional loop (`loop`) to print `arg` 8 times, and then break. | ||
// You will need to count your loops, somehow. Run it with `cargo run bananas` | ||
// 2. Use a `for` loop to iterate through integers from 7 to 23 *inclusive* using a range | ||
// and add them all together (add each value to the `sum` variable). Hint: You should get 255 | ||
|
||
let mut sum = 0; | ||
|
||
// (write the `for` loop here) | ||
|
||
println!("The sum is {}", sum); | ||
|
||
// 3. Use a `while` loop to create a vector containing the first 12 multiples of 5. | ||
// | ||
// The loop should continue until `fives.len()` returns 12. | ||
// | ||
// Each time through the loop, call `fives.push( ... )` to push `current_five` onto the vector, | ||
// and then add 5 to `current_five`. | ||
|
||
let mut fives: Vec<i32> = vec![]; | ||
let mut current_five = 5; | ||
|
||
// (write the `while` loop here) | ||
|
||
println!("Here are the first 12 multiples of 5: {:?}", fives); | ||
|
||
// 4. Use `if`, `else if` and `else` inside the `for` loop below to do the following: | ||
// | ||
// print!("{} ", arg); // Execute this line 8 times, and then break. `print!` doesn't add a newline. | ||
// - If the number is 0, then add 7 to `total` | ||
// - If the number is 1 or 2 then add 30 to `total` | ||
// - If the number is anything else, subtract 5 from `total` | ||
// | ||
// Hint: The total should be 52 | ||
|
||
println!(); // This will output just a newline at the end for cleanliness. | ||
let mut total = 0; | ||
let numbers = vec![0, 1, 2, 3, 4, 5]; | ||
for number in numbers { | ||
// (write your `if/else` expression here) | ||
} | ||
|
||
println!("The total is {}", total); | ||
|
||
// Challenge: Change the implementation of your answers to #1-#3 as follows: | ||
// | ||
// - Change #1 to use `while` | ||
// - Change #2 to use `loop` | ||
// - Change #3 to use `for` and a range (multiply the range value by 5 inside your loop before | ||
} |