complete: iterators
This commit is contained in:
parent
77887224d2
commit
d6490f118b
exercises/standard_library_types
|
@ -18,19 +18,17 @@
|
||||||
// where the second TODO comment is. Try not to create any copies of the `numbers` Vec!
|
// where the second TODO comment is. Try not to create any copies of the `numbers` Vec!
|
||||||
// Execute `rustlings hint arc1` for hints :)
|
// Execute `rustlings hint arc1` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#![forbid(unused_imports)] // Do not change this, (or the next) line.
|
#![forbid(unused_imports)] // Do not change this, (or the next) line.
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let numbers: Vec<_> = (0..100u32).collect();
|
let numbers: Vec<_> = (0..100u32).collect();
|
||||||
let shared_numbers = // TODO
|
let shared_numbers = Arc::new(numbers);
|
||||||
let mut joinhandles = Vec::new();
|
let mut joinhandles = Vec::new();
|
||||||
|
|
||||||
for offset in 0..8 {
|
for offset in 0..8 {
|
||||||
let child_numbers = // TODO
|
let child_numbers = Arc::clone(&shared_numbers);
|
||||||
joinhandles.push(thread::spawn(move || {
|
joinhandles.push(thread::spawn(move || {
|
||||||
let mut i = offset;
|
let mut i = offset;
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
|
|
@ -16,11 +16,11 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint box1` for hints :)
|
// Execute `rustlings hint box1` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
use crate::List::{Cons, Nil};
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub enum List {
|
pub enum List {
|
||||||
Cons(i32, List),
|
Cons(i32, Box<List>),
|
||||||
Nil,
|
Nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,11 +33,13 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_empty_list() -> List {
|
pub fn create_empty_list() -> List {
|
||||||
unimplemented!()
|
Nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_non_empty_list() -> List {
|
pub fn create_non_empty_list() -> List {
|
||||||
unimplemented!()
|
Cons(1, Box::new(
|
||||||
|
Cons(2, Box::new(Nil))
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -8,17 +8,15 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint iterators1` for hints :D
|
// Execute `rustlings hint iterators1` for hints :D
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main () {
|
fn main () {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2.1
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 2.1
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 3
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,15 @@
|
||||||
// can offer. Follow the steps to complete the exercise.
|
// can offer. Follow the steps to complete the exercise.
|
||||||
// As always, there are hints if you execute `rustlings hint iterators2`!
|
// As always, there are hints if you execute `rustlings hint iterators2`!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// Step 1.
|
// Step 1.
|
||||||
// Complete the `capitalize_first` function.
|
// Complete the `capitalize_first` function.
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
pub fn capitalize_first(input: &str) -> String {
|
pub fn capitalize_first(input: &str) -> String {
|
||||||
let mut c = input.chars();
|
let mut c = input.chars();
|
||||||
match c.next() {
|
if let Some(first) = c.next() {
|
||||||
None => String::new(),
|
first.to_uppercase().to_string() + c.as_str()
|
||||||
Some(first) => ???,
|
}else{
|
||||||
|
String::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +20,7 @@ pub fn capitalize_first(input: &str) -> String {
|
||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
words.iter().map(|str| capitalize_first(str)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// Step 3.
|
||||||
|
@ -29,7 +28,7 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
// Return a single string.
|
// Return a single string.
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
pub fn capitalize_words_string(words: &[&str]) -> String {
|
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
words.iter().map(|str| capitalize_first(str)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
// list_of_results functions.
|
// list_of_results functions.
|
||||||
// Execute `rustlings hint iterators3` to get some hints!
|
// Execute `rustlings hint iterators3` to get some hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum DivisionError {
|
pub enum DivisionError {
|
||||||
NotDivisible(NotDivisibleError),
|
NotDivisible(NotDivisibleError),
|
||||||
|
@ -22,20 +20,35 @@ pub struct NotDivisibleError {
|
||||||
|
|
||||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||||
// Otherwise, return a suitable error.
|
// Otherwise, return a suitable error.
|
||||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||||
|
if b == 0 { Err(DivisionError::DivideByZero) }
|
||||||
|
else {
|
||||||
|
let s = a / b;
|
||||||
|
if s*b == a {
|
||||||
|
Ok(s)
|
||||||
|
}else{
|
||||||
|
Err(DivisionError::NotDivisible(NotDivisibleError {
|
||||||
|
dividend: a,
|
||||||
|
divisor: b
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test passes.
|
// Complete the function and return a value of the correct type so the test passes.
|
||||||
// Desired output: Ok([1, 11, 1426, 3])
|
// Desired output: Ok([1, 11, 1426, 3])
|
||||||
fn result_with_list() -> () {
|
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
|
division_results.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test passes.
|
// Complete the function and return a value of the correct type so the test passes.
|
||||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||||
fn list_of_results() -> () {
|
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
|
division_results.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
// iterators4.rs
|
// iterators4.rs
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn factorial(num: u64) -> u64 {
|
pub fn factorial(num: u64) -> u64 {
|
||||||
// Complete this function to return the factorial of num
|
// Complete this function to return the factorial of num
|
||||||
// Do not use:
|
// Do not use:
|
||||||
|
@ -12,6 +10,9 @@ pub fn factorial(num: u64) -> u64 {
|
||||||
// For an extra challenge, don't use:
|
// For an extra challenge, don't use:
|
||||||
// - recursion
|
// - recursion
|
||||||
// Execute `rustlings hint iterators4` for hints.
|
// Execute `rustlings hint iterators4` for hints.
|
||||||
|
(1..num+1).fold(1, |acc, x| {
|
||||||
|
acc*x
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
//
|
//
|
||||||
// Make the code compile and the tests pass.
|
// Make the code compile and the tests pass.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
|
@ -35,6 +33,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
// map is a hashmap with String keys and Progress values.
|
// map is a hashmap with String keys and Progress values.
|
||||||
// map = { "variables1": Complete, "from_str": None, ... }
|
// map = { "variables1": Complete, "from_str": None, ... }
|
||||||
|
map.values().filter(|&val| val == &value).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
|
@ -53,6 +52,10 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
|
||||||
// collection is a slice of hashmaps.
|
// collection is a slice of hashmaps.
|
||||||
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
||||||
// { "variables2": Complete, ... }, ... ]
|
// { "variables2": Complete, ... }, ... ]
|
||||||
|
|
||||||
|
collection.iter().fold(0, |acc, x| {
|
||||||
|
acc + x.values().filter(|&val| val == &value).count()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue