mirror of https://github.com/01-edu/public.git
Augusto
4 years ago
4 changed files with 247 additions and 0 deletions
@ -0,0 +1,93 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
[[package]] |
||||||
|
name = "arrange_it" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"jemalloc-ctl", |
||||||
|
"jemallocator", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "arrange_it_test" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"arrange_it", |
||||||
|
"jemalloc-ctl", |
||||||
|
"jemallocator", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cc" |
||||||
|
version = "1.0.65" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "fs_extra" |
||||||
|
version = "1.2.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "jemalloc-ctl" |
||||||
|
version = "0.3.3" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "c502a5ff9dd2924f1ed32ba96e3b65735d837b4bfd978d3161b1702e66aca4b7" |
||||||
|
dependencies = [ |
||||||
|
"jemalloc-sys", |
||||||
|
"libc", |
||||||
|
"paste", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "jemalloc-sys" |
||||||
|
version = "0.3.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45" |
||||||
|
dependencies = [ |
||||||
|
"cc", |
||||||
|
"fs_extra", |
||||||
|
"libc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "jemallocator" |
||||||
|
version = "0.3.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69" |
||||||
|
dependencies = [ |
||||||
|
"jemalloc-sys", |
||||||
|
"libc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "libc" |
||||||
|
version = "0.2.80" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "paste" |
||||||
|
version = "0.1.18" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" |
||||||
|
dependencies = [ |
||||||
|
"paste-impl", |
||||||
|
"proc-macro-hack", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "paste-impl" |
||||||
|
version = "0.1.18" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro-hack", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "proc-macro-hack" |
||||||
|
version = "0.5.19" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" |
@ -0,0 +1,12 @@ |
|||||||
|
[package] |
||||||
|
name = "arrange_it_test" |
||||||
|
version = "0.1.0" |
||||||
|
authors = ["lee <lee-dasilva@hotmail.com>"] |
||||||
|
edition = "2018" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
jemalloc-ctl = "0.3.3" |
||||||
|
jemallocator = "0.3.2" |
||||||
|
arrange_it = { path = "../../../../rust-piscine-solutions/arrange_it"} |
@ -0,0 +1,103 @@ |
|||||||
|
/* |
||||||
|
## arrange_it |
||||||
|
|
||||||
|
### Instructions |
||||||
|
|
||||||
|
Create a function called `arrange_phrase` that takes a string literal as a phrase and returns it organized |
||||||
|
Each word will have a number that indicates the position of that word |
||||||
|
|
||||||
|
### Example |
||||||
|
|
||||||
|
```rust |
||||||
|
``` |
||||||
|
|
||||||
|
> This exercise will test the **heap allocation** of your function! |
||||||
|
> So try your best to allocate the minimum data on the heap! |
||||||
|
|
||||||
|
### Notions |
||||||
|
|
||||||
|
- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html
|
||||||
|
- https://doc.rust-lang.org/std/primitive.str.html#method.split
|
||||||
|
|
||||||
|
*/ |
||||||
|
|
||||||
|
#[allow(unused_imports)] |
||||||
|
#[global_allocator] |
||||||
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; |
||||||
|
#[allow(unused_imports)] |
||||||
|
use arrange_it::*; |
||||||
|
|
||||||
|
#[allow(unused_imports)] |
||||||
|
use jemalloc_ctl::{epoch, stats}; |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
fn main() { |
||||||
|
println!("{:?}", arrange_phrase("is2 Thi1s T4est 3a")); |
||||||
|
} |
||||||
|
|
||||||
|
// example of function that works but does not pass the heap test
|
||||||
|
// fn arrange_phrase(phrase: &str) -> String {
|
||||||
|
// let words_nbr = phrase.matches(" ").count() + 1;
|
||||||
|
// let mut result_vec:Vec<String> = vec!["".to_string();words_nbr];
|
||||||
|
// for word in phrase.split_whitespace().into_iter() {
|
||||||
|
// for i in 1..words_nbr+1 {
|
||||||
|
// if word.contains(&i.to_string()){
|
||||||
|
// result_vec[i-1] = word.split(&i.to_string()).collect::<String>();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// result_vec.join(" ")
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
fn arrange_phrase_sol(phrase: &str) -> String { |
||||||
|
let nbrs: Vec<&str> = phrase.matches(char::is_numeric).collect(); |
||||||
|
let a = &phrase.replace(char::is_numeric, ""); |
||||||
|
let mut m: Vec<&str> = a.split_whitespace().collect(); |
||||||
|
|
||||||
|
for (i, ele) in nbrs.iter().enumerate() { |
||||||
|
let strs: Vec<&str> = a.split_whitespace().collect(); |
||||||
|
m[ele.parse::<usize>().unwrap() - 1] = strs[i]; |
||||||
|
} |
||||||
|
m.join(" ") |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn test_heap_memory_allocation() { |
||||||
|
// the statistics tracked by jemalloc are cached
|
||||||
|
// The epoch controls when they are refreshed
|
||||||
|
let e = epoch::mib().unwrap(); |
||||||
|
// allocated: number of bytes allocated by the application
|
||||||
|
let allocated = stats::allocated::mib().unwrap(); |
||||||
|
let test_value = "4of Fo1r pe6ople g3ood th5e the2"; |
||||||
|
|
||||||
|
arrange_phrase_sol(test_value); |
||||||
|
// this will advance with the epoch giving the its old value
|
||||||
|
// where we read the updated heap allocation using the `allocated.read()`
|
||||||
|
e.advance().unwrap(); |
||||||
|
let solution = allocated.read().unwrap(); |
||||||
|
|
||||||
|
arrange_phrase(test_value); |
||||||
|
e.advance().unwrap(); |
||||||
|
let student = allocated.read().unwrap(); |
||||||
|
|
||||||
|
assert!( |
||||||
|
student <= solution, |
||||||
|
format!( |
||||||
|
"your heap allocation is {}, and it must be less or equal to {}", |
||||||
|
student, solution |
||||||
|
) |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn test_function() { |
||||||
|
let cases = vec![ |
||||||
|
"4of Fo1r pe6ople g3ood th5e the2", |
||||||
|
"is2 Thi1s T4est 3a", |
||||||
|
"w7ork t3he a4rt o5f Per1formance is2 a6voiding", |
||||||
|
]; |
||||||
|
for v in cases { |
||||||
|
assert_eq!(arrange_phrase(v), arrange_phrase_sol(v)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
## arrange_it |
||||||
|
|
||||||
|
### Instructions |
||||||
|
|
||||||
|
Create a function called `arrange_phrase` that takes a string literal as a phrase and returns it organized |
||||||
|
Each word will have a number that indicates the position of that word |
||||||
|
|
||||||
|
> This exercise will test the **heap allocation** of your function! |
||||||
|
> So try your best to allocate the minimum data on the heap! |
||||||
|
|
||||||
|
### Expected Function |
||||||
|
|
||||||
|
```rust |
||||||
|
fn arrange_phrase(phrase: &str) -> String { |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
### Notions |
||||||
|
|
||||||
|
- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html |
||||||
|
- https://doc.rust-lang.org/std/primitive.str.html#method.split |
||||||
|
|
||||||
|
### Usage |
||||||
|
|
||||||
|
Here is a program to test your function |
||||||
|
|
||||||
|
```rust |
||||||
|
fn main() { |
||||||
|
println!("{:?}", initials("is2 Thi1s T4est 3a")); |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
And its output |
||||||
|
|
||||||
|
```console |
||||||
|
student@ubuntu:~/[[ROOT]]/test$ cargo run |
||||||
|
"This is a Test" |
||||||
|
student@ubuntu:~/[[ROOT]]/test$ |
||||||
|
``` |
Loading…
Reference in new issue