feat: completed solutions
This commit is contained in:
@@ -13,13 +13,13 @@ mod tests {
|
||||
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||
|
||||
// TODO: Create an iterator over the array.
|
||||
let mut fav_fruits_iterator = todo!();
|
||||
let mut fav_fruits_iterator = my_fav_fruits.iter();
|
||||
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple")); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"peach")); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), None); // TODO: Replace `todo!()`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
// "hello" -> "Hello"
|
||||
fn capitalize_first(input: &str) -> String {
|
||||
let mut chars = input.chars();
|
||||
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => todo!(),
|
||||
Some(first) => first.to_uppercase().chain(chars).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +16,14 @@ fn capitalize_first(input: &str) -> String {
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
// ???
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
// TODO: Apply the `capitalize_first` function again to a slice of string
|
||||
// slices. Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
fn capitalize_words_string(words: &[&str]) -> String {
|
||||
// ???
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -11,21 +11,36 @@ enum DivisionError {
|
||||
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||
// Otherwise, return a suitable error.
|
||||
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
||||
todo!();
|
||||
match (a, b) {
|
||||
(_, 0) => Err(DivisionError::DivideByZero),
|
||||
(a, b) if a == i64::MIN && b == -1 => Err(DivisionError::IntegerOverflow),
|
||||
(a, b) if a % b != 0 => Err(DivisionError::NotDivisible),
|
||||
(a, b) => Ok(a / b),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type and complete the function body.
|
||||
// Desired output: `Ok([1, 11, 1426, 3])`
|
||||
fn result_with_list() {
|
||||
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
|
||||
let numbers = [27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
let result: Vec<i64> = numbers
|
||||
.into_iter()
|
||||
.map(|n| divide(n, 27))
|
||||
.collect::<Result<Vec<i64>, DivisionError>>()?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type and complete the function body.
|
||||
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
||||
fn list_of_results() {
|
||||
fn list_of_results() -> Vec<Result<i64, DivisionError>> {
|
||||
let numbers = [27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
let division_results: Vec<Result<i64, DivisionError>> = numbers
|
||||
.into_iter()
|
||||
.map(|n| divide(n, 27))
|
||||
.collect::<Vec<Result<i64, DivisionError>>>();
|
||||
|
||||
division_results
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -10,6 +10,7 @@ fn factorial(num: u64) -> u64 {
|
||||
// - additional variables
|
||||
// For an extra challenge, don't use:
|
||||
// - recursion
|
||||
(1..=num).fold(1, |acc, e| acc * e)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -28,6 +28,9 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
// `map` is a hash map with `String` keys and `Progress` values.
|
||||
// map = { "variables1": Complete, "from_str": None, … }
|
||||
map.values()
|
||||
.filter(|excercise| **excercise == value)
|
||||
.count()
|
||||
}
|
||||
|
||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||
@@ -48,6 +51,11 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
|
||||
// `collection` is a slice of hash maps.
|
||||
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||
// { "variables2": Complete, … }, … ]
|
||||
collection
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter(|excercise| *excercise.1 == value)
|
||||
.count()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user