Last active
June 21, 2024 23:06
-
-
Save chase-lambert/21a8eace62c868eaab8a224edd5da187 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 24.05.21
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
(ns fix-inverted-punc | |
(:require [clojure.test :refer [deftest is])) | |
(defn fix-inverted-punc [s] | |
(let [sentences (->> (re-seq #".+?[.!?]" s) | |
(map str/trim))] | |
(->> sentences | |
(map (fn [sentence] | |
(let [first-char (first sentence) | |
last-char (last sentence)] | |
(cond | |
(#{\¡ \¿} first-char) sentence | |
(= \! last-char) (str "¡" sentence) | |
(= \? last-char) (str "¿" sentence) | |
:else sentence)))) | |
(str/join " ")))) | |
(deftest fix-inverted-punc-test | |
(is (= (fix-inverted-punc "Feliz cumpleaños!") | |
"¡Feliz cumpleaños!")) | |
(is (= (fix-inverted-punc "Ella ya se graduó de la universidad? ¡No!") | |
"¿Ella ya se graduó de la universidad? ¡No!"))) |
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 fix_inverted_punc(s: &str) -> String { | |
let sentence_endings = ['.', '!', '?']; | |
let sentences: Vec<&str> = s | |
.split_inclusive(&sentence_endings) | |
.map(|s| s.trim()) | |
.collect(); | |
let new_sentences: Vec<String> = sentences | |
.iter() | |
.map(|sentence| { | |
let mut chars = sentence.chars(); | |
let first_char = chars.next().unwrap(); | |
if first_char == '¡' || first_char == '¿' { | |
sentence.to_string() | |
} else if let Some(last_char) = chars.next_back() { | |
let new_first = match last_char { | |
'!' => '¡', | |
'?' => '¿', | |
_ => last_char, | |
}; | |
format!("{}{}", new_first, sentence) | |
} else { | |
sentence.to_string() | |
} | |
}) | |
.collect(); | |
new_sentences.join(" ") | |
} | |
#[test] | |
fn fix_inverted_punc_test() { | |
let s1 = "Feliz cumpleaños!"; | |
let s2 = "Ella ya se graduó de la universidad? ¡No!"; | |
assert_eq!(fix_inverted_punc(s1), "¡Feliz cumpleaños!"); | |
assert_eq!( | |
fix_inverted_punc(s2), | |
"¿Ella ya se graduó de la universidad? ¡No!" | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment