2020-12-29 10:40:42 +00:00
|
|
|
## get_products
|
|
|
|
|
|
|
|
### Instructions
|
|
|
|
|
2022-06-14 21:34:10 +00:00
|
|
|
Create a function named `get_products` that takes a vector of integers, and returns a vector of the products of each index.
|
2020-12-29 10:40:42 +00:00
|
|
|
|
2022-06-14 21:34:10 +00:00
|
|
|
You'll need to return the product of every index
|
|
|
|
except the current one.
|
2021-03-10 04:18:44 +00:00
|
|
|
|
2022-06-14 21:34:10 +00:00
|
|
|
### Example:
|
|
|
|
For `[1,2,3,4]`, we get:
|
2021-03-10 04:18:44 +00:00
|
|
|
|
2022-06-14 21:34:10 +00:00
|
|
|
- for the number `1` we get `2*3*4 = 24`.
|
|
|
|
- for the number `3` we get `1*2*4 = 8`.
|
2020-12-29 10:40:42 +00:00
|
|
|
|
|
|
|
### Expected functions
|
|
|
|
|
|
|
|
```rust
|
2021-03-10 04:18:44 +00:00
|
|
|
pub fn get_products(arr: Vec<usize>) -> Vec<usize> {
|
|
|
|
|
|
|
|
}
|
2020-12-29 10:40:42 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
|
|
Here is a program to test your function.
|
|
|
|
|
|
|
|
```rust
|
2020-12-29 14:38:26 +00:00
|
|
|
use get_products::get_products;
|
|
|
|
|
2020-12-29 10:40:42 +00:00
|
|
|
fn main() {
|
|
|
|
let arr: Vec<usize> = vec![1, 7, 3, 4];
|
|
|
|
let output = get_products(arr);
|
|
|
|
println!("{:?}", output);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-03-10 04:18:44 +00:00
|
|
|
And its output:
|
2020-12-29 10:40:42 +00:00
|
|
|
|
|
|
|
```console
|
2021-04-28 09:47:34 +00:00
|
|
|
$ cargo run
|
2020-12-29 10:40:42 +00:00
|
|
|
[84, 12, 28, 21]
|
2021-04-28 09:47:34 +00:00
|
|
|
$
|
2020-12-29 10:40:42 +00:00
|
|
|
```
|
2022-06-14 21:34:10 +00:00
|
|
|
|
|
|
|
### Notions
|
|
|
|
|
|
|
|
- [Trait iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
|