Skip to content

Instantly share code, notes, and snippets.

@DogPawHat
Created May 30, 2022 16:04
Show Gist options
  • Select an option

  • Save DogPawHat/4f31534b8065efa67136c0d37230547d to your computer and use it in GitHub Desktop.

Select an option

Save DogPawHat/4f31534b8065efa67136c0d37230547d to your computer and use it in GitHub Desktop.
all_unique for cassidoo
use std::env;
use std::collections::HashSet;
fn all_unique(maybe_unique: String, ignore_caps: bool) -> bool {
return maybe_unique.len()
== (if ignore_caps {
maybe_unique.to_lowercase()
} else {
maybe_unique
})
.chars()
.collect::<HashSet<char>>()
.len();
}
fn main() {
let test_str: String = env::args().next().unwrap();
let res = all_unique(test_str, false);
println!("{:?}", &res);
}
#[cfg(test)]
mod tests {
use crate::all_unique;
#[test]
fn test_cassidy() {
assert_eq!(all_unique(String::from("Cassidy"), false), false);
}
#[test]
fn test_cat_dog_amp() {
assert_eq!(all_unique(String::from("cat & dog"), false), false);
}
#[test]
fn test_cat_dog_space() {
assert_eq!(all_unique(String::from("cat+dog"), false), true);
}
#[test]
fn test_cat_cat_no_caps() {
assert_eq!(all_unique(String::from("cat+cat"), false), false);
}
#[test]
fn test_cat_cat_caps() {
assert_eq!(all_unique(String::from("cat+CAT"), false), true);
}
#[test]
fn test_cat_cat_caps_pass() {
assert_eq!(all_unique(String::from("cat+CAT"), true), false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment