Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinklerG committed Jan 18, 2025
1 parent 54edb24 commit 91b332e
Show file tree
Hide file tree
Showing 91 changed files with 942 additions and 578 deletions.
324 changes: 162 additions & 162 deletions .github/result/check_result.json

Large diffs are not rendered by default.

131 changes: 73 additions & 58 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
single linked list merge
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
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 All @@ -16,10 +15,7 @@ struct Node<T> {

impl<T> Node<T> {
fn new(t: T) -> Node<T> {
Node {
val: t,
next: None,
}
Node { val: t, next: None }
}
}
#[derive(Debug)]
Expand All @@ -29,13 +25,13 @@ struct LinkedList<T> {
end: Option<NonNull<Node<T>>>,
}

impl<T> Default for LinkedList<T> {
impl<T: Clone + std::cmp::PartialOrd> Default for LinkedList<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> LinkedList<T> {
impl<T: Clone + std::cmp::PartialOrd> LinkedList<T> {
pub fn new() -> Self {
Self {
length: 0,
Expand Down Expand Up @@ -69,15 +65,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(list_a: LinkedList<T>, list_b: LinkedList<T>) -> Self {
let mut ret = LinkedList::new();
let mut a_ptr = list_a.start;
let mut b_ptr = list_b.start;
while let (Some(a_node), Some(b_node)) = (a_ptr, b_ptr) {
let a_val = unsafe { &(*a_node.as_ptr()).val };
let b_val = unsafe { &(*b_node.as_ptr()).val };

if a_val <= b_val {
ret.add(a_val.clone());
a_ptr = unsafe { (*a_node.as_ptr()).next };
} else {
ret.add(b_val.clone());
b_ptr = unsafe { (*b_node.as_ptr()).next };
}
}
}
while let Some(a_node) = a_ptr {
let a_val = unsafe { &(*a_node.as_ptr()).val };
ret.add(a_val.clone());
a_ptr = unsafe { (*a_node.as_ptr()).next };
}
while let Some(b_node) = b_ptr {
let b_val = unsafe { &(*b_node.as_ptr()).val };
ret.add(b_val.clone());
b_ptr = unsafe { (*b_node.as_ptr()).next };
}
ret
}
}

impl<T> Display for LinkedList<T>
Expand Down Expand Up @@ -130,44 +145,44 @@ mod tests {

#[test]
fn test_merge_linked_list_1() {
let mut list_a = LinkedList::<i32>::new();
let mut list_b = LinkedList::<i32>::new();
let vec_a = vec![1,3,5,7];
let vec_b = vec![2,4,6,8];
let target_vec = vec![1,2,3,4,5,6,7,8];
for i in 0..vec_a.len(){
list_a.add(vec_a[i]);
}
for i in 0..vec_b.len(){
list_b.add(vec_b[i]);
}
println!("list a {} list b {}", list_a,list_b);
let mut list_c = LinkedList::<i32>::merge(list_a,list_b);
println!("merged List is {}", list_c);
for i in 0..target_vec.len(){
assert_eq!(target_vec[i],*list_c.get(i as i32).unwrap());
}
}
#[test]
fn test_merge_linked_list_2() {
let mut list_a = LinkedList::<i32>::new();
let mut list_b = LinkedList::<i32>::new();
let vec_a = vec![11,33,44,88,89,90,100];
let vec_b = vec![1,22,30,45];
let target_vec = vec![1,11,22,30,33,44,45,88,89,90,100];

for i in 0..vec_a.len(){
list_a.add(vec_a[i]);
}
for i in 0..vec_b.len(){
list_b.add(vec_b[i]);
}
println!("list a {} list b {}", list_a,list_b);
let mut list_c = LinkedList::<i32>::merge(list_a,list_b);
println!("merged List is {}", list_c);
for i in 0..target_vec.len(){
assert_eq!(target_vec[i],*list_c.get(i as i32).unwrap());
}
}
}
let mut list_a = LinkedList::<i32>::new();
let mut list_b = LinkedList::<i32>::new();
let vec_a = vec![1, 3, 5, 7];
let vec_b = vec![2, 4, 6, 8];
let target_vec = vec![1, 2, 3, 4, 5, 6, 7, 8];

for i in 0..vec_a.len() {
list_a.add(vec_a[i]);
}
for i in 0..vec_b.len() {
list_b.add(vec_b[i]);
}
println!("list a {} list b {}", list_a, list_b);
let mut list_c = LinkedList::<i32>::merge(list_a, list_b);
println!("merged List is {}", list_c);
for i in 0..target_vec.len() {
assert_eq!(target_vec[i], *list_c.get(i as i32).unwrap());
}
}
#[test]
fn test_merge_linked_list_2() {
let mut list_a = LinkedList::<i32>::new();
let mut list_b = LinkedList::<i32>::new();
let vec_a = vec![11, 33, 44, 88, 89, 90, 100];
let vec_b = vec![1, 22, 30, 45];
let target_vec = vec![1, 11, 22, 30, 33, 44, 45, 88, 89, 90, 100];

for i in 0..vec_a.len() {
list_a.add(vec_a[i]);
}
for i in 0..vec_b.len() {
list_b.add(vec_b[i]);
}
println!("list a {} list b {}", list_a, list_b);
let mut list_c = LinkedList::<i32>::merge(list_a, list_b);
println!("merged List is {}", list_c);
for i in 0..target_vec.len() {
assert_eq!(target_vec[i], *list_c.get(i as i32).unwrap());
}
}
}
25 changes: 20 additions & 5 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
graph
This problem requires you to implement a basic graph functio
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE

use std::collections::{HashMap, HashSet};
use std::fmt;
Expand Down Expand Up @@ -30,6 +29,15 @@ impl Graph for UndirectedGraph {
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
let (node1, node2, w) = edge;
self.adjacency_table
.entry(node1.into())
.or_insert_with(Vec::new)
.push((node2.into(), w));
self.adjacency_table
.entry(node2.into())
.or_insert_with(Vec::new)
.push((node1.into(), w));
}
}
pub trait Graph {
Expand All @@ -38,10 +46,17 @@ pub trait Graph {
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool {
//TODO
true
if self.contains(node) {
false
} else {
self.nodes().insert(&node.to_string());
true
}
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
self.edges()
.push((&edge.0.to_string(), &edge.1.to_string(), edge.2))
}
fn contains(&self, node: &str) -> bool {
self.adjacency_table().get(node).is_some()
Expand Down Expand Up @@ -81,4 +96,4 @@ mod test_undirected_graph {
assert_eq!(graph.edges().contains(edge), true);
}
}
}
}
16 changes: 15 additions & 1 deletion exercises/algorithm/algorithm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
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 @@ -74,6 +73,21 @@ impl<T> LinkedList<T> {
}
pub fn reverse(&mut self){
// TODO
let mut current = self.start;
while let Some(mut current_ptr) = current {
unsafe {
// Swap the `next` and `prev` pointers of the current node
let temp = (*current_ptr.as_ptr()).next;
(*current_ptr.as_ptr()).next = (*current_ptr.as_ptr()).prev;
(*current_ptr.as_ptr()).prev = temp;

// Move to the next node (which was the previous node before swapping)
current = (*current_ptr.as_ptr()).prev;
}
}

// Swap the `start` and `end` pointers of the list
std::mem::swap(&mut self.start, &mut self.end);
}
}

