Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.65 KB | None | 0 0
  1. fn main() {
  2.     println!("Hello, world!");
  3. }
  4.  
  5. fn justify_words(line: String, length: usize) -> String {
  6.     let cloned_line = line.clone();
  7.     let words = cloned_line.split_whitespace();
  8.     let mut final_line = String::new();
  9.     let mut current_len = 0;
  10.  
  11.     // println!("-- {:?}", line.split_whitespace().collect::<Vec<&str>>());
  12.     for word in words {
  13.         if current_len + word.len() < length {
  14.             // Add it
  15.             if final_line == "" {
  16.                 current_len += word.len();
  17.                 final_line = word.to_owned();
  18.             } else {
  19.                 final_line = format!("{} {}", final_line, word);
  20.                 current_len += word.len() + 1;
  21.             }
  22.         } else {
  23.             //add \n
  24.             final_line = format!("{}\n{}", final_line, word);
  25.             current_len = word.len();
  26.         }
  27.     }
  28.  
  29.     final_line
  30. }
  31.  
  32. #[cfg(test)]
  33. mod test {
  34.     use super::*;
  35.     #[test]
  36.     fn test_justify_words() {
  37.         let resp = String::from(
  38.             r#"here is another
  39. example for
  40. consensys
  41. livecoding for
  42. the ethereum
  43. blockchain"#,
  44.         );
  45.         assert_eq!(
  46.             justify_words(
  47.                 String::from(
  48.                     "here is another example for consensys livecoding for the ethereum blockchain"
  49.                 ),
  50.                 15
  51.             ),
  52.             resp
  53.         );
  54.  
  55.         let resp = String::from(
  56.             r#"here
  57. is my
  58. other
  59. text"#,
  60.         );
  61.         assert_eq!(
  62.             justify_words(String::from("here is my other text"), 5),
  63.             resp
  64.         );
  65.  
  66.         let resp = String::from(
  67.             r#"xxx
  68. xxxxx
  69. yyyyy
  70. dsf-dsf
  71. kkkk
  72. dddd
  73. ssssss"#,
  74.         );
  75.         assert_eq!(
  76.             justify_words(String::from("xxx xxxxx yyyyy dsf-dsf kkkk dddd ssssss"), 7),
  77.             resp
  78.         );
  79.  
  80.         let resp = String::from(
  81.             r#"here is another
  82. example for
  83. consensys
  84. livecoding for
  85. the ethereum
  86. blockchain"#,
  87.         );
  88.         assert_eq!(
  89.             justify_words(
  90.                 String::from(
  91.                     r#"here is another
  92. example for consensys livecoding
  93. for the ethereum blockchain"#
  94.                 ),
  95.                 15
  96.             ),
  97.             resp
  98.         );
  99.  
  100.         let resp = String::from(
  101.             r#"Hi William, what a
  102. great news ! This
  103. week for the culture
  104. interview I can be
  105. available this
  106. tuesday or friday
  107. after 6:00pm CET.
  108. Let me know if it
  109. could fit with your
  110. schedule ?"#,
  111.         );
  112.         assert_eq!(
  113.             justify_words(
  114.                 String::from(
  115.                     r#"Hi William, what a great news ! This week for the culture interview I can be available this tuesday or friday after 6:00pm CET. Let me know if it could fit with your schedule ?"#
  116.                 ),
  117.                 20
  118.             ),
  119.             resp
  120.         );
  121.  
  122.         let resp = String::from(
  123.             "here is another example for consensys livecoding for the ethereum blockchain",
  124.         );
  125.         assert_eq!(
  126.             justify_words(
  127.                 String::from(
  128.                     "here is another example for consensys livecoding for the ethereum blockchain"
  129.                 ),
  130.                 100
  131.             ),
  132.             resp
  133.         );
  134.  
  135.         let resp = String::from(
  136.             "here is another example for consensys livecoding for the ethereum blockchain",
  137.         );
  138.         assert_eq!(
  139.             justify_words(
  140.                 String::from(
  141.                     "here is another example for consensys livecoding for the ethereum blockchain"
  142.                 ),
  143.                 100
  144.             ),
  145.             resp
  146.         );
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement