Avatar of paulfioravanti

paulfioravanti's solution

to Diamond in the Ruby Track

Published at May 20 2019 · 0 comments
Instructions
Test suite
Solution

The diamond kata takes as its input a letter, and outputs it in a diamond shape. Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.

Requirements

  • The first row contains one 'A'.
  • The last row contains one 'A'.
  • All rows, except the first and last, have exactly two identical letters.
  • All rows have as many trailing spaces as leading spaces. (This might be 0).
  • The diamond is horizontally symmetric.
  • The diamond is vertically symmetric.
  • The diamond has a square shape (width equals height).
  • The letters form a diamond shape.
  • The top half has the letters in ascending order.
  • The bottom half has the letters in descending order.
  • The four corners (containing the spaces) are triangles.

Examples

In the following examples, spaces are indicated by · characters.

Diamond for letter 'A':

A

Diamond for letter 'C':

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

Diamond for letter 'E':

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

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby diamond_test.rb

To include color from the command line:

ruby -r minitest/pride diamond_test.rb

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

diamond_test.rb

require 'minitest/autorun'
require_relative 'diamond'

class DiamondTest < Minitest::Test
  def test_letter_a
    answer = Diamond.make_diamond('A')
    assert_equal "A\n", answer
  end

  def test_letter_c
    skip
    answer = Diamond.make_diamond('C')
    string = "  A  \n"\
             " B B \n"\
             "C   C\n"\
             " B B \n"\
             "  A  \n"
    assert_equal string, answer
  end

  def test_letter_e
    skip
    answer = Diamond.make_diamond('E')
    string = "    A    \n"\
             "   B B   \n"\
             "  C   C  \n"\
             " D     D \n"\
             "E       E\n"\
             " D     D \n"\
             "  C   C  \n"\
             "   B B   \n"\
             "    A    \n"
    assert_equal string, answer
  end
end
# frozen_string_literal: true

module Diamond
  DIAMOND_TIP = "A"
  private_constant :DIAMOND_TIP
  NEWLINE = "\n"
  private_constant :NEWLINE
  SPACE = " "
  private_constant :SPACE

  module_function

  def make_diamond(max_letter)
    letters = generate_letters(max_letter)
    max_outer_padding = length_from_center(max_letter)

    letters
      .each
      .with_object([letters.length, max_outer_padding])
      .each_with_object([], &method(:add_line))
      .join
  end

  def generate_letters(max_letter)
    (DIAMOND_TIP..max_letter).to_a + (DIAMOND_TIP...max_letter).to_a.reverse
  end
  private_class_method :generate_letters

  def length_from_center(letter)
    letter.ord - DIAMOND_TIP.ord
  end
  private_class_method :length_from_center

  def add_line((letter, (diamond_height, max_outer_padding)), acc)
    line =
      if letter == DIAMOND_TIP
        diamond_tip(letter, diamond_height)
      else
        diamond_row(letter, max_outer_padding)
      end
    acc << line + NEWLINE
  end
  private_class_method :add_line

  def diamond_tip(letter, diamond_height)
    letter.center(diamond_height)
  end
  private_class_method :diamond_tip

  def diamond_row(letter, max_outer_padding)
    inner_padding = length_from_center(letter)
    # Pad string spacing outwards from center first, then have outer
    # padding fill in the remaining gaps, if any.
    half_row =
      letter
      .ljust(inner_padding)
      .rjust(max_outer_padding)

    half_row + SPACE + half_row.reverse
  end
  private_class_method :diamond_row
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?