Skip to content

Commit

Permalink
https://exercism.org/tracks/rust/exercises/acronym
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunjun committed Feb 28, 2024
1 parent dfb084d commit 1c23454
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rust/acronym/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
edition = "2021"
name = "acronym"
version = "1.7.0"

[dependencies]
19 changes: 19 additions & 0 deletions rust/acronym/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub fn abbreviate(phrase: &str) -> String {
// https://www.hackertouch.com/rust-split-string-on-multiple-delimiters.html
let s = phrase.to_string();
let v: Vec<_> = s.split([':', '-', '_', ' '].as_ref()).filter(|s| s.len() > 0).collect();
//v.iter().map(|w| w.chars().nth(0).unwrap().to_ascii_uppercase()).into_iter().collect::<String>()
/*
v.iter().map(|w| {
let uppercases = w.chars().filter(|c| c.is_ascii_uppercase());
if uppercases.clone().count() > 0 {
uppercases.into_iter().collect::<String>()
} else {
w.chars().nth(0).unwrap().to_ascii_uppercase().to_string()
}
}).into_iter().collect::<String>()
*/
v.iter().map(|w| {
w.chars().enumerate().filter(|&(i, c)| i == 0 || (w.chars().nth(i - 1).unwrap().is_ascii_uppercase() == false && c.is_ascii_uppercase())).map(|(_, c)| c).into_iter().collect::<String>().to_uppercase()
}).into_iter().collect::<String>()
}
79 changes: 79 additions & 0 deletions rust/acronym/tests/acronym.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#[test]
fn basic() {
let input = "Portable Network Graphics";
let output = acronym::abbreviate(input);
let expected = "PNG";
assert_eq!(output, expected);
}

#[test]
fn lowercase_words() {
let input = "Ruby on Rails";
let output = acronym::abbreviate(input);
let expected = "ROR";
assert_eq!(output, expected);
}

#[test]
fn punctuation() {
let input = "First In, First Out";
let output = acronym::abbreviate(input);
let expected = "FIFO";
assert_eq!(output, expected);
}

#[test]
fn all_caps_word() {
let input = "GNU Image Manipulation Program";
let output = acronym::abbreviate(input);
let expected = "GIMP";
assert_eq!(output, expected);
}

#[test]
fn punctuation_without_whitespace() {
let input = "Complementary metal-oxide semiconductor";
let output = acronym::abbreviate(input);
let expected = "CMOS";
assert_eq!(output, expected);
}

#[test]
fn very_long_abbreviation() {
let input = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me";
let output = acronym::abbreviate(input);
let expected = "ROTFLSHTMDCOALM";
assert_eq!(output, expected);
}

#[test]
fn consecutive_delimiters() {
let input = "Something - I made up from thin air";
let output = acronym::abbreviate(input);
let expected = "SIMUFTA";
assert_eq!(output, expected);
}

#[test]
fn apostrophes() {
let input = "Halley's Comet";
let output = acronym::abbreviate(input);
let expected = "HC";
assert_eq!(output, expected);
}

#[test]
fn underscore_emphasis() {
let input = "The Road _Not_ Taken";
let output = acronym::abbreviate(input);
let expected = "TRNT";
assert_eq!(output, expected);
}

#[test]
fn camelcase() {
let input = "HyperText Markup Language";
let output = acronym::abbreviate(input);
let expected = "HTML";
assert_eq!(output, expected);
}

0 comments on commit 1c23454

Please sign in to comment.