DEV Community

loading...

Discussion on: Daily Challenge #47 - Alphabets

Collapse
5422m4n profile image
Sven Assmann

solved in rust made with tests first :)

pub fn alphabet_position(s: &str) -> String {
  s.to_lowercase()
    .chars()
    .filter(|x| x.is_alphabetic())
    .map(|x| -> u8 { x as u8 - 'a' as u8 + 1 })
    .map(|x| -> String { x.to_string() })
    .collect::<Vec<String>>()
    .join(" ")
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn it_should_relace_the_a_with_1() {
    let replaced = alphabet_position("a");
    assert_eq!(replaced, "1");
  }

  #[test]
  fn it_should_relace_the_capital_a_with_1() {
    let replaced = alphabet_position("A");
    assert_eq!(replaced, "1");
  }

  #[test]
  fn it_should_ignore_non_characters() {
    let replaced = alphabet_position("'a a. 2");
    assert_eq!(replaced, "1 1");
  }

  #[test]
  fn it_should_relace_the_sentence() {
    let replaced = alphabet_position("The sunset sets at twelve o' clock.");
    assert_eq!(
      replaced,
      "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
    );
  }
}
Forem Open with the Forem app