Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #[derive(Copy, Clone, Debug, PartialEq)]
  2. pub enum StartingCase {
  3. Lowercase,
  4. Uppercase,
  5. }
  6.  
  7. #[derive(Copy, Clone, Debug, PartialEq)]
  8. pub enum IsSarcasm {
  9. Yes(StartingCase),
  10. No,
  11. TooShort,
  12. }
  13.  
  14. fn is_sarcasm(input: &str) -> IsSarcasm {
  15. fn iter_is_sarcasm(mut chars: impl Iterator<Item=char>) -> IsSarcasm {
  16. let mut chars_chunk = (&mut chars)
  17. .skip_while(|c| !c.is_alphabetic())
  18. .take_while(|c| c.is_alphabetic());
  19.  
  20. match chars_chunk.next() {
  21. Some(first_char) => {
  22. let first_char_uppercase = first_char.is_uppercase();
  23.  
  24. let mod_bit = match first_char_uppercase {
  25. true => 1,
  26. false => 0,
  27. };
  28.  
  29. dbg!(first_char_uppercase);
  30. dbg!(mod_bit);
  31.  
  32. let is_sarcasm = chars_chunk
  33. .enumerate()
  34. .all(|(i, c)| {
  35. dbg!((i, c));
  36. dbg!((i % 2 == 1));
  37. dbg!(((i % 2 == 1) ^ first_char_uppercase));
  38. dbg!((((i % 2 == 1) ^ first_char_uppercase) == c.is_lowercase()));
  39.  
  40. ((i % 2 == 1) ^ first_char_uppercase) == c.is_lowercase()
  41. });
  42.  
  43. let is_sarcasm = is_sarcasm && match iter_is_sarcasm(chars) {
  44. IsSarcasm::Yes(_) => true,
  45. IsSarcasm::TooShort => true,
  46. _ => false,
  47. };
  48.  
  49. match is_sarcasm {
  50. true => IsSarcasm::Yes(match first_char_uppercase {
  51. true => StartingCase::Uppercase,
  52. false => StartingCase::Lowercase,
  53. }),
  54. false => IsSarcasm::No,
  55. }
  56. },
  57. None => IsSarcasm::TooShort,
  58. }
  59. }
  60. iter_is_sarcasm(input.chars())
  61. }
  62.  
  63. fn main() {
  64. assert_eq!(
  65. crate::is_sarcasm("HeLlO! hElLo!"),
  66. IsSarcasm::Yes(StartingCase::Uppercase)
  67. );
  68. assert_eq!(
  69. crate::is_sarcasm("hElLo! HeLlO!"),
  70. IsSarcasm::Yes(StartingCase::Lowercase)
  71. );
  72. assert_eq!(
  73. crate::is_sarcasm("HeLlO! HeLlO!"),
  74. IsSarcasm::Yes(StartingCase::Uppercase)
  75. );
  76. assert_eq!(
  77. crate::is_sarcasm("hElLo! hElLo!"),
  78. IsSarcasm::Yes(StartingCase::Lowercase)
  79. );
  80. assert_eq!(crate::is_sarcasm(""), IsSarcasm::TooShort);
  81. assert_eq!(crate::is_sarcasm("Not Sarcasm"), IsSarcasm::No);
  82. assert_eq!(crate::is_sarcasm("NoT SaRcASM"), IsSarcasm::No);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement