completed longest substring and palindromic number

This commit is contained in:
Luke Else 2024-09-10 10:45:58 +01:00
parent 5b09840a8f
commit d4897b5034
10 changed files with 181 additions and 0 deletions

1
src/input/3_test1 Normal file
View File

@ -0,0 +1 @@
abcabcbb

1
src/input/3_test2 Normal file
View File

@ -0,0 +1 @@
bbbbb

1
src/input/3_test3 Normal file
View File

@ -0,0 +1 @@
dvdf

1
src/input/9_test1 Normal file
View File

@ -0,0 +1 @@
121

1
src/input/9_test2 Normal file
View File

@ -0,0 +1 @@
-121

1
src/input/9_test3 Normal file
View File

@ -0,0 +1 @@
10

View File

@ -10,6 +10,8 @@ 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(longest_substring::LongestSubstring {}),
Box::new(palidromic_number::PalidromicNumber {}),
Box::new(xx::XX {}),
];

View File

@ -0,0 +1,91 @@
use super::Solution;
use std::collections::HashMap;
pub struct LongestSubstring {}
impl Solution for LongestSubstring {
fn solution(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
let mut hm = HashMap::new();
let mut largest = 0;
let mut lo = -1;
for s in input {
for (hi, ch) in s.chars().enumerate() {
if let Some(i) = hm.insert(ch, hi as i32) {
lo = lo.max(i);
}
largest = largest.max(hi as i32 - lo);
}
}
Ok(Box::new(largest))
}
fn get_id(&self) -> u32 {
3
}
}
impl LongestSubstring {}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
use crate::*;
#[test]
fn test1() {
let solution = longest_substring::LongestSubstring {};
//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, "3");
}
#[test]
fn test2() {
let solution = longest_substring::LongestSubstring {};
//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, "1");
}
#[test]
fn test3() {
let solution = longest_substring::LongestSubstring {};
//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, "3");
}
}

View File

@ -3,6 +3,8 @@ use std::{error::Error, fmt::Display, time::SystemTime};
pub mod valid_parentheses;
pub mod merge_sorted_lists;
pub mod longest_substring;
pub mod palidromic_number;
pub mod xx;
pub trait Solution {

View File

@ -0,0 +1,80 @@
use super::Solution;
pub struct PalidromicNumber {}
impl Solution for PalidromicNumber {
fn solution(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
let num: i32 = input.get(0).unwrap().parse::<i32>()?;
let num_str: String = format!("{}", num);
let rev: String = num_str.chars().rev().collect::<String>();
Ok(Box::new(num_str == rev))
}
fn get_id(&self) -> u32 {
9
}
}
impl PalidromicNumber {}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
use crate::*;
#[test]
fn test1() {
let solution = palidromic_number::PalidromicNumber {};
//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, "true");
}
#[test]
fn test2() {
let solution = palidromic_number::PalidromicNumber {};
//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, "false");
}
#[test]
fn test3() {
let solution = palidromic_number::PalidromicNumber {};
//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, "false");
}
}