From 942b3c9851a8028a8df6c7de74cda5b1f907878c Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 29 Jan 2025 11:46:35 +0000 Subject: [PATCH] fix(rust-quest03): applied proposed changes --- subjects/arrays/README.md | 34 ++++++------- subjects/bigger/README.md | 21 ++++---- subjects/capitalizing/README.md | 4 +- subjects/card_deck/README.md | 44 ++++++++--------- subjects/changes/README.md | 20 ++++---- subjects/circle/README.md | 70 +++++++++++++-------------- subjects/collect/README.md | 17 ++++--- subjects/edit_distance/README.md | 19 ++++---- subjects/hashing/README.md | 22 ++++----- subjects/is_anagram/README.md | 43 ---------------- subjects/simple_hash/README.md | 12 ++--- subjects/string_permutation/README.md | 19 ++++---- subjects/strings/README.md | 38 --------------- 13 files changed, 136 insertions(+), 227 deletions(-) delete mode 100644 subjects/is_anagram/README.md delete mode 100644 subjects/strings/README.md diff --git a/subjects/arrays/README.md b/subjects/arrays/README.md index 28e0c1dc10..6a7f356eb5 100644 --- a/subjects/arrays/README.md +++ b/subjects/arrays/README.md @@ -4,7 +4,7 @@ Define a **function** named `thirtytwo_tens` that returns an array with 32 positions filled with only the value `10`, so that `[10, 10, 10, ... 10].len()` is equal to 32. -Write a **function** that takes an array of `i32` and returns the sum of the elements (make it work with the main). +Write a **function** that takes an array of `i32` and returns the sum of the elements. ### Expected functions @@ -12,7 +12,6 @@ The type of one of the arguments is missing. Use the example `main` function to ```rust pub fn sum(a: _) -> i32 { - //type of argument missing in the signature here } pub fn thirtytwo_tens() -> [i32; 32] { @@ -26,21 +25,19 @@ Here is a program to test your function. > It's incomplete. Use the output and the other available information to figure out what is missing. ```rust -use arrays::{sum, thirtytwo_tens}; +use arrays::*; fn main() { - let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let a1: Vec = (1..11).; //missing info here - let b = [_; 10]; //missing info here - - println!("The Sum of the elements in {:?} = {}", a, sum(a));//missing info here - println!("The Sum of the elements in {:?} = ", a1, sum(a1));//missing info here - println!("The Sum of the elements in {:?} = {}", b, sum(b));//missing info here - println!( - "Array size {} with only 10's in it {:?}", - thirtytwo_tens().len(), - thirtytwo_tens() - ); + let a = (1..=10)._; + let b = [_]; + + println!("The sum of the elements in {:?} is {}", a, sum(&a)); + println!("The sum of the elements in {:?} is {}", b, sum(&b)); + println!( + "Array of {} elements filled with 10 = {:?}", + thirtytwo_tens().len(), + thirtytwo_tens() + ); } ``` @@ -48,10 +45,9 @@ And its output: ```console $ cargo run -The Sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = 55 -The Sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = 55 -The Sum of the elements in [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] = 50 -Array size 32 with only 10's in it [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] +The sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 55 +The sum of the elements in [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] is 50 +Array of 32 elements filled with 10 = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] $ ``` diff --git a/subjects/bigger/README.md b/subjects/bigger/README.md index 0eb48dbb5c..31d0645c7c 100644 --- a/subjects/bigger/README.md +++ b/subjects/bigger/README.md @@ -16,18 +16,21 @@ pub fn bigger(h: HashMap<&str, i32>) -> i32 { Here is a program to test your function. ```rust +use bigger::*; use std::collections::HashMap; -use bigger::bigger; fn main() { - - let mut hash = HashMap::new(); - hash.insert("Daniel", 122); - hash.insert("Ashley", 333); - hash.insert("Katie", 334); - hash.insert("Robert", 14); - - println!("The biggest of the elements in the HashMap is {}", bigger(hash)); + let hash = HashMap::from_iter([ + ("Daniel", 122), + ("Ashley", 333), + ("Katie", 334), + ("Robert", 14), + ]); + + println!( + "The biggest of the elements in the HashMap is {}", + bigger(hash) + ); } ``` diff --git a/subjects/capitalizing/README.md b/subjects/capitalizing/README.md index 21d3187749..5ce9b8fd31 100644 --- a/subjects/capitalizing/README.md +++ b/subjects/capitalizing/README.md @@ -31,13 +31,13 @@ use capitalizing::*; fn main() { println!("{}", capitalize_first("joe is missing")); println!("{}", title_case("jill is leaving A")); - println!("{}",change_case("heLLo THere")); + println!("{}", change_case("heLLo THere")); } ``` And its output -```consoole +```console $ cargo run Joe is missing Jill Is Leaving A diff --git a/subjects/card_deck/README.md b/subjects/card_deck/README.md index bcfef9f407..b27d7d8032 100644 --- a/subjects/card_deck/README.md +++ b/subjects/card_deck/README.md @@ -19,10 +19,6 @@ Define: - The associated **function** `random` for `Rank` and `Suit` returns a random `Rank` and `Suit` respectively. - Finally define the **function** `winner_card` which returns `true` if the card passed as an argument is an ace of spades. -### Dependencies - -[rand = "0.3.14"](https://docs.rs/crate/rand/0.3.14) - ### Expected Functions and Structures ```rust @@ -33,24 +29,24 @@ pub enum Rank { } impl Suit { - pub fn random() -> Suit { - } + pub fn random() -> Suit { + } - pub fn translate(value: u8) -> Suit { - } + pub fn translate(value: u8) -> Suit { + } } impl Rank { - pub fn random() -> Rank { - } + pub fn random() -> Rank { + } - pub fn translate(value: u8) -> Rank { - } + pub fn translate(value: u8) -> Rank { + } } pub struct Card { - pub suit: Suit, - pub rank: Rank, + pub suit: Suit, + pub rank: Rank, } pub fn winner_card(card: &Card) -> bool { @@ -63,18 +59,18 @@ Here is a program to test your function ```rust use card_deck::*; + fn main() { - let your_card = Card { - rank: Rank::random(), - suit: Suit::random(), - }; + let your_card = Card { + rank: Rank::random(), + suit: Suit::random(), + }; - println!("Your card is {:?}", your_card); + println!("Your card is {:?}", your_card); - // Now if the card is an Ace of Spades print "You are the winner" - if card_deck::winner_card(&your_card) { - println!("You are the winner!"); - } + if card_deck::winner_card(your_card) { + println!("You are the winner!"); + } } ``` @@ -88,5 +84,5 @@ $ ### Notions -- [Crate rand](https://docs.rs/rand/0.3.14/rand/) +- [Crate rand](https://docs.rs/rand/latest/rand/) - [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html) diff --git a/subjects/changes/README.md b/subjects/changes/README.md index e5cacf06c4..1f8c24e256 100644 --- a/subjects/changes/README.md +++ b/subjects/changes/README.md @@ -6,7 +6,7 @@ Imagine you are working on some software to control smart lights in a house. You Define the associated **function** `new`, and add it to the data structure `Light`. It should create a new light with the alias passed as an argument, with a brightness of 0. -Define the **function** `change_brightness`, which receives a `Vec` of lights, an `alias` and a `u8`value. It should find the light in the `Vec` by its alias, and set the value of the brightness. +Define the **function** `change_brightness`, which receives a slice of lights, an `alias` and a `u8`value. It should attempt to find the correct light by its alias, and change the value of the brightness if found. ### Expected Functions and Structure @@ -22,7 +22,7 @@ impl Light { } } -pub fn change_brightness(lights: &mut Vec, alias: &str, value: u8) { +pub fn change_brightness(lights: &mut [Light], alias: &str, value: u8) { } ``` @@ -34,15 +34,13 @@ Here is an incomplete program to test your function use changes::*; fn main() { - // bedroom - let mut lights = vec![ - Light::new("living_room"), - Light::new("bedroom"), - Light::new("rest_room"), - ]; - println!("brightness = {}", lights[0].brightness); - change_brightness(&mut lights, "living_room", 200); - println!("new brightness = {}", lights[0].brightness); + let mut lights = ["living_room", "bedroom", "rest_room"].map(Light::new); + + println!("brightness = {}", lights[0].brightness); + + change_brightness(&mut lights, "living_room", 200); + + println!("new brightness = {}", lights[0].brightness); } ``` diff --git a/subjects/circle/README.md b/subjects/circle/README.md index 0170275730..ec2450144e 100644 --- a/subjects/circle/README.md +++ b/subjects/circle/README.md @@ -2,7 +2,7 @@ ### Instructions -Create the structures `Circle` and `Point`. You'll need to create the necessary methods for the code in the [usage](#usage) to compile, and give the expected output. +Create the structures `Circle` and `Point`. You'll need to create the necessary methods for the code to compile correctly. #### Methods: @@ -11,19 +11,19 @@ Create the structures `Circle` and `Point`. You'll need to create the necessary - `Circle`: - `diameter()` -> returns the diameter of the circle. - `area()` -> returns the area of the circle. - - `intersect()` -> which returns `true`, if 2 circles intersect. + - `intersect()` -> returns if two circles intersect. #### Associated Functions - `Circle`: - - `new()` -> receives three 64bit floating point numbers in the following order: x, y and radius (x and y are the coordinates of the center of the new circle). The function returns a new circle. + - `new()` -> receives three 64-bit floating point numbers in the following order: x, y and radius (x and y are the coordinates of the center of the new circle). The function returns a new circle. ### Expected Functions and Structures -This snippets are incomplete, you'll need to complete them. You'll find some useful information in the [usage](#usage). +This snippet is incomplete, you'll need to complete it. Base yourself from the usage section below. ```rust -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct Circle { pub center //.. pub radius //.. @@ -33,10 +33,8 @@ impl Circle { // ... } -#[derive(Debug)] -pub struct Point { - // ... -} +#[derive(Debug, Clone, Copy)] +pub struct Point(/* */); impl Point { // ... @@ -48,32 +46,30 @@ impl Point { Here is a program to test your function ```rust -use std::f64::consts; -use circle::{Circle, Point}; +use circle::*; fn main() { - let circle = Circle::new(500.0, 500.0, 150.0); - let circle1 = Circle { - center: Point { x: 80.0, y: 115.0 }, - radius: 30.0, - }; - let point_a = Point { x: 1.0, y: 1.0 }; - let point_b = Point { x: 0.0, y: 0.0 }; - println!("circle = {:?} area = {}", circle, circle.area()); - println!("circle = {:?} diameter = {}", circle, circle.diameter()); - println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter()); - println!( - "circle and circle1 intersect = {}", - circle.intersect(&circle1) - ); - - println!( - "distance between {:?} and {:?} is {}", - point_a, - point_b, - point_a.distance(&point_b) - ); - + let circle = Circle::new(500.0, 500.0, 150.0); + let circle1 = Circle { + center: Point(80.0, 115.0), + radius: 30.0, + }; + let point_a = Point(1.0, 1.0); + let point_b = Point(0.0, 0.0); + println!("circle = {:?} area = {}", circle, circle.area()); + println!("circle = {:?} diameter = {}", circle, circle.diameter()); + println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter()); + println!( + "circle and circle1 intersect = {}", + circle.intersect(circle1) + ); + + println!( + "distance between {:?} and {:?} is {}", + point_a, + point_b, + point_a.distance(point_b) + ); } ``` @@ -81,11 +77,11 @@ And its output ```console $ cargo run -circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } area = 70685.83470577035 -circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } diameter = 300 -circle1 = Circle { center: Point { x: 80.0, y: 115.0 }, radius: 30.0 } diameter = 60 +circle = Circle { center: Point(500.0, 500.0), radius: 150.0 } area = 70685.83470577035 +circle = Circle { center: Point(500.0, 500.0), radius: 150.0 } diameter = 300 +circle1 = Circle { center: Point(80.0, 115.0), radius: 30.0 } diameter = 60 circle and circle1 intersect = false -distance between Point { x: 1.0, y: 1.0 } and Point { x: 0.0, y: 0.0 } is 1.4142135623730951 +distance between Point(1.0, 1.0) and Point(0.0, 0.0) is 1.4142135623730951 $ ``` diff --git a/subjects/collect/README.md b/subjects/collect/README.md index 5cd01847f3..55993498a4 100644 --- a/subjects/collect/README.md +++ b/subjects/collect/README.md @@ -2,12 +2,12 @@ ### Instructions -Implement the **function** `bubble_sort`, which receives a `Vec` and returns the same vector but in increasing order using the bubble sort algorithm. +Implement the **function** `bubble_sort`, which receives a list of integers and sorts it in increasing order using the bubble sort algorithm. ### Expected Function ```rust -pub fn bubble_sort(vec: &mut Vec) { +pub fn bubble_sort(arr: &mut [i32]) { } ``` @@ -19,13 +19,14 @@ Here is a program to test your function. use collect::*; fn main() { - let ref mut v = vec![3, 2, 4, 5, 1, 7]; - let mut b = v.clone(); - bubble_sort(v); - println!("{:?}", v); + let mut v = [3, 2, 4, 5, 1, 7]; + let mut v_clone = v; - b.sort(); - println!("{:?}", b); + bubble_sort(&mut v); + println!("{:?}", v); + + v_clone.sort_unstable(); + println!("{:?}", v_clone); } ``` diff --git a/subjects/edit_distance/README.md b/subjects/edit_distance/README.md index 81b75a8d4c..73ef4c7150 100644 --- a/subjects/edit_distance/README.md +++ b/subjects/edit_distance/README.md @@ -19,14 +19,15 @@ Here is a program to test your function. use edit_distance::*; fn main() { - let source = "alignment"; - let target = "assignment"; - println!( - "It's necessary to make {} change(s) to {}, to get {}", - edit_distance(source, target), - source, - target - ); + let source = "alignment"; + let target = "assignment"; + + println!( + "It's necessary to make {} change(s) to {:?} to get {:?}", + edit_distance(source, target), + source, + target + ); } ``` @@ -34,7 +35,7 @@ And its output: ```console $ cargo run -It's necessary to make 2 change(s) to alignment, to get assignment +It's necessary to make 2 change(s) to "alignment" to get "assignment" $ ``` diff --git a/subjects/hashing/README.md b/subjects/hashing/README.md index 3afac3c830..5d2bbe3036 100644 --- a/subjects/hashing/README.md +++ b/subjects/hashing/README.md @@ -2,25 +2,24 @@ ### Instructions -Given a list of integers (`Vec`) write three **functions**. +Given a list of integers write three **functions**. - `mean`: that calculates the mean (the average value) of all the values in the list. - `median`: that calculates the median (for a sorted list, it is the value in the middle). If there is an even amount of numbers in the list, the middle pair must be determined, added together, and divided by two to find the median value. -- `mode` that calculates the mode (the value -that appears more often). +- `mode` that calculates the mode (the value that appears more often). ### Expected Functions ```rust -pub fn mean(list: &Vec) -> f64 { +pub fn mean(list: &[i32]) -> f64 { } -pub fn median(list: &Vec) -> i32 { +pub fn median(list: &[i32]) -> i32 { } -pub fn mode(list: &Vec) -> i32 { +pub fn mode(list: &[i32]) -> i32 { } ``` @@ -32,12 +31,13 @@ Here is a program to test your function. use hashing::*; fn main() { - println!("Hello, world!"); - let v = vec![4, 7, 5, 2, 5, 1, 3]; - println!("mean {}", hashing::mean(&v)); - println!("median {}", hashing::median(&v)); - println!("mode {}", hashing::mode(&v)); + let v = [4, 7, 5, 2, 5, 1, 3]; + + println!("mean {}", mean(&v)); + println!("median {}", median(&v)); + println!("mode {}", mode(&v)); } + ``` And its output; diff --git a/subjects/is_anagram/README.md b/subjects/is_anagram/README.md deleted file mode 100644 index ba6a744d8d..0000000000 --- a/subjects/is_anagram/README.md +++ /dev/null @@ -1,43 +0,0 @@ -## is_anagram - -### Instructions - -Write a function called `is_anagram` that checks if one string is an anagram of another string. An anagram is a word or phrase formed by rearranging the letters of another, such as "listen" and "silent." - -```rust -pub fn is_anagram(s1: &str, s2: &str) -> bool { - // Your code goes here -} -``` - -- `s1: &str`: The first input string. -- `s2: &str`: The second input string. - -The function should return `true` if `s1` is an anagram of `s2`, and `false` otherwise. -Your task is to implement the `is_anagram` function to determine whether the two input strings are anagrams of each other. - -### Usage - -Here is a possible runner to test your function: - -```rust -use is_anagram::is_anagram; - -fn main() { - let s1 = "listen"; - let s2 = "silent"; - - if is_anagram(s1, s2) { - println!("{} and {} are anagrams!", s1, s2); - } else { - println!("{} and {} are not anagrams.", s1, s2); - } -} -``` - -And its output: - -```console -$ cargo run -listen and silent are anagrams! -``` diff --git a/subjects/simple_hash/README.md b/subjects/simple_hash/README.md index dbf628714c..71c6752319 100644 --- a/subjects/simple_hash/README.md +++ b/subjects/simple_hash/README.md @@ -22,15 +22,13 @@ Here is a program to test your function. ```rust use simple_hash::*; -use std::collections::HashMap; + +const SENTENCE: &str = "this is a very basic sentence with only a few repetitions. once again this is very basic but it should be enough for basic tests"; fn main() { - let sentence = "this is a very basic sentence with only few \ - repetitions. once again this is very basic and \ - but it should be enough for basic tests".to_string(); - let words = sentence.split(" ").collect::>(); + let words = SENTENCE.split_ascii_whitespace().collect::>(); + let frequency_count = word_frequency_counter(&words); - let frequency_count = word_frequency_counter(words); println!("{:?}", frequency_count); println!("{}", nb_distinct_words(&frequency_count)); } @@ -40,7 +38,7 @@ And its output ```console $ cargo run -{"tests": 1, "with": 1, "this": 2, "it": 1, "enough": 1, "is": 2, "but": 1, "sentence": 1, "only": 1, "basic": 3, "again": 1, "for": 1, "be": 1, "once": 1, "very": 2, "should": 1, "few": 1, "and": 1, "a": 1, "repetitions.": 1} +{"tests": 1, "with": 1, "this": 2, "it": 1, "enough": 1, "is": 2, "but": 1, "sentence": 1, "only": 1, "basic": 3, "again": 1, "for": 1, "be": 1, "once": 1, "very": 2, "should": 1, "few": 1, "a": 2, "repetitions.": 1} 20 $ ``` diff --git a/subjects/string_permutation/README.md b/subjects/string_permutation/README.md index 4875daa8ad..a5a8fb4ccf 100644 --- a/subjects/string_permutation/README.md +++ b/subjects/string_permutation/README.md @@ -21,14 +21,15 @@ Here is a program to test your function. use string_permutation::*; fn main() { - let word = "thought"; - let word1 = "thougth"; - println!( - "Is `{}` a permutation of `{}`? = {}", - word, - word1, - is_permutation(word, word1) - ); + let word = "thought"; + let word1 = "thougth"; + + println!( + "Is {:?} a permutation of {:?}? = {}", + word, + word1, + is_permutation(word, word1) + ); } ``` @@ -36,7 +37,7 @@ And its output ```console $ cargo run -Is `thought` a permutation of `thougth`? = true +Is "thought" a permutation of "thougth"? = true $ ``` diff --git a/subjects/strings/README.md b/subjects/strings/README.md deleted file mode 100644 index 7f8cf18766..0000000000 --- a/subjects/strings/README.md +++ /dev/null @@ -1,38 +0,0 @@ -## strings - -### Instructions - -Create a **function** which receives a string slice and returns the number of characters in that string. - -### Expected Function - -```rust -pub fn char_length(s: &str) -> usize { -} -``` - -### Usage - -Here is a program to test your function. - -```rust -use strings::*; - -fn main() { - println!("length of {} = {}", "❤", char_length("❤")); - println!("length of {} = {}", "形声字", char_length("形聲字")); - println!("length of {} = {}", "change", char_length("change")); - println!("length of {} = {}", "😍", char_length("😍")); -} -``` - -And its output - -```console -$ cargo run -length of ❤ = 1 -length of 形声字 = 3 -length of change = 6 -length of 😍 = 1 -$ -```