21 - Merge Two Sorted Lists (incomplete)

This commit is contained in:
Luke Else 2024-07-19 14:03:40 +01:00
parent 1835230516
commit ef6c2f5d52
8 changed files with 152 additions and 0 deletions

1
src/input/21_test1 Normal file
View File

@ -0,0 +1 @@
list1 = [1,2,4], list2 = [1,3,4]

1
src/input/21_test2 Normal file
View File

@ -0,0 +1 @@
list1 = [], list2 = []

1
src/input/21_test3 Normal file
View File

@ -0,0 +1 @@
list1 = [], list2 = [0]

View File

@ -9,6 +9,7 @@ use solutions::*;
async fn main() -> Result<(), Box<dyn Error>> {
let problems: Vec<Box<dyn Solution + Sync>> = vec![
Box::new(valid_parentheses::ValidParentheses {}),
Box::new(merge_sorted_lists::MergeSortedLists {}),
Box::new(xx::XX {}),
];

View File

@ -0,0 +1,112 @@
use super::Solution;
use crate::utils::linked_list::ListNode;
pub struct MergeSortedLists {}
impl Solution for MergeSortedLists {
fn solution(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
let lists = parse_input(input.first().unwrap());
Ok(Box::new(self.merge_two_lists(lists.0, lists.1)))
}
fn get_id(&self) -> u32 {
21
}
}
impl MergeSortedLists {
pub fn merge_two_lists(
&self,
list1: Option<Box<ListNode>>,
list2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
match (list1, list2) {
(Some(l1), None) => Some(l1),
(None, Some(l2)) => Some(l2),
(None, None) => None,
(Some(l1), Some(l2)) => match l1.val <= l2.val {
true => Some(Box::new(ListNode {
val: l1.val,
next: self.merge_two_lists(l1.next, Some(l2)),
})),
false => Some(Box::new(ListNode {
val: l2.val,
next: self.merge_two_lists(Some(l1), l2.next),
})),
},
}
}
}
fn parse_input(input: &str) -> (Option<Box<ListNode>>, Option<Box<ListNode>>) {
let parts: Vec<&str> = input.split(",").collect();
let list1_str = parts[0].split('=').nth(1).unwrap().trim();
let list2_str = parts[1].split('=').nth(1).unwrap().trim();
let list1 = ListNode::from_vec(ListNode::parse_list(list1_str));
let list2 = ListNode::from_vec(ListNode::parse_list(list2_str));
(list1, list2)
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
use crate::*;
#[test]
fn test1() {
let solution = merge_sorted_lists::MergeSortedLists {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test1)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "[1,1,2,3,4,4]");
}
#[test]
fn test2() {
let solution = merge_sorted_lists::MergeSortedLists {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test2)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "[]");
}
#[test]
fn test3() {
let solution = merge_sorted_lists::MergeSortedLists {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test3)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "[0]");
}
}

View File

@ -2,6 +2,7 @@ use crate::utils::{self, get_input};
use std::{error::Error, fmt::Display, time::SystemTime};
pub mod valid_parentheses;
pub mod merge_sorted_lists;
pub mod xx;
pub trait Solution {

33
src/utils/linked_list.rs Normal file
View File

@ -0,0 +1,33 @@
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
pub fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
pub fn from_vec(vec: Vec<i32>) -> Option<Box<Self>> {
let mut current = None;
for &v in vec.iter().rev() {
let mut new_node = Box::new(ListNode::new(v));
new_node.next = current;
current = Some(new_node);
}
current
}
pub fn parse_list(s: &str) -> Vec<i32> {
s.trim_matches(|c| c == '[' || c == ']')
.split(',')
.filter_map(|s| s.trim().parse().ok())
.collect()
}
}

View File

@ -1,3 +1,5 @@
pub mod linked_list;
use std::{
error::Error,
fs::File,