Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solutions/adding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "adding"
version = "0.1.0"
authors = ["MSilva95 <miguel-silva98@hotmail.com>"]
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
84 changes: 2 additions & 82 deletions solutions/adding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,3 @@
/*
## adding

### Instructions

Create the function `add_curry` that returns a closure.
The purpose is to curry the add method to create more variations
so that you can use the curried method like:

// let add_1 = add_curry(1);
// add_1(2) -> 3

### Notions

- https://doc.rust-lang.org/book/ch13-02-iterators.html

fn main() {
let add10 = add_curry(-10);
let add20 = add_curry(2066);
let add30 = add_curry(300000);

println!("{}", add10(5));
println!("{}", add20(195));
println!("{}", add30(5696));
}
*/
pub fn add_curry(x: i32) -> impl Fn(i32) -> i32 {
return move |y| x + y;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_zero() {
let z = add_curry(0);
assert_eq!(z(1999), 1999);
}

#[test]
fn test_negatives() {
let neg = add_curry(-10);
assert_eq!(neg(6), -4);
assert_eq!(neg(10), 0);
assert_eq!(neg(600), 590);
assert_eq!(neg(1000), 990);
assert_eq!(neg(463), 453);
assert_eq!(neg(400000000), 399999990);
}

#[test]
fn test_add10() {
let add10 = add_curry(10);
assert_eq!(add10(6), 16);
assert_eq!(add10(10), 20);
assert_eq!(add10(600), 610);
assert_eq!(add10(1000), 1010);
assert_eq!(add10(463), 473);
assert_eq!(add10(400000000), 400000010);
}

#[test]
fn test_add250() {
let add250 = add_curry(250);
assert_eq!(add250(6), 256);
assert_eq!(add250(10), 260);
assert_eq!(add250(600), 850);
assert_eq!(add250(1000), 1250);
assert_eq!(add250(463), 713);
assert_eq!(add250(400000000), 400000250);
}
#[test]
fn test_add3960() {
let add3960 = add_curry(3960);
assert_eq!(add3960(6), 3966);
assert_eq!(add3960(10), 3970);
assert_eq!(add3960(600), 4560);
assert_eq!(add3960(1000), 4960);
assert_eq!(add3960(463), 4423);
assert_eq!(add3960(400000000), 400003960);
}
pub const fn add_curry(x: i32) -> impl Fn(i32) -> i32 {
move |y| x + y
}
2 changes: 1 addition & 1 deletion solutions/adding_twice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "adding_twice"
version = "0.1.0"
authors = ["MSilva95 <miguel-silva98@hotmail.com>"]
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
120 changes: 4 additions & 116 deletions solutions/adding_twice/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,119 +1,7 @@
/*
## adding_twice

### Instructions

In this exercise you will have to reuse your `add_curry` function
Then you have to complete the function `twice` using closures, this function will
take a function f(x) as parameter and return a function f(f(x))
So, the purpose of this function is to add two times the value in `add_curry` to the original value.

### Notions

- https://doc.rust-lang.org/rust-by-example/fn/hof.html#higher-order-functions

### Expected functions

The type of the arguments are missing use the example `main` function to determine the correct type.

```rust
fn twice<T>(F: _) -> _{}
```

### Usage

Here is a program to test your function.

```rust
fn main() {
let add10 = add_curry(10);
let value = twice(add10);
println!("The value is {}", value(7));

let add20 = add_curry(20);
let value = twice(add20);
println!("The value is {}", value(7));

let add30 = add_curry(30);
let value = twice(add30);
println!("The value is {}", value(7));

let neg = add_curry(-32);
let value = twice(neg);
println!("The value is {}", value(7));
}
```

And its output

```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
The value is 27
The value is 47
The value is 67
The value is -57
student@ubuntu:~/[[ROOT]]/test$
```

*/

pub fn twice<T>(function: impl Fn(T) -> T) -> impl Fn(T) -> T {
move |a| function(function(a))
pub const fn add_curry(x: i32) -> impl Fn(i32) -> i32 {
move |y| x + y
}

pub fn add_curry(x: i32) -> impl Fn(i32) -> i32 {
return move |y| -> i32 { x + y };
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_zero_twice() {
let z = twice(add_curry(0));
assert_eq!(z(1902), 1902);
}

#[test]
fn test_negative_twice() {
let neg = twice(add_curry(-32));
assert_eq!(neg(6), -58);
assert_eq!(neg(10), -54);
assert_eq!(neg(660), 596);
assert_eq!(neg(1902), 1838);
assert_eq!(neg(463), 399);
assert_eq!(neg(400000000), 399999936);
}

#[test]
fn test_add10_twice() {
let value = twice(add_curry(10));
assert_eq!(value(6), 26);
assert_eq!(value(10), 30);
assert_eq!(value(600), 620);
assert_eq!(value(1000), 1020);
assert_eq!(value(463), 483);
assert_eq!(value(400000000), 400000020);
}
#[test]
fn test_add20_twice() {
let value = twice(add_curry(20));
assert_eq!(value(6), 46);
assert_eq!(value(10), 50);
assert_eq!(value(600), 640);
assert_eq!(value(1000), 1040);
assert_eq!(value(463), 503);
assert_eq!(value(400000000), 400000040);
}
#[test]
fn test_add30_twice() {
let value = twice(add_curry(30));
assert_eq!(value(6), 66);
assert_eq!(value(10), 70);
assert_eq!(value(600), 660);
assert_eq!(value(1000), 1060);
assert_eq!(value(463), 523);
assert_eq!(value(400000000), 400000060);
}
pub const fn twice(f: impl Fn(i32) -> i32) -> impl Fn(i32) -> i32 {
move |x| f(f(x))
}
2 changes: 1 addition & 1 deletion solutions/closures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "closures"
version = "0.1.0"
authors = ["Augusto <aug.ornelas@gmail.com>"]
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
36 changes: 6 additions & 30 deletions solutions/closures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
// Using closures and iterators create a function,
// first_fifty_even_square() that returns the
// first 50 pair numbers squares that it's [4, 16, 36, ..., 10000].

use std::iter::Iterator;

#[inline]
pub fn first_fifty_even_square() -> Vec<i32> {
(1..101).filter(|x| x % 2 == 0).map(|x| x * x).collect()
}

// fn main() {
// println!("Hello, world!");
// let v1 = first_fifty_even_square();

// println!("All elements in {:?}, len = {}", v1, v1.len());
// }

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test() {
let v1 = vec![
4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296,
1444, 1600, 1764, 1936, 2116, 2304, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096,
4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056, 7396, 7744, 8100, 8464,
8836, 9216, 9604, 10000,
];
assert_eq!(v1, first_fifty_even_square());
}
(1..)
.filter(|x| x % 2 == 0)
.take(50)
.map(|x| x * x)
.collect()
}
2 changes: 1 addition & 1 deletion solutions/get_products/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "get_products"
version = "0.1.0"
authors = ["MSilva95 <miguel-silva98@hotmail.com>"]
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
79 changes: 9 additions & 70 deletions solutions/get_products/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,10 @@
/*
## get_products

### Instructions

Create a function `get_products` that takes an array of integers, and returns an array of the products
of each index. For this exercise to be correct you will have to return the product of every index
except the current one.

### Example:

Input: arr[] = {10, 3, 5, 6, 2}
Output: prod[] = {180, 600, 360, 300, 900}

```rust
fn main() {
let arr: Vec<usize> = vec![1, 7, 3, 4];
let output = get_products(arr);
println!("{:?}", output);
}
```
*/

pub fn get_products(arr: Vec<usize>) -> Vec<usize> {
let mut counter: usize = 0;
let mut product_results: Vec<usize> = Vec::new();

if arr.len() < 2 {
return product_results;
};

for _ in arr.iter() {
let mut prod: usize = 1;
let mut others: Vec<usize> = arr.clone();
others.remove(counter);
for x in others.iter() {
prod *= *x;
}
product_results.push(prod);
counter += 1;
}
return product_results;
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_multiple() {
let arr: Vec<usize> = vec![1, 7, 3, 4];
let output = get_products(arr);
let arr2: Vec<usize> = vec![10, 3, 5, 6, 2];
let output2 = get_products(arr2);
assert_eq!(output, vec![84, 12, 28, 21]);
assert_eq!(output2, vec![180, 600, 360, 300, 900]);
}

#[test]
fn test_empty_case() {
let arr: Vec<usize> = Vec::new();
let output = get_products(arr);
assert_eq!(output, vec![]);
}

#[test]
fn test_single_case() {
let arr: Vec<usize> = vec![2];
let output = get_products(arr);
assert_eq!(output, vec![]);
}
#[inline]
pub fn get_products<const N: usize>(arr: [u32; N]) -> [u32; N] {
std::array::from_fn(|i| {
arr.into_iter()
.enumerate()
.filter(|&(j, _)| j != i)
.map(|(_, v)| v)
.product()
})
}
2 changes: 1 addition & 1 deletion solutions/highest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "highest"
version = "0.1.0"
authors = ["MSilva95 <miguel-silva98@hotmail.com>"]
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
Loading