Ad
Puzzles
Regular Expressions
Code
Diff
  • use regex::Regex;
    use chrono::prelude::*;
    
    fn validate_password(password: &str) -> bool {
        let mut regexps = vec![
            r"^.{17}|.{19}|.{23}|.{29}|.{31}|.{37}$",   // The maximum length of the password is 40 characters, 
                                                        // The minimum length of the password is 15 characters
                                                        // The length of the password must be a prime number
            r"[a-z]",                                   // Must at least 1 lower case 
            r"[A-Z].*[A-Z].*[A-Z]",                     // and 3 upper case letter,
            r"[\d]",                                    // 1 number,
            r"[^\w^\s]",                                // 1 special character
            r"jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec", //Must have the diminituve of a month
        ];
        let date = Local::now().format("%Y%m%d").to_string(); //Must have today's date
        regexps.push(&date);
        let len = password.len().to_string();                 //The length of the password must be inside the password
        regexps.push(&len);
        
        let not_match = Regex::new(r"[^\w^\s]{2}").unwrap(); //2 special characters can't be together
        
        !not_match.is_match(password) && regexps.iter().all(|r|Regex::new(r).unwrap().is_match(password))
    }
    • public class PasswordValidation {
    • public static boolean validatePassword(String password) {
    • return false;
    • }
    • use regex::Regex;
    • use chrono::prelude::*;
    • fn validate_password(password: &str) -> bool {
    • let mut regexps = vec![
    • r"^.{17}|.{19}|.{23}|.{29}|.{31}|.{37}$", // The maximum length of the password is 40 characters,
    • // The minimum length of the password is 15 characters
    • // The length of the password must be a prime number
    • r"[a-z]", // Must at least 1 lower case
    • r"[A-Z].*[A-Z].*[A-Z]", // and 3 upper case letter,
    • r"[\d]", // 1 number,
    • r"[^\w^\s]", // 1 special character
    • r"jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec", //Must have the diminituve of a month
    • ];
    • let date = Local::now().format("%Y%m%d").to_string(); //Must have today's date
    • regexps.push(&date);
    • let len = password.len().to_string(); //The length of the password must be inside the password
    • regexps.push(&len);
    • let not_match = Regex::new(r"[^\w^\s]{2}").unwrap(); //2 special characters can't be together
    • !not_match.is_match(password) && regexps.iter().all(|r|Regex::new(r).unwrap().is_match(password))
    • }
Code
Diff
  • fn reverse(s: &str) -> String {
        s.chars().rev().collect()
    }
    • const reverse = str => str.split('').reverse();
    • fn reverse(s: &str) -> String {
    • s.chars().rev().collect()
    • }
Code
Diff
  • //who needs dyn for that?
    enum Animal {
        Dog,
        Cat,
        Rat,
    }
    
    impl Animal {
        //moved from create_animal
        fn new(animal_type: &str) -> Animal {
            match animal_type {
                "dog" => Animal::Dog,
                "cat" => Animal::Cat,
                "rat" => Animal::Rat,
                _ => panic!("Unknown animal {animal_type}"),
            }
        }
        
        fn speak(&self) -> &'static str {
            match self {
                Animal::Dog => "Woof!",
                Animal::Cat => "Meow!",
                _ => unimplemented!(),
            }
        }
    }
    
    //next: implement Result to avoid panicing
    • trait Animal {
    • fn speak(&self) -> &'static str {
    • unimplemented!();
    • }
    • //who needs dyn for that?
    • enum Animal {
    • Dog,
    • Cat,
    • Rat,
    • }
    • struct Dog;
    • impl Animal for Dog {
    • fn speak(&self) -> &'static str {
    • "Woof!"
    • impl Animal {
    • //moved from create_animal
    • fn new(animal_type: &str) -> Animal {
    • match animal_type {
    • "dog" => Animal::Dog,
    • "cat" => Animal::Cat,
    • "rat" => Animal::Rat,
    • _ => panic!("Unknown animal {animal_type}"),
    • }
    • }
    • }
    • struct Cat;
    • impl Animal for Cat {
    • fn speak(&self) -> &'static str {
    • "Meow!"
    • match self {
    • Animal::Dog => "Woof!",
    • Animal::Cat => "Meow!",
    • _ => unimplemented!(),
    • }
    • }
    • }
    • struct Rat;
    • impl Animal for Rat {}
    • fn create_animal(animal_type: &str) -> Box<dyn Animal> {
    • match animal_type {
    • "dog" => Box::new(Dog),
    • "cat" => Box::new(Cat),
    • "rat" => Box::new(Rat),
    • _ => panic!()
    • }
    • }
    • //next: implement Result to avoid panicing