Skip to content

Commit

Permalink
merge Merge branch 'main' of https://github.com/mishalturkane/rust-lang-
Browse files Browse the repository at this point in the history
  • Loading branch information
mishalturkane committed May 29, 2024
2 parents c18ce55 + b817884 commit 6c51a83
Show file tree
Hide file tree
Showing 31 changed files with 187 additions and 0 deletions.
Binary file added array/avrg_of_array/main.exe
Binary file not shown.
Binary file added array/avrg_of_array/main.pdb
Binary file not shown.
14 changes: 14 additions & 0 deletions array/avrg_of_array/main.rs
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 added array/binary_search/main.exe
Binary file not shown.
Binary file added array/binary_search/main.pdb
Binary file not shown.
23 changes: 23 additions & 0 deletions array/binary_search/main.rs
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 added array/check_duplicates/main.exe
Binary file not shown.
Binary file added array/check_duplicates/main.pdb
Binary file not shown.
16 changes: 16 additions & 0 deletions array/check_duplicates/main.rs
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 added array/find_max/main.exe
Binary file not shown.
Binary file added array/find_max/main.pdb
Binary file not shown.
19 changes: 19 additions & 0 deletions array/find_max/main.rs
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 added array/find_min/mai.exe
Binary file not shown.
Binary file added array/find_min/mai.pdb
Binary file not shown.
21 changes: 21 additions & 0 deletions array/find_min/mai.rs
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 added array/linear_search/main.exe
Binary file not shown.
Binary file added array/linear_search/main.pdb
Binary file not shown.
16 changes: 16 additions & 0 deletions array/linear_search/main.rs
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 added array/remove_duplicates/main.exe
Binary file not shown.
Binary file added array/remove_duplicates/main.pdb
Binary file not shown.
16 changes: 16 additions & 0 deletions array/remove_duplicates/main.rs
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 added array/reverse/main.exe
Binary file not shown.
Binary file added array/reverse/main.pdb
Binary file not shown.
20 changes: 20 additions & 0 deletions array/reverse/main.rs
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 added array/second_large/main.exe
Binary file not shown.
Binary file added array/second_large/main.pdb
Binary file not shown.
24 changes: 24 additions & 0 deletions array/second_large/main.rs
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 added array/sum_of_array/main.exe
Binary file not shown.
Binary file added array/sum_of_array/main.pdb
Binary file not shown.
15 changes: 15 additions & 0 deletions array/sum_of_array/main.rs
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

}
3 changes: 3 additions & 0 deletions main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(){
println!("Hello Rust!");
}

0 comments on commit 6c51a83

Please sign in to comment.