-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge Merge branch 'main' of https://github.com/mishalturkane/rust-lang-
- Loading branch information
Showing
31 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
fn main() { | ||
let arr = vec![10.0, 20.0, 30.0, 40.0, 50.0, 1212.0, 133.0, 10.0, 0.0, 12.99]; | ||
println!("Average of the array is: {}", avrg_of_array(&arr)); | ||
} | ||
|
||
fn avrg_of_array(arr: &Vec<f64>) -> f64 { | ||
let mut sum = 0.0; | ||
let ele = arr.len(); | ||
|
||
for &i in arr.iter() { | ||
sum += i; | ||
} | ||
sum / ele as f64 | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
fn main() { | ||
let arr = vec![10, 20, 30, 40, 50]; | ||
println!("{}", binary_search(&arr, 20)); | ||
} | ||
|
||
fn binary_search(arr: &Vec<i32>, a: i32) -> i32 { | ||
let mut low = 0; | ||
let mut high = arr.len() - 1; | ||
|
||
while low <= high { | ||
let mid = (low + high) / 2; | ||
|
||
if arr[mid] == a { | ||
return mid as i32; | ||
} else if arr[mid] < a { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
|
||
-1 | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
fn main() { | ||
let arr = vec![1, 2, 3, 4, 5]; | ||
println!("Are there any duplicate elements in this array? {}", check_for_duplicates(&arr)); | ||
} | ||
|
||
fn check_for_duplicates(arr: &Vec<i32>) -> bool { | ||
for i in 0..arr.len() { | ||
for j in i + 1..arr.len() { | ||
if arr[i] == arr[j] { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
fn find_max(arr: &mut Vec<i32>) -> i32{ | ||
|
||
let mut max: i32 = i32::min_value(); | ||
|
||
|
||
for &num in arr.iter() { | ||
if num > max { | ||
max = num; | ||
} | ||
} | ||
|
||
return max; | ||
|
||
} | ||
fn main() { | ||
let mut arr = vec![2,343,3,34]; | ||
println!("max element is:{}",find_max(&mut arr)); | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
fn find_min(arr: &mut Vec<i32>) -> i32{ | ||
|
||
let mut min: i32 = i32::max_value(); | ||
|
||
|
||
for &num in arr.iter() { | ||
if num < min { | ||
min = num; | ||
} | ||
} | ||
|
||
return min; | ||
|
||
} | ||
|
||
fn main() { | ||
let mut arr = vec![1,2,3,2,6,8,0]; | ||
println!("max element is:{}",find_min(&mut arr)); | ||
} | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
fn main(){ | ||
let arr = vec![1,2,3,4,5]; | ||
println!("element is :{}",linear_search(&arr,22) ); | ||
} | ||
fn linear_search(arr: &Vec<i32>,a :i32) -> i32 { | ||
|
||
let element = -1; | ||
|
||
for &i in arr.iter(){ | ||
if a== i{ | ||
return i; | ||
} | ||
} | ||
element | ||
|
||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
fn main() { | ||
let arr = vec![1, 2, 3, 4, 4, 5]; | ||
println!("Array with duplicates removed: {:?}", remove_duplicates(&arr)); | ||
} | ||
|
||
fn remove_duplicates(arr: &Vec<i32>) -> Vec<i32> { | ||
let mut unique_elements = Vec::new(); | ||
|
||
for &num in arr { | ||
if !unique_elements.contains(&num) { | ||
unique_elements.push(num); | ||
} | ||
} | ||
|
||
unique_elements | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
fn main() { | ||
let mut arr = vec![1,2,3,2,6,8,0]; | ||
println!("reverse array is:{:?}",reverse_array(&mut arr)); | ||
} | ||
fn reverse_array(arr: &mut Vec<i32>) -> &Vec<i32> { | ||
|
||
let mut i = 0; | ||
let mut j = arr.len()-1; | ||
|
||
while i < j{ | ||
// let mut temp = arr[i]; | ||
// arr[i] = arr[j]; | ||
// arr[j] = temp; | ||
arr.swap(i,j); | ||
i +=1; | ||
j -= 1; | ||
} | ||
return arr; | ||
} | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
fn main() { | ||
let arr = vec![1, 2, 3, 4, 5]; | ||
println!("Second largest element: {}", second_largest(&arr)); | ||
} | ||
|
||
fn second_largest(arr: &Vec<i32>) -> i32 { | ||
if arr.len() < 2 { | ||
return -1; // Return a default value indicating that there is no second largest element | ||
} | ||
|
||
let mut max = arr[0]; | ||
let mut sec_max = i32::MIN; | ||
|
||
for &num in arr.iter().skip(1) { | ||
if num > max { | ||
sec_max = max; | ||
max = num; | ||
} else if num > sec_max && num < max { | ||
sec_max = num; | ||
} | ||
} | ||
|
||
sec_max | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn main(){ | ||
let arr = vec![10,20,30,40,50]; | ||
println!("sum of the array is:{}",sum_of_array(arr)); | ||
} | ||
|
||
fn sum_of_array(arr: Vec<i32>) -> i32{ | ||
|
||
let mut sum = 0; | ||
|
||
for &i in arr.iter(){ | ||
sum += i; | ||
} | ||
sum | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main(){ | ||
println!("Hello Rust!"); | ||
} |