From 1b252e4ba2c4d6fd362deabb0fa6acb71ab01ec3 Mon Sep 17 00:00:00 2001 From: mustakimur khandaker Date: Sat, 29 Jan 2022 21:38:37 -0500 Subject: [PATCH] exercise for lab1 --- exercises/bubble/README.md | 18 ++++++++++++++++++ exercises/bubble/bubble.rs | 20 ++++++++++++++++++++ exercises/palindrome/README.md | 19 +++++++++++++++++++ exercises/palindrome/palindrome.rs | 12 ++++++++++++ info.toml | 13 +++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 exercises/bubble/README.md create mode 100644 exercises/bubble/bubble.rs create mode 100644 exercises/palindrome/README.md create mode 100644 exercises/palindrome/palindrome.rs diff --git a/exercises/bubble/README.md b/exercises/bubble/README.md new file mode 100644 index 00000000..c211816a --- /dev/null +++ b/exercises/bubble/README.md @@ -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. \ No newline at end of file diff --git a/exercises/bubble/bubble.rs b/exercises/bubble/bubble.rs new file mode 100644 index 00000000..4423ea4a --- /dev/null +++ b/exercises/bubble/bubble.rs @@ -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); +} diff --git a/exercises/palindrome/README.md b/exercises/palindrome/README.md new file mode 100644 index 00000000..f399fb5c --- /dev/null +++ b/exercises/palindrome/README.md @@ -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. \ No newline at end of file diff --git a/exercises/palindrome/palindrome.rs b/exercises/palindrome/palindrome.rs new file mode 100644 index 00000000..acf250a0 --- /dev/null +++ b/exercises/palindrome/palindrome.rs @@ -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); +} diff --git a/info.toml b/info.toml index 0c03def3..10a1aec1 100644 --- a/info.toml +++ b/info.toml @@ -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 = "" \ No newline at end of file