From b3d092a9022618b756af51a653a519821a11f661 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 13:28:18 +0000 Subject: [PATCH 01/28] fix(borrow): changed exercise --- subjects/borrow/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/subjects/borrow/README.md b/subjects/borrow/README.md index 031983bccf..1467788084 100644 --- a/subjects/borrow/README.md +++ b/subjects/borrow/README.md @@ -2,7 +2,7 @@ ### Instructions -Create a **function** named `str_len`, you'll need to complete the function signature. Your function should accept a string or a string literal, and return its length without taking ownership of the value (i.e, borrowing the value). +Create a **function** named `str_len`, you'll need to complete the function signature. Your function should accept a borrowed string, and return its length (in characters). ### Expected functions ```rust @@ -20,9 +20,11 @@ use borrow::*; fn main() { let s = "hello"; let s1 = "camelCase".to_string(); + let s2 = "olá!"; println!("\tstr_len(\"{}\") = {}", s, str_len(s)); println!("\tstr_len(\"{}\") = {}", s1, str_len(&s1)); + println!("\tstr_len(\"{}\") = {}", s2, str_len(s2)); } ``` @@ -32,6 +34,7 @@ And its output: $ cargo run str_len("hello") = 5 str_len("camelCase") = 9 +str_len("olá!") = 4 $ ``` From 0695ea3a6486a49f6f938cad502958d82cd2db58 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 13:34:21 +0000 Subject: [PATCH 02/28] fix(doubtful): changed exercise --- subjects/doubtful/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/subjects/doubtful/README.md b/subjects/doubtful/README.md index 11a2d4b49c..9af1742df3 100644 --- a/subjects/doubtful/README.md +++ b/subjects/doubtful/README.md @@ -17,14 +17,16 @@ You'll need to complete the function signature, so that it works properly with t Here is a program to test your function ```rust +use doubtful::*; + fn main() { - let mut s = String::from("Hello"); + let mut s = "Hello".to_owned(); - println!("Before changing the string: {}", s); + println!("Before changing the string: {}", s); - doubtful(/*add your code here*/); + doubtful(&mut s); - println!("After changing the string: {}", s); + println!("After changing the string: {}", s); } ``` From 601bc99d7f21affb7708c9b50ff16456bfe08b34 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 13:42:06 +0000 Subject: [PATCH 03/28] fix(to_url): changed exercise --- subjects/to_url/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/subjects/to_url/README.md b/subjects/to_url/README.md index 3e3f0d0702..eb178496c8 100644 --- a/subjects/to_url/README.md +++ b/subjects/to_url/README.md @@ -2,7 +2,7 @@ ### Instructions -Create a **function** named `to_url` which takes a string and substitutes every white-space with `"%20"`. +Create a **function** named `to_url` which takes a string and substitutes every ASCII space with `"%20"`. ### Expected functions ```rust @@ -18,8 +18,8 @@ Here is a program to test your function. use to_url::*; fn main() { - let s = "Hello, world!"; - println!("{} to be use as an url is {}", s, to_url(s)); + let s = "Hello, world!"; + println!("'{}' parsed as an URL becomes '{}'", s, to_url(s)); } ``` @@ -27,6 +27,6 @@ And its output ```console $ cargo run -Hello, world! to be use as an url is Hello,%20world! +'Hello, world!' parsed as an URL becomes 'Hello,%20world!' $ ``` From ccfc17819a1e30ba8feda2658fdd703b36b5c66e Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 14:17:30 +0000 Subject: [PATCH 04/28] fix(string_literals): changed exercise --- subjects/string_literals/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subjects/string_literals/README.md b/subjects/string_literals/README.md index 91330d8f25..e4d8f1a3d5 100644 --- a/subjects/string_literals/README.md +++ b/subjects/string_literals/README.md @@ -28,7 +28,7 @@ pub fn find(v: &str, pat: char) -> usize { } ``` -> Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized. +> You mustn't allocate to the heap in any of these functions. ### Usage From bd9de2b23895d98af78d1a9c278e31f487d93b54 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 14:57:49 +0000 Subject: [PATCH 05/28] fix(name_initials): changed exercise --- subjects/name_initials/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subjects/name_initials/README.md b/subjects/name_initials/README.md index 9db6cd42cf..71a187d071 100644 --- a/subjects/name_initials/README.md +++ b/subjects/name_initials/README.md @@ -18,7 +18,7 @@ pub fn initials(names: Vec<&str>) -> Vec { Here is a program to test your function: ```rust -use name_initials::initials; +use name_initials::*; fn main() { let names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"]; From b78ae3be6d2e29d4362002723a700de4ced8be3a Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 18:24:56 +0000 Subject: [PATCH 06/28] fix(ownership): changed exercise --- subjects/ownership/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/subjects/ownership/README.md b/subjects/ownership/README.md index 77ee9a3108..ab7de6a74c 100644 --- a/subjects/ownership/README.md +++ b/subjects/ownership/README.md @@ -2,7 +2,7 @@ ### Instruction -Create a **function** named `first_subword`, that takes ownership of a string and returns the first sub-word in it. It should work for `camelCase`, `PascalCase`, and `snake_case`. +Create a **function** named `first_subword`, that **moves** a string (takes ownership of it) and returns the first sub-word in it. You can mutate the original string. It should work for `camelCase`, `PascalCase`, and `snake_case`. ### Expected functions ```rust @@ -18,15 +18,15 @@ Here is a program to test your function: use ownership::first_subword; fn main() { - let s1 = String::from("helloWorld"); - let s2 = String::from("snake_case"); - let s3 = String::from("CamelCase"); - let s4 = String::from("just"); - - println!("first_subword({}) = {}", s1.clone(), first_subword(s1)); - println!("first_subword({}) = {}", s2.clone(), first_subword(s2)); - println!("first_subword({}) = {}", s3.clone(), first_subword(s3)); - println!("first_subword({}) = {}", s4.clone(), first_subword(s4)); + let s1 = "helloWorld"; + let s2 = "snake_case"; + let s3 = "CamelCase"; + let s4 = "just"; + + println!("first_subword({}) = {}", s1, first_subword(s1.to_owned())); + println!("first_subword({}) = {}", s2, first_subword(s2.to_owned())); + println!("first_subword({}) = {}", s3, first_subword(s3.to_owned())); + println!("first_subword({}) = {}", s4, first_subword(s4.to_owned())); } ``` From 8818b680f22fa7cd260abbce6bd20b2b34a570ac Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 18:32:15 +0000 Subject: [PATCH 07/28] fix(copy): changed exercise --- subjects/copy/README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/subjects/copy/README.md b/subjects/copy/README.md index 9108c6a035..0a75bc083a 100644 --- a/subjects/copy/README.md +++ b/subjects/copy/README.md @@ -29,19 +29,19 @@ pub fn vec_function(b: Vec) -> (Vec, Vec) { ### Usage -Here is a possible program to test your function : +Here is a possible program to test your function: ```rust use copy::*; fn main() { - let a: i32 = 0; - let b = String::from("1 2 4 5 6"); - let c = vec![1, 2, 4, 5]; + let a = "1 2 4 5 6".to_owned(); + let b = vec![1, 2, 4, 5]; + let c = 0; - println!("{:?}", nbr_function(a)); - println!("{:?}", str_function(b)); - println!("{:?}", vec_function(c)); + println!("{:?}", nbr_function(c)); + println!("{:?}", vec_function(b)); + println!("{:?}", str_function(a)); } ``` From 20499ff5224c563035fd515909c8f938a7fb62ab Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 10 Dec 2024 18:51:08 +0000 Subject: [PATCH 08/28] fix(borrow_me_the_reference): changed exercise --- subjects/borrow_me_the_reference/README.md | 28 +++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/subjects/borrow_me_the_reference/README.md b/subjects/borrow_me_the_reference/README.md index 8c48baf116..8d91d03cee 100644 --- a/subjects/borrow_me_the_reference/README.md +++ b/subjects/borrow_me_the_reference/README.md @@ -2,15 +2,15 @@ ### Instructions -> Ownership is Rust's most unique feature. It enables Rust to make memory safety guarantees without needing a garbage collector. +> Ownership is arguably Rust's most unique feature. It enables Rust to make memory safety guarantees without needing a garbage collector. -Understanding ownership is essential to take full advantage of Rust capabilities, it influences almost all aspects of the language. +Understanding ownership is essential to take full advantage of Rust's capabilities, as it influences almost all aspects of the language. Create the following functions: - `delete_and_backspace`: which receives a borrowed string, and processes it. `-` represents the backspace key and `+` represents the delete key, so that `"helll-o"` and `"he+lllo"` are both converted to `"hello"`. The `-` and `+` characters should be removed from the string. -- `do_operations`: which borrows a Vector of string literals representing simple addition and subtraction equations. The function should replace the operation with the result. +- `do_operations`: which borrows a vector of string literals representing simple addition and subtraction equations. The function should replace the operation with the result. ### Expected Functions @@ -18,7 +18,7 @@ Create the following functions: pub fn delete_and_backspace(s: &mut String) { } -pub fn do_operations(v: &mut Vec) { +pub fn do_operations(v: &mut [String]) { } ``` @@ -27,21 +27,21 @@ pub fn do_operations(v: &mut Vec) { Here is a program to test your function ```rust -use borrow_me_the_reference::{delete_and_backspace, do_operations}; +use borrow_me_the_reference::*; fn main() { - let mut a = String::from("bpp--o+er+++sskroi-++lcw"); - let mut b: Vec = vec![ - "2+2".to_string(), - "3+2".to_string(), - "10-3".to_string(), - "5+5".to_string(), + let mut a = "bpp--o+er+++sskroi-++lcw".to_owned(); + let mut b = [ + "2+2".to_owned(), + "3+2".to_owned(), + "10-3".to_owned(), + "5+5".to_owned(), ]; - delete_and_backspace(&mut a); - do_operations(&mut b); + delete_and_backspace(&mut a); + do_operations(&mut b); - println!("{:?}", (a, b)); + println!("{:?}", (a, b)); } ``` From 0bc38ffbfd55f894dc4a4e148fd43d69f74e22fc Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 11 Dec 2024 12:04:52 +0000 Subject: [PATCH 09/28] fix(tic_tac_toe): changed exercise --- subjects/tic_tac_toe/README.md | 44 +++++++++++++--------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/subjects/tic_tac_toe/README.md b/subjects/tic_tac_toe/README.md index c5c90b5731..f24213c588 100644 --- a/subjects/tic_tac_toe/README.md +++ b/subjects/tic_tac_toe/README.md @@ -11,52 +11,42 @@ Also create the following functions, which each accept a player and a table. The ### Expected functions ```rust -pub fn tic_tac_toe(table: Vec>) -> String { +pub fn tic_tac_toe(table: [[char; 3]; 3]) -> String { } -pub fn diagonals(player: &str, table: &Vec>) -> bool { +pub fn diagonals(player: char, table: [[char; 3]; 3]) -> bool { } -pub fn horizontal(player: &str, table: &Vec>) -> bool { +pub fn horizontal(player: char, table: [[char; 3]; 3]) -> bool { } -pub fn vertical(player: &str, table: &Vec>) -> bool { +pub fn vertical(player: char, table: [[char; 3]; 3]) -> bool { } ``` ### Usage -Here is a program to test your `tic_tac_toe`. You'll need to test the other functions yourself. But they'll probably be useful for implementing your `tic_tac_toe` checker. +Here is a program to test your `tic_tac_toe`. You'll need to test the other functions yourself. ```rust use tic_tac_toe::*; fn main() { println!( - "{:?}", - tic_tac_toe(vec![ - vec!["O", "X", "O"], - vec!["O", "O", "X"], - vec!["X", "#", "X"] - ]) + "{}", + tic_tac_toe([['O', 'X', 'O'], ['O', 'P', 'X'], ['X', '#', 'X']]) ); - + // tie println!( - "{:?}", - tic_tac_toe(vec![ - vec!["X", "O", "O"], - vec!["X", "O", "O"], - vec!["#", "O", "X"] - ]) + "{}", + tic_tac_toe([['X', 'O', 'O'], ['X', 'O', 'O'], ['#', 'O', 'X']]) ); + // player O won - let dig = vec![ - vec!["O", "O", "X"], - vec!["O", "X", "O"], - vec!["X", "#", "X"] - ]; + let diag = [['O', 'O', 'X'], ['O', 'X', 'O'], ['X', '#', 'X']]; - println!("{:?}",tic_tac_toe(dig)); + println!("{}", tic_tac_toe(diag)); + // player X won } ``` @@ -64,9 +54,9 @@ And its output ```console $ cargo run -"tie" -"player O won" -"player X won" +tie +player O won +player X won $ ``` From ac9002f32c92b785e5958db870239e7967dbbb9f Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 11 Dec 2024 12:46:29 +0000 Subject: [PATCH 10/28] fix(arrange_it): changed exercise --- subjects/arrange_it/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subjects/arrange_it/README.md b/subjects/arrange_it/README.md index ac06c12e28..d04bb1fa2e 100644 --- a/subjects/arrange_it/README.md +++ b/subjects/arrange_it/README.md @@ -22,7 +22,7 @@ Here is a program to test your function use arrange_it::*; fn main() { - println!("{:?}", arrange_phrase("is2 Thi1s T4est 3a")); + println!("{}", arrange_phrase("is2 Thi1s T4est 3a")); } ``` @@ -30,7 +30,7 @@ And its output ```console $ cargo run -"This is a Test" +This is a Test $ ``` From 42c2b650167bc0fc633ba75d409422d1df984a52 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 11 Dec 2024 12:56:04 +0000 Subject: [PATCH 11/28] fix(armstrong_number): changed exercise --- subjects/armstrong_number/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/subjects/armstrong_number/README.md b/subjects/armstrong_number/README.md index 0d4b048749..c6563f2eb5 100644 --- a/subjects/armstrong_number/README.md +++ b/subjects/armstrong_number/README.md @@ -18,9 +18,11 @@ pub fn is_armstrong_number(nb: u32) -> Option { ### Usage -Here is a possible program to test your function, +Here is a possible program to test your function: ```rust +use armstrong_number::*; + fn main() { println!("{:?}", is_armstrong_number(0)); println!("{:?}", is_armstrong_number(1)); From d4f960c6117ca9bdf71c3a55ea3d000133849c0b Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 11 Dec 2024 13:09:26 +0000 Subject: [PATCH 12/28] fix(quest-02): made prettier compliant --- subjects/arrange_it/README.md | 3 +-- subjects/borrow/README.md | 1 + subjects/copy/README.md | 1 + subjects/doubtful/README.md | 1 + subjects/name_initials/README.md | 2 +- subjects/ownership/README.md | 1 + subjects/string_literals/README.md | 1 + subjects/tic_tac_toe/README.md | 2 +- subjects/to_url/README.md | 1 + 9 files changed, 9 insertions(+), 4 deletions(-) diff --git a/subjects/arrange_it/README.md b/subjects/arrange_it/README.md index d04bb1fa2e..0f59c9bab0 100644 --- a/subjects/arrange_it/README.md +++ b/subjects/arrange_it/README.md @@ -5,6 +5,7 @@ Create a **function** named `arrange_phrase`, that takes a string literal, _sorts_ the words and returns it. Each word will contain a number that indicates the position of that word. ### Expected Functions + ```rust pub fn arrange_phrase(phrase: &str) -> String { } @@ -12,8 +13,6 @@ pub fn arrange_phrase(phrase: &str) -> String { > Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized. - - ### Usage Here is a program to test your function diff --git a/subjects/borrow/README.md b/subjects/borrow/README.md index 1467788084..8606610bbe 100644 --- a/subjects/borrow/README.md +++ b/subjects/borrow/README.md @@ -5,6 +5,7 @@ Create a **function** named `str_len`, you'll need to complete the function signature. Your function should accept a borrowed string, and return its length (in characters). ### Expected functions + ```rust pub fn str_len(s: ) -> usize { } diff --git a/subjects/copy/README.md b/subjects/copy/README.md index 0a75bc083a..cc93ecb9ed 100644 --- a/subjects/copy/README.md +++ b/subjects/copy/README.md @@ -16,6 +16,7 @@ Create the following **functions**. The objective is to know how ownership works - and the `natural logarithm` of each `absolute` value. ### Expected functions + ```rust pub fn nbr_function(c: i32) -> (i32, f64, f64) { } diff --git a/subjects/doubtful/README.md b/subjects/doubtful/README.md index 9af1742df3..e2bf21762f 100644 --- a/subjects/doubtful/README.md +++ b/subjects/doubtful/README.md @@ -5,6 +5,7 @@ Create a function named `doubtful` which appends a question mark to every string passed to it. It must not return a value. ### Expected functions + ```rust pub fn doubtful(s: /*give the correct type*/ ) { } diff --git a/subjects/name_initials/README.md b/subjects/name_initials/README.md index 71a187d071..4dd962eaa3 100644 --- a/subjects/name_initials/README.md +++ b/subjects/name_initials/README.md @@ -5,6 +5,7 @@ Create a **function** named `initials`. This function will receive a vector of string literals with names, and return a vector of Strings with the initials of each name. ### Expected Functions + ```rust pub fn initials(names: Vec<&str>) -> Vec { } @@ -12,7 +13,6 @@ pub fn initials(names: Vec<&str>) -> Vec { > Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized. - ### Usage Here is a program to test your function: diff --git a/subjects/ownership/README.md b/subjects/ownership/README.md index ab7de6a74c..edc5f8506f 100644 --- a/subjects/ownership/README.md +++ b/subjects/ownership/README.md @@ -5,6 +5,7 @@ Create a **function** named `first_subword`, that **moves** a string (takes ownership of it) and returns the first sub-word in it. You can mutate the original string. It should work for `camelCase`, `PascalCase`, and `snake_case`. ### Expected functions + ```rust pub fn first_subword(mut s: String) -> String { } diff --git a/subjects/string_literals/README.md b/subjects/string_literals/README.md index e4d8f1a3d5..1cb8e08229 100644 --- a/subjects/string_literals/README.md +++ b/subjects/string_literals/README.md @@ -11,6 +11,7 @@ Create the following functions: - `find`: that returns the index of the first character of a given string that matches the pattern. ### Expected functions + ```rust pub fn is_empty(v: &str) -> bool { } diff --git a/subjects/tic_tac_toe/README.md b/subjects/tic_tac_toe/README.md index f24213c588..a58d4c2082 100644 --- a/subjects/tic_tac_toe/README.md +++ b/subjects/tic_tac_toe/README.md @@ -6,10 +6,10 @@ You must create some functions for a tic-tac-toe checker. Create a function named `tic_tac_toe`, which receives a tic-tac-toe table. It should return the appropriate string: `"player O won"`, `"player X won"` or `"tie"`. - Also create the following functions, which each accept a player and a table. These functions should return `true` if the player has completed one of the diagonals, rows or columns: ### Expected functions + ```rust pub fn tic_tac_toe(table: [[char; 3]; 3]) -> String { } diff --git a/subjects/to_url/README.md b/subjects/to_url/README.md index eb178496c8..cb7a7325f9 100644 --- a/subjects/to_url/README.md +++ b/subjects/to_url/README.md @@ -5,6 +5,7 @@ Create a **function** named `to_url` which takes a string and substitutes every ASCII space with `"%20"`. ### Expected functions + ```rust pub fn to_url(s: &str) -> String { } From 2999fd905920ab14d511fa4aca5eec66783fbb8c Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Mon, 23 Dec 2024 16:06:51 +0000 Subject: [PATCH 13/28] fix(changes) --- subjects/changes/README.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) 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); } ``` From d95d441ec2e91327b2c51a314ec93ab1877c9ef9 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Mon, 23 Dec 2024 16:25:31 +0000 Subject: [PATCH 14/28] fix(circle) --- subjects/circle/README.md | 66 ++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/subjects/circle/README.md b/subjects/circle/README.md index 0170275730..cc7e92fbd6 100644 --- a/subjects/circle/README.md +++ b/subjects/circle/README.md @@ -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). ```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 $ ``` From 4344dc9f74c39a5a44aa8eff47efe5f381e328b1 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 24 Dec 2024 03:22:34 +0000 Subject: [PATCH 15/28] fix(card_deck) --- subjects/card_deck/README.md | 44 ++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 24 deletions(-) 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) From c818d62952dbfcb1e3d6340d2d0bc138f8721290 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 00:08:04 +0000 Subject: [PATCH 16/28] fix(arrays) --- subjects/arrays/README.md | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/subjects/arrays/README.md b/subjects/arrays/README.md index 28e0c1dc10..2a7fb8f82a 100644 --- a/subjects/arrays/README.md +++ b/subjects/arrays/README.md @@ -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] $ ``` From 330852c2d3806cf4219e0a17e84ca2b489fba1db Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 00:30:37 +0000 Subject: [PATCH 17/28] fix(arrays) --- subjects/arrays/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subjects/arrays/README.md b/subjects/arrays/README.md index 2a7fb8f82a..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 From d7fb77f8f891f3407db7ce23c563b45075ef7848 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 00:36:46 +0000 Subject: [PATCH 18/28] rm(strings) --- subjects/strings/README.md | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 subjects/strings/README.md 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 -$ -``` From fc918c75bdc96d580d9c51c7b146acba46494209 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 00:51:49 +0000 Subject: [PATCH 19/28] fix(capitalizing) --- subjects/capitalizing/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From befb2626ff0e438fb30027353a871b5fb6a33f7b Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 00:57:52 +0000 Subject: [PATCH 20/28] fix(edit_distance) --- subjects/edit_distance/README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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" $ ``` From 491292138b6e01181c7e882862c7997e9350b56d Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 01:12:54 +0000 Subject: [PATCH 21/28] fix(simple_hash) --- subjects/simple_hash/README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/subjects/simple_hash/README.md b/subjects/simple_hash/README.md index dbf628714c..8ba7184c4d 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)); } From 55670bdb2a08737c90c5f35d01e93895c7f3edf2 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 01:17:14 +0000 Subject: [PATCH 22/28] fix(bigger) --- subjects/bigger/README.md | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) 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) + ); } ``` From 63ec49e6a205c5c8ca14118b0d932a750542cc22 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 01:25:09 +0000 Subject: [PATCH 23/28] fix(string_permutation) --- subjects/string_permutation/README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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 $ ``` From 09feb7a08590696c34d804842cb4f8f3a7b61c7e Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 01:35:45 +0000 Subject: [PATCH 24/28] fix(hashing) --- subjects/hashing/README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) 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; From 9ae0340fd987e0daf7442e5b829b22035df49deb Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 01:53:41 +0000 Subject: [PATCH 25/28] fix(collect) --- subjects/collect/README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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); } ``` From f74a48c680eb8cd8f2412cd6ad9f6ddf82424afd Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 26 Dec 2024 14:56:38 +0000 Subject: [PATCH 26/28] fix(circle): improved md --- subjects/circle/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subjects/circle/README.md b/subjects/circle/README.md index cc7e92fbd6..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: @@ -20,7 +20,7 @@ Create the structures `Circle` and `Point`. You'll need to create the necessary ### 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, Clone, Copy)] From 484937a65f0930504eded2ccadca0be5c58d8f28 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Fri, 24 Jan 2025 19:24:42 +0000 Subject: [PATCH 27/28] fix(simple_hash): corrected readme --- subjects/simple_hash/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subjects/simple_hash/README.md b/subjects/simple_hash/README.md index 8ba7184c4d..71c6752319 100644 --- a/subjects/simple_hash/README.md +++ b/subjects/simple_hash/README.md @@ -38,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 $ ``` From 523ccf0533284b1dab8ba1c41b6babcdbc190561 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 28 Jan 2025 12:39:25 +0000 Subject: [PATCH 28/28] rm(is_anagram) --- subjects/is_anagram/README.md | 43 ----------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 subjects/is_anagram/README.md 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! -```