Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
single linked list merge
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
*/
// I AM NOT DONE


use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
Expand Down Expand Up @@ -69,15 +69,34 @@ impl<T> LinkedList<T> {
},
}
}
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
{
//TODO
Self {
length: 0,
start: None,
end: None,
pub fn merge(mut list_a: LinkedList<i32>, mut list_b: LinkedList<i32>) -> LinkedList<i32> {
let mut merged_list = LinkedList::new();
while list_a.start.is_some() && list_b.start.is_some() {
unsafe {
if list_a.start.unwrap().as_ref().val < list_b.start.unwrap().as_ref().val{
merged_list.add(list_a.start.unwrap().as_ref().val);
list_a.start = list_a.start.unwrap().as_ref().next;
}
else {
merged_list.add(list_b.start.unwrap().as_ref().val);
list_b.start = list_b.start.unwrap().as_ref().next;
}
}
}
}
while list_a.start.is_some(){
unsafe{
merged_list.add(list_a.start.unwrap().as_ref().val);
list_a.start = list_a.start.unwrap().as_ref().next;
}
}
while list_b.start.is_some(){
unsafe{
merged_list.add(list_b.start.unwrap().as_ref().val);
list_b.start = list_b.start.unwrap().as_ref().next;
}
}
merged_list
}
}

impl<T> Display for LinkedList<T>
Expand Down
19 changes: 13 additions & 6 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE


use std::collections::{HashMap, HashSet};
use std::fmt;
Expand All @@ -29,19 +29,26 @@ impl Graph for UndirectedGraph {
&self.adjacency_table
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
}
self.add_node(edge.0) ;
self.add_node(edge.1) ;
self.adjacency_table_mutable().entry(edge.0.to_string()).and_modify(|q| q.push((edge.1.to_string(),edge.2)));
self.adjacency_table_mutable().entry(edge.1.to_string()).and_modify(|q| q.push((edge.0.to_string(),edge.2)));
}
}
pub trait Graph {
fn new() -> Self;
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool {
//TODO
true
if self.contains(node)
{
return false ;
}
self.adjacency_table_mutable().insert(node.to_string(),Vec::<(String, i32)>::new()) ;
return true ;
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO

}
fn contains(&self, node: &str) -> bool {
self.adjacency_table().get(node).is_some()
Expand Down
38 changes: 35 additions & 3 deletions exercises/algorithm/algorithm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
double linked list reverse
This problem requires you to reverse a doubly linked list
*/
// I AM NOT DONE


use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
Expand Down Expand Up @@ -73,8 +73,25 @@ impl<T> LinkedList<T> {
}
}
pub fn reverse(&mut self){
// TODO
}
if self.length < 2 {
return ;
}
let mut p = self.end ;
let end = self.start ;
let start = self.end ;
while p.is_some()
{
unsafe
{
let last = (*p.unwrap().as_ptr()).prev ;
(*p.unwrap().as_ptr()).prev = (*p.unwrap().as_ptr()).next;
(*p.unwrap().as_ptr()).next = last ;
p = last ;
}
}
self.start = start ;
self.end = end ;
}
}

impl<T> Display for LinkedList<T>
Expand Down Expand Up @@ -156,4 +173,19 @@ mod tests {
assert_eq!(reverse_vec[i],*list.get(i as i32).unwrap());
}
}
#[test]
fn test_reverse_linked_list_3() {
let mut list = LinkedList::<i32>::new();
let original_vec = vec![1,2];
let reverse_vec = vec![2,1];
for i in 0..original_vec.len(){
list.add(original_vec[i]);
}
println!("Linked List is {}", list);
list.reverse();
println!("Reversed Linked List is {}", list);
for i in 0..original_vec.len(){
assert_eq!(reverse_vec[i],*list.get(i as i32).unwrap());
}
}
}
14 changes: 12 additions & 2 deletions exercises/algorithm/algorithm3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
This problem requires you to implement a sorting algorithm
you can use bubble sorting, insertion sorting, heap sorting, etc.
*/
// I AM NOT DONE

