Codewars: Rust workspace for solving the 'Printer Errors' kata
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

3 years ago
3 years ago
  1. fn main() {
  2. println!();
  3. let mut str = ('a'..='z').collect::<String>();
  4. let bad = ('n'..='z').collect::<String>();
  5. let rng: Vec<_> = ('n'..='z').collect();
  6. let err: &[_] = &&rng;
  7. let sls = "";
  8. let st2 = "kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz";
  9. println!("{:?}\t{:?}\t{:?}\t{:?}",
  10. func(&str),
  11. &str.trim_matches(err),
  12. str.len(),
  13. sls
  14. );
  15. println!("\n{:?}\n{:?}\t{:?}:{:?}",
  16. st2,
  17. st2.chars().filter(|&c| err.contains(&c)).collect::<String>(),
  18. st2.chars().filter(|&c| "nopqrstuvwxyz".contains(c)).count(),
  19. st2.len()
  20. );
  21. println!("\n{:?}\n{:?}\t{:?}:{:?}",
  22. st2,
  23. st2.chars().filter(|&c| c > 'm').collect::<String>(),
  24. st2.chars().filter(|&c| c > 'm').count(),
  25. st2.len()
  26. );
  27. println!("\n{:?}\t{:?}",
  28. str,
  29. str.chars().filter(|&c| err.contains(&c)).collect::<String>(),
  30. );
  31. println!();
  32. }
  33. fn func(s: &str) -> String {
  34. s.into()
  35. }
  36. fn printer_error(s: &str) -> String {
  37. format!("{}/{}",
  38. s.chars().filter(|&c| "nopqrstuvwxyz".contains(c)).count(),
  39. s.len()
  40. )
  41. }
  42. #[cfg(test)]
  43. mod tests {
  44. use super::*;
  45. #[test]
  46. fn should_pass_all_the_tests_provided() {
  47. assert_eq!(&printer_error("aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"), "3/56");
  48. assert_eq!(&printer_error("kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"), "6/60");
  49. assert_eq!(&printer_error("kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyzuuuuu"), "11/65");
  50. }
  51. }