Puzzles
Regular Expressions
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))
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_add() { assert!(!validate_password(&"abcdedfghijlkmn")); let password = Local::now().format("%Y%m%d").to_string() + &"17jan#NAN"; assert!(validate_password(&password)); } }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- #[cfg(test)]
- mod tests {
- use super::*;
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testvoid testSomething() {// assertEquals("expected", "actual");- #[test]
- fn test_add() {
- assert!(!validate_password(&"abcdedfghijlkmn"));
- let password = Local::now().format("%Y%m%d").to_string() + &"17jan#NAN";
- assert!(validate_password(&password));
- }
- }
fn reverse(s: &str) -> String { s.chars().rev().collect() }
const reverse = str => str.split('').reverse();- fn reverse(s: &str) -> String {
- s.chars().rev().collect()
- }
// Add your tests here. // See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html #[cfg(test)] mod tests { use super::*; #[test] fn test_add() { assert_eq!(reverse(""), ""); assert_eq!(reverse("abc"), "cba"); assert_eq!(reverse("ccabc"), "cbacc"); assert_eq!(reverse("Reverse strings"), "sgnirts esreveR"); assert_eq!(reverse("Codewars"), "srawedoC"); } }
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.const chai = require("chai");const assert = chai.assert;// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- // Add your tests here.
- // See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);});});- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_add() {
- assert_eq!(reverse(""), "");
- assert_eq!(reverse("abc"), "cba");
- assert_eq!(reverse("ccabc"), "cbacc");
- assert_eq!(reverse("Reverse strings"), "sgnirts esreveR");
- assert_eq!(reverse("Codewars"), "srawedoC");
- }
- }
//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
#[cfg(test)] mod tests { use super::*; #[test] fn test_implemented() { let dog = Animal::new("dog"); let cat = Animal::new("cat"); assert_eq!(dog.speak(), "Woof!"); assert_eq!(cat.speak(), "Meow!"); } #[test] #[should_panic] fn test_unimplemented() { let rat = Animal::new("rat"); rat.speak(); } #[test] #[should_panic] fn test_wrong_input() { let kangaroo = Animal::new("kangaroo"); kangaroo.speak(); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_implemented() {
let dog = create_animal("dog");let cat = create_animal("cat");- let dog = Animal::new("dog");
- let cat = Animal::new("cat");
- assert_eq!(dog.speak(), "Woof!");
- assert_eq!(cat.speak(), "Meow!");
- }
- #[test]
- #[should_panic]
- fn test_unimplemented() {
let rat = create_animal("rat");- let rat = Animal::new("rat");
- rat.speak();
- }
- #[test]
- #[should_panic]
- fn test_wrong_input() {
- let kangaroo = Animal::new("kangaroo");
- kangaroo.speak();
- }
- }