Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Copy, Clone, Debug, PartialEq)]
- pub enum StartingCase {
- Lowercase,
- Uppercase,
- }
- #[derive(Copy, Clone, Debug, PartialEq)]
- pub enum IsSarcasm {
- Yes(StartingCase),
- No,
- TooShort,
- }
- fn is_sarcasm(input: &str) -> IsSarcasm {
- fn iter_is_sarcasm(mut chars: impl Iterator<Item=char>) -> IsSarcasm {
- let mut chars_chunk = (&mut chars)
- .skip_while(|c| !c.is_alphabetic())
- .take_while(|c| c.is_alphabetic());
- match chars_chunk.next() {
- Some(first_char) => {
- let first_char_uppercase = first_char.is_uppercase();
- let mod_bit = match first_char_uppercase {
- true => 1,
- false => 0,
- };
- dbg!(first_char_uppercase);
- dbg!(mod_bit);
- let is_sarcasm = chars_chunk
- .enumerate()
- .all(|(i, c)| {
- dbg!((i, c));
- dbg!((i % 2 == 1));
- dbg!(((i % 2 == 1) ^ first_char_uppercase));
- dbg!((((i % 2 == 1) ^ first_char_uppercase) == c.is_lowercase()));
- ((i % 2 == 1) ^ first_char_uppercase) == c.is_lowercase()
- });
- let is_sarcasm = is_sarcasm && match iter_is_sarcasm(chars) {
- IsSarcasm::Yes(_) => true,
- IsSarcasm::TooShort => true,
- _ => false,
- };
- match is_sarcasm {
- true => IsSarcasm::Yes(match first_char_uppercase {
- true => StartingCase::Uppercase,
- false => StartingCase::Lowercase,
- }),
- false => IsSarcasm::No,
- }
- },
- None => IsSarcasm::TooShort,
- }
- }
- iter_is_sarcasm(input.chars())
- }
- fn main() {
- assert_eq!(
- crate::is_sarcasm("HeLlO! hElLo!"),
- IsSarcasm::Yes(StartingCase::Uppercase)
- );
- assert_eq!(
- crate::is_sarcasm("hElLo! HeLlO!"),
- IsSarcasm::Yes(StartingCase::Lowercase)
- );
- assert_eq!(
- crate::is_sarcasm("HeLlO! HeLlO!"),
- IsSarcasm::Yes(StartingCase::Uppercase)
- );
- assert_eq!(
- crate::is_sarcasm("hElLo! hElLo!"),
- IsSarcasm::Yes(StartingCase::Lowercase)
- );
- assert_eq!(crate::is_sarcasm(""), IsSarcasm::TooShort);
- assert_eq!(crate::is_sarcasm("Not Sarcasm"), IsSarcasm::No);
- assert_eq!(crate::is_sarcasm("NoT SaRcASM"), IsSarcasm::No);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement