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
Binary file added ..gitignore.un~
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.rs~
*.rs.un~
*.swp
target/
**/*.rs.bk
Expand Down
18 changes: 18 additions & 0 deletions .gitignore~
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*.rs~
*.swp
target/
**/*.rs.bk
.DS_Store
*.pdb
exercises/clippy/Cargo.toml
exercises/clippy/Cargo.lock
rust-project.json
.idea
.vscode/*
!.vscode/extensions.json
*.iml
*.o
public/

# Local Netlify folder
.netlify
Binary file added exercises/.quiz2.rs.un~
Binary file not shown.
Binary file added exercises/.quiz3.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm1.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm10.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm2.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm3.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm4.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm5.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm6.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm7.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm8.rs.un~
Binary file not shown.
Binary file added exercises/algorithm/.algorithm9.rs.un~
Binary file not shown.
50 changes: 40 additions & 10 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
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,17 +68,48 @@ impl<T> LinkedList<T> {
},
}
}
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
{
//TODO
Self {
length: 0,
start: None,
end: None,
}

impl<T: PartialOrd + Clone> LinkedList<T> {
pub fn merge(list_a: LinkedList<T>, list_b: LinkedList<T>) -> Self {
let mut merged_list = LinkedList::<T>::new();

let mut curr_a = list_a.start;
let mut curr_b = list_b.start;

while curr_a.is_some() && curr_b.is_some() {
let val_a = unsafe { curr_a.as_ref().unwrap().as_ref().val.clone() };
let val_b = unsafe { curr_b.as_ref().unwrap().as_ref().val.clone() };

if val_a <= val_b {
merged_list.add(val_a);
curr_a = unsafe { curr_a.unwrap().as_ref().next };
} else {
merged_list.add(val_b);
curr_b = unsafe { curr_b.unwrap().as_ref().next };
}
}
}

// Append remaining elements from list_a if any
while let Some(node) = curr_a {
let val = unsafe { node.as_ref().val.clone() };
merged_list.add(val);
curr_a = unsafe { node.as_ref().next };
}

// Append remaining elements from list_b if any
while let Some(node) = curr_b {
let val = unsafe { node.as_ref().val.clone() };
merged_list.add(val);
curr_b = unsafe { node.as_ref().next };
}

merged_list
}
}



impl<T> Display for LinkedList<T>
where
T: Display,
Expand Down Expand Up @@ -170,4 +200,4 @@ mod tests {
assert_eq!(target_vec[i],*list_c.get(i as i32).unwrap());
}
}
}
}
203 changes: 203 additions & 0 deletions exercises/algorithm/algorithm1.rs~
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
single linked list merge
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
*/

use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
use std::vec::*;

#[derive(Debug)]
struct Node<T> {
val: T,
next: Option<NonNull<Node<T>>>,
}

impl<T> Node<T> {
fn new(t: T) -> Node<T> {
Node {
val: t,
next: None,
}
}
}
#[derive(Debug)]
struct LinkedList<T> {
length: u32,
start: Option<NonNull<Node<T>>>,
end: Option<NonNull<Node<T>>>,
}

impl<T> Default for LinkedList<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> LinkedList<T> {
pub fn new() -> Self {
Self {
length: 0,
start: None,
end: None,
}
}

pub fn add(&mut self, obj: T) {
let mut node = Box::new(Node::new(obj));
node.next = None;
let node_ptr = Some(unsafe { NonNull::new_unchecked(Box::into_raw(node)) });
match self.end {
None => self.start = node_ptr,
Some(end_ptr) => unsafe { (*end_ptr.as_ptr()).next = node_ptr },
}
self.end = node_ptr;
self.length += 1;
}

pub fn get(&mut self, index: i32) -> Option<&T> {
self.get_ith_node(self.start, index)
}

fn get_ith_node(&mut self, node: Option<NonNull<Node<T>>>, index: i32) -> Option<&T> {
match node {
None => None,
Some(next_ptr) => match index {
0 => Some(unsafe { &(*next_ptr.as_ptr()).val }),
_ => self.get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1),
},
}
}
}

impl<T: PartialOrd + Clone> LinkedList<T> {
pub fn merge(list_a: LinkedList<T>, list_b: LinkedList<T>) -> Self {
let mut merged_list = LinkedList::<T>::new();

let mut curr_a = list_a.start;
let mut curr_b = list_b.start;

while curr_a.is_some() && curr_b.is_some() {
let val_a = unsafe { curr_a.as_ref().unwrap().as_ref().val.clone() };
let val_b = unsafe { curr_b.as_ref().unwrap().as_ref().val.clone() };

if val_a <= val_b {
merged_list.add(val_a);
curr_a = unsafe { curr_a.unwrap().as_ref().next };
} else {
merged_list.add(val_b);
curr_b = unsafe { curr_b.unwrap().as_ref().next };
}
}

// Append remaining elements from list_a if any
while let Some(node) = curr_a {
let val = unsafe { node.as_ref().val.clone() };
merged_list.add(val);
curr_a = unsafe { node.as_ref().next };
}

// Append remaining elements from list_b if any
while let Some(node) = curr_b {
let val = unsafe { node.as_ref().val.clone() };
merged_list.add(val);
curr_b = unsafe { node.as_ref().next };
}

merged_list
}
}



impl<T> Display for LinkedList<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.start {
Some(node) => write!(f, "{}", unsafe { node.as_ref() }),
None => Ok(()),
}
}
}

impl<T> Display for Node<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.next {
Some(node) => write!(f, "{}, {}", self.val, unsafe { node.as_ref() }),
None => write!(f, "{}", self.val),
}
}
}

#[cfg(test)]
mod tests {
use super::LinkedList;

#[test]
fn create_numeric_list() {
let mut list = LinkedList::<i32>::new();
list.add(1);
list.add(2);
list.add(3);
println!("Linked List is {}", list);
assert_eq!(3, list.length);
}

#[test]
fn create_string_list() {
let mut list_str = LinkedList::<String>::new();
list_str.add("A".to_string());
list_str.add("B".to_string());
list_str.add("C".to_string());
println!("Linked List is {}", list_str);
assert_eq!(3, list_str.length);
}

#[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());
}
}
}
21 changes: 17 additions & 4 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE

use std::collections::{HashMap, HashSet};
use std::fmt;
#[derive(Debug, Clone)]
Expand All @@ -29,7 +27,22 @@ impl Graph for UndirectedGraph {
&self.adjacency_table
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
let (from_node, to_node, weight) = edge;

// Add both nodes if they don't exist
self.add_node(from_node);
self.add_node(to_node);

// Add edge to both nodes' adjacency lists
self.adjacency_table
.entry(from_node.to_string())
.and_modify(|neighbors| neighbors.push((to_node.to_string(), weight)))
.or_insert_with(|| vec![(to_node.to_string(), weight)]);

self.adjacency_table
.entry(to_node.to_string())
.and_modify(|neighbors| neighbors.push((from_node.to_string(), weight)))
.or_insert_with(|| vec![(from_node.to_string(), weight)]);
}
}
pub trait Graph {
Expand Down Expand Up @@ -81,4 +94,4 @@ mod test_undirected_graph {
assert_eq!(graph.edges().contains(edge), true);
}
}
}
}
Loading