completed longest substring and palindromic number
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/input/3_test1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/3_test1
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
abcabcbb
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/3_test2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/3_test2
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
bbbbb
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/3_test3
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/3_test3
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
dvdf
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/9_test1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/9_test1
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
121
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/9_test2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/9_test2
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
-121
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/9_test3
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/9_test3
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
10
 | 
			
		||||
@@ -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 {}),
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										91
									
								
								src/solutions/longest_substring.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								src/solutions/longest_substring.rs
									
									
									
									
									
										Normal 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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										80
									
								
								src/solutions/palidromic_number.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/solutions/palidromic_number.rs
									
									
									
									
									
										Normal 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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user