fn sort<T>(array: &mut [T]){

fn sort<T:std::cmp::PartialOrd + std::ops::Add<Output = T> + std::ops::Sub<Output = T> >(array: &mut [T]){
//TODO
for i in 0..array.len()
{
for j in (i+1)..array.len()
{
if array[i] > array[j]
{
array.swap(i,j);
}
}
}
}
#[cfg(test)]
mod tests {
Expand Down
72 changes: 66 additions & 6 deletions exercises/algorithm/algorithm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This problem requires you to implement a basic interface for a binary tree
*/

//I AM NOT DONE

use std::cmp::Ordering;
use std::fmt::Debug;

Expand Down Expand Up @@ -37,6 +37,10 @@ where
right: None,
}
}
pub fn create(value:T)->Option<Box<TreeNode<T>>>{
let root = TreeNode::<T>::new(value) ;
Some(Box::<TreeNode<T>>::new(root))
}
}

impl<T> BinarySearchTree<T>
Expand All @@ -47,16 +51,41 @@ where
fn new() -> Self {
BinarySearchTree { root: None }
}

// Insert a value into the BST
fn insert(&mut self, value: T) {
//TODO
if self.root.is_none()
{
self.root =TreeNode::<T>::create(value);
return ();
}
TreeNode::<T>::insert( self.root.as_mut().unwrap(),value);


}

// Search for a value in the BST
fn search(&self, value: T) -> bool {
//TODO
true
if self.root.is_none()
{
return false ;
}
let mut p = &self.root;
while p.is_some()
{
if p.as_ref().unwrap().value == value
{
return true ;
}
else if p.as_ref().unwrap().value < value
{
p = & p.as_ref().unwrap().left ;
}
else
{
p = & p.as_ref().unwrap().right ;
}
}
return false ;
}
}

Expand All @@ -66,7 +95,38 @@ where
{
// Insert a node into the tree
fn insert(&mut self, value: T) {
//TODO
if self.value == value
{
return ();
}
if self.value < value
{
if self.left.is_none()
{
self.left = TreeNode::<T>::create(value) ;
return ();
}
else
{
TreeNode::<T>::insert(self.left.as_mut().unwrap(),value) ;
return ();
}
}

if self.value > value
{
if self.right.is_none()
{
self.right = TreeNode::<T>::create(value) ;
return ();
}
else
{
TreeNode::<T>::insert(self.right.as_mut().unwrap(),value) ;
return ();
}
}

}
}

Expand Down
21 changes: 20 additions & 1 deletion exercises/algorithm/algorithm5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This problem requires you to implement a basic BFS algorithm
*/

//I AM NOT DONE

use std::collections::VecDeque;

// Define a graph
Expand Down Expand Up @@ -31,6 +31,25 @@ impl Graph {
//TODO

let mut visit_order = vec![];
let mut st = vec![false ;self.adj.len()];
let mut queue = VecDeque::new() ;
st[start] = true ;
queue.push_back(start) ;
while !queue.is_empty()
{
let cur = *queue.front().unwrap();
visit_order.push(cur);
queue.pop_front();
for j in self.adj[cur].iter()
{
if st[*j] == true
{
continue ;
}
st[*j] = true ;
queue.push_back(*j) ;
}
}
visit_order
}
}
Expand Down
14 changes: 12 additions & 2 deletions exercises/algorithm/algorithm6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This problem requires you to implement a basic DFS traversal
*/

// I AM NOT DONE

use std::collections::HashSet;

struct Graph {
Expand All @@ -23,7 +23,17 @@ impl Graph {
}

fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
//TODO
visited.insert(v);
visit_order.push(v) ;
for j in self.adj[v].iter()
{
if visited.contains(j)
{
continue ;
}
visited.insert(*j);
self.dfs_util(*j,visited,visit_order);
}
}

// Perform a depth-first search on the graph, return the order of visited nodes
Expand Down
Loading