Expand Down
25 changes: 15 additions & 10 deletions exercises/algorithm/algorithm3.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/*
sort
This problem requires you to implement a sorting algorithm
you can use bubble sorting, insertion sorting, heap sorting, etc.
sort
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]){
//TODO
fn sort<T: std::cmp::PartialOrd + Copy>(array: &mut [T]) {
let n = array.len();
for i in 0..n {
for j in 1..(n - i) {
if array[j] < array[j - 1] {
(array[j - 1], array[j]) = (array[j], array[j - 1]);
}
}
}
}
#[cfg(test)]
mod tests {
Expand All @@ -18,16 +23,16 @@ mod tests {
sort(&mut vec);
assert_eq!(vec, vec![19, 37, 46, 57, 64, 73, 75, 91]);
}
#[test]
#[test]
fn test_sort_2() {
let mut vec = vec![1];
sort(&mut vec);
assert_eq!(vec, vec![1]);
}
#[test]
#[test]
fn test_sort_3() {
let mut vec = vec![99, 88, 77, 66, 55, 44, 33, 22, 11];
sort(&mut vec);
assert_eq!(vec, vec![11, 22, 33, 44, 55, 66, 77, 88, 99]);
}
}
}
55 changes: 52 additions & 3 deletions exercises/algorithm/algorithm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
binary_search tree
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 @@ -51,12 +49,21 @@ where
// Insert a value into the BST
fn insert(&mut self, value: T) {
//TODO
if let Some(ref mut root) = self.root {
root.insert(value);
} else {
self.root = Some(Box::new(TreeNode::new(value)));
}
}

// Search for a value in the BST
fn search(&self, value: T) -> bool {
//TODO
true
if let Some(ref root) = self.root {
root.search(value)
} else {
false
}
}
}

Expand All @@ -67,6 +74,48 @@ where
// Insert a node into the tree
fn insert(&mut self, value: T) {
//TODO
match value.cmp(&self.value) {
Ordering::Less => {
if let Some(ref mut left) = self.left {
left.insert(value);
} else {
self.left = Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Greater => {
if let Some(ref mut right) = self.right {
right.insert(value);
} else {
self.right = Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Equal => {
// Duplicate values are not allowed in this implementation
// You can handle duplicates differently if needed
}
}
}

fn search(&self, value: T) -> bool {
match value.cmp(&self.value) {
Ordering::Less => {
if let Some(ref left) = self.left {
left.search(value)
} else {
false
}
},
Ordering::Greater => {
if let Some(ref right) = self.right {
right.search(value)
} else {
false
}
},
Ordering::Equal => {
true
}
}
}
}

Expand Down
Loading

0 comments on commit 91b332e

Please sign in to comment.