feat(exercies): easy binary_search exercise
This commit is contained in:
30
src/exercises/mod.rs
Normal file
30
src/exercises/mod.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
mod easy_difficulty;
|
||||
mod medium_difficulty;
|
||||
|
||||
pub fn run_medium() {
|
||||
println!("{}", medium_difficulty::longest_palindrome("asdffdas"));
|
||||
}
|
||||
|
||||
pub fn run_easy() {
|
||||
let list = vec![5, 1, 8, 20, 4];
|
||||
// easy_difficulty::binary_search(list, 20);
|
||||
// let list = easy_difficulty::slow_sort_list(list);
|
||||
// println!("List ordered to {:?}", list);
|
||||
println!(
|
||||
"The vec {:?} is {} sorted, but [1,2,3,4] is {} sorted",
|
||||
list,
|
||||
easy_difficulty::is_sorted(&list),
|
||||
easy_difficulty::is_sorted(&[1, 2, 3, 4])
|
||||
);
|
||||
|
||||
let sorted_list = vec![1, 10, 15, 20, 30];
|
||||
let searched = 40;
|
||||
let position = easy_difficulty::binary_search(&sorted_list, searched);
|
||||
match position {
|
||||
Ok(pos) => println!(
|
||||
"The element {searched} is indexed in {pos} at {:?}",
|
||||
sorted_list
|
||||
),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user