Last active
May 28, 2022 23:24
-
-
Save hungneox/acf17666ca6259387af615986f5c707d to your computer and use it in GitHub Desktop.
Rust String concat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
fn is_anagram(word: &str, candidate: &str) -> bool { | |
if word.to_lowercase() == candidate.to_lowercase() { | |
return false; | |
} | |
let mut a = word.to_lowercase().chars().collect::<Vec<char>>(); | |
let mut b = candidate.to_lowercase().chars().collect::<Vec<char>>(); | |
a.sort(); | |
b.sort(); | |
return a.into_iter().collect::<String>() == b.into_iter().collect::<String>(); | |
} | |
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> { | |
let mut result: HashSet<&str> = HashSet::new(); | |
for i in 0..possible_anagrams.len() { | |
if is_anagram(word, possible_anagrams[i]) { | |
result.insert(possible_anagrams[i]); | |
} | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
fn sorted(word: &str) -> Vec<char> { | |
let mut word_chars = word.to_lowercase().chars().collect::<Vec<char>>(); | |
word_chars.sort(); | |
word_chars | |
} | |
fn is_anagram(word: &str, candidate: &str) -> bool { | |
if word.to_lowercase() == candidate.to_lowercase() { | |
return false; | |
} | |
let mut a = sorted(&word); | |
let mut b = sorted(&candidate); | |
return a.eq(&b) | |
} | |
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> { | |
let mut result: HashSet<&str> = HashSet::new(); | |
for i in 0..possible_anagrams.len() { | |
if is_anagram(word, possible_anagrams[i]) { | |
result.insert(possible_anagrams[i]); | |
} | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
pub fn anagrams_for<'a>(word: &'a str, possible_anagrams: &'a [&str]) -> HashSet<&'a str> { | |
let word = word.to_lowercase(); | |
let word_chars = sorted(&word); | |
possible_anagrams | |
.iter() | |
.filter(|predicate| { | |
let predicate = predicate.to_lowercase(); | |
if predicate.to_lowercase() == word { | |
return false; | |
} else { | |
let possible_anagram_chars = sorted(&predicate); | |
word_chars.eq(&possible_anagram_chars) | |
} | |
}) | |
.cloned() | |
.collect() | |
} | |
fn sorted(word: &str) -> Vec<char> { | |
let mut word_chars = word.chars().collect::<Vec<char>>(); | |
word_chars.sort(); | |
word_chars | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn reply(message: &str) -> &str { | |
if message.trim() == "" { | |
return "Fine. Be that way!"; | |
} | |
let msg = message.replace(&[')','(',':',',', '1', '2', '3', '4', '5','6','7','8','9','0'], ""); | |
let last_char = message.trim().chars().last().unwrap(); | |
if msg.to_uppercase() == msg && msg.trim().len() > 1 && last_char == "?".chars().last().unwrap() { | |
return "Calm down, I know what I'm doing!"; | |
} | |
if msg.to_uppercase() == msg && msg.trim() != "" && msg.trim() != "?" { | |
return "Whoa, chill out!" | |
} | |
if last_char == '?' { | |
return "Sure."; | |
} | |
return "Whatever."; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn raindrops(n: u32) -> String { | |
let mut result = String::from(""); | |
if n % 3 == 0 { | |
result = "Pling".to_string(); | |
} | |
if n % 5 == 0 { | |
result += &String::from("Plang"); | |
} | |
if n % 7 == 0 { | |
result += &String::from("Plong"); | |
} | |
if(n % 3 !=0 && n % 5 != 0 && n%7!=0) { | |
result = n.to_string(); | |
} | |
return String::from(result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn raindrops(n: u32) -> String { | |
let mut result = "".to_string(); | |
if n % 3 == 0 { | |
result = "Pling".to_string(); | |
} | |
if n % 5 == 0 { | |
result.push_str("Plang"); | |
} | |
if n % 7 == 0 { | |
result.push_str("Plong"); | |
} | |
if(n % 3 !=0 && n % 5 != 0 && n%7!=0) { | |
result = n.to_string(); | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment