exercise for lab1

This commit is contained in:
mustakimur khandaker 2022-01-29 21:38:37 -05:00
parent 5002c54ffb
commit 1b252e4ba2
5 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,18 @@
In C, a simple bubble sort application would be following:
```c
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
```
Implement the same code in Rust.

View File

@ -0,0 +1,20 @@
fn bubbleSort(mut arr: [i32; 7], len: usize) -> [i32; 7] {
// write down the algorithm here
arr
}
fn printArray(arr: [i32; 7], len: usize) {
for item in arr.iter() {
println!("{}", item);
}
}
fn main() {
let mut arr = [64, 34, 25, 12, 22, 11, 90];
let sort = [11, 12, 22, 25, 34, 64, 90];
arr = bubbleSort(arr, arr.len());
println!("Sorted array: ");
printArray(arr, arr.len());
assert_eq!(arr, sort);
}

View File

@ -0,0 +1,19 @@
In C, a simple palindrome checker would be following:
```c
int pallindrome(int arr[], int n) {
int flag = 0;
for(int i = 0, j=n-1; i< n/2, j>=n/2; i++, j--) {
if(arr[i]!=arr[j]) {
flag = 1;
break;
}
}
if (flag == 1)
return 0;
else
return 1;
}
```
Implement the same code in Rust.

View File

@ -0,0 +1,12 @@
fn palindrome(arr: [i32; 7], len: usize) -> bool {
// write down the algorithm here
true
}
fn main() {
let arr1 = [1, 0, 2, 3, 2, 2, 1];
let arr2 = [2, 0, 2, 3, 2, 0, 2];
assert_eq!(palindrome(arr1, arr1.len()), false);
assert_eq!(palindrome(arr2, arr2.len()), true);
}

View File

@ -1041,3 +1041,16 @@ Challenge: There is one test that is marked `#[ignore]`. Can you supply the
missing code that will make it pass? You may want to consult the standard
library documentation for a certain trait for more hints.
"""
[[exercises]]
name = "bubble"
path = "exercises/bubble/bubble.rs"
mode = "compile"
hint = ""
[[exercises]]
name = "palindrome"
path = "exercises/palindrome/palindrome.rs"
mode = "compile"
hint = ""