Diamond

1. Readme

钻石

钻石 kata 将一个字母作为输入,并以菱形输出。给定一个字母,它会打印一个以’A’开头的钻石,并在最宽处提供所提供的字母。

要求

  • 第一行包含一个’A’.
  • 最后一行包含一个’A’.
  • 除第一行和最后一行之外的所有行都有两个完全相同的字母.
  • 所有行都具有与前导空格一样多的尾随空格.(这可能是 0).
  • 钻石是水平对称的.
  • 钻石是垂直对称的.
  • 钻石具有方形(宽度等于高度).
  • 字母形成菱形.
  • 上半部分的字母按升序排列.
  • 下半部分的字母按降序排列.
  • 四个角(包含空格)是三角形.

例子

在以下示例中,空格表示为·字符.

字母’A’的钻石:

A

字母’C’的钻石:

··A··
·B·B·
C···C
·B·B·
··A··

字母’E’的钻石:

····A····
···B·B···
··C···C··
·D·····D·
E·······E
·D·····D·
··C···C··
···B·B···
····A····

资源

Seb Rosehttp://claysnow.co.uk/recycling-tests-in-tdd/

2. 开始你的表演

pub fn get_diamond(c: char) -> Vec<String> {
   unimplemented!(
       "Return the vector of strings which represent the diamond with particular char {}",
       c
   );
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[test]
fn test_a() {
   assert_eq!(get_diamond('A'), vec!["A"]);
}

#[test]
//#[ignore]
fn test_b() {
   assert_eq!(get_diamond('B'), vec![" A ", "B B", " A "]);
}

#[test]
//#[ignore]
fn test_c() {
   assert_eq!(
       get_diamond('C'),
       vec!["  A  ", " B B ", "C   C", " B B ", "  A  "]
   );
}

#[test]
//#[ignore]
fn test_d() {
   assert_eq!(
       get_diamond('D'),
       vec!["   A   ", "  B B  ", " C   C ", "D     D", " C   C ", "  B B  ", "   A   ",]
   );
}

#[test]
//#[ignore]
fn test_e() {
   assert_eq!(
       get_diamond('Z'),
       vec![
           "                         A                         ",
           "                        B B                        ",
           "                       C   C                       ",
           "                      D     D                      ",
           "                     E       E                     ",
           "                    F         F                    ",
           "                   G           G                   ",
           "                  H             H                  ",
           "                 I               I                 ",
           "                J                 J                ",
           "               K                   K               ",
           "              L                     L              ",
           "             M                       M             ",
           "            N                         N            ",
           "           O                           O           ",
           "          P                             P          ",
           "         Q                               Q         ",
           "        R                                 R        ",
           "       S                                   S       ",
           "      T                                     T      ",
           "     U                                       U     ",
           "    V                                         V    ",
           "   W                                           W   ",
           "  X                                             X  ",
           " Y                                               Y ",
           "Z                                                 Z",
           " Y                                               Y ",
           "  X                                             X  ",
           "   W                                           W   ",
           "    V                                         V    ",
           "     U                                       U     ",
           "      T                                     T      ",
           "       S                                   S       ",
           "        R                                 R        ",
           "         Q                               Q         ",
           "          P                             P          ",
           "           O                           O           ",
           "            N                         N            ",
           "             M                       M             ",
           "              L                     L              ",
           "               K                   K               ",
           "                J                 J                ",
           "                 I               I                 ",
           "                  H             H                  ",
           "                   G           G                   ",
           "                    F         F                    ",
           "                     E       E                     ",
           "                      D     D                      ",
           "                       C   C                       ",
           "                        B B                        ",
           "                         A                         ",
       ]
   );
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
static ABC: &'static str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

pub fn get_diamond(diamond_char: char) -> Vec<String> {
   let mut result: Vec<String> = Vec::new();
   let diamond_char = diamond_char.to_ascii_uppercase();
   if ABC.find(diamond_char).is_none() {
       return result;
   }
   if diamond_char == 'A' {
       return vec![String::from("A")];
   }

   //build first half
   for char_in_abc in ABC.chars() {
       result.push(get_line(char_in_abc, diamond_char).clone());
       if char_in_abc == diamond_char {
           break;
       }
   }

   //build second half
   let mut rev = result.clone();
   rev.pop(); //remove middle piece to avoid duplicates
   for line in rev.drain(..).rev() {
       result.push(line);
   }

   result
}

fn get_line(char_in_abc: char, diamond_char: char) -> String {
   let mut r = String::new();
   let letter_e = get_letter_line(char_in_abc);
   let letter_c = get_letter_line(diamond_char);
   let ws = letter_c.len() - letter_e.len(); //number of whitespaces

   //left
   for _ in 0..ws / 2 {
       r.push(' ');
   }
   //letter line
   for i in letter_e.chars() {
       r.push(i)
   }
   //right
   for _ in 0..ws / 2 {
       r.push(' ');
   }
   r
}

fn get_letter_line(char_in_abc: char) -> String {
   let mut r = String::new();
   let odd = (0..)
       .filter(|x| x % 2 != 0)
       .nth(ABC.find(char_in_abc).unwrap())
       .unwrap();
   for i in 0..odd {
       if i == 0 || i == odd - 1 {
           r.push(char_in_abc);
       } else {
           r.push(' ');
       }
   }
   r
}

#}



填充/相关