Sha256: 54523733fe566399d65b138d693888b2923803f4622fde39e32aa9244616bfa8

Contents?: true

Size: 1.32 KB

Versions: 329

Compression:

Stored size: 1.32 KB

Contents

module Bookkeeping
  VERSION = 1
end
class Diamond
  def self.make_diamond(letter)
    script = ""
    have_reached_middle = false
    space_from_middle = 0
    total_lines = ((letter.ord - 64) * 2 - 1)
    middle = total_lines/2
    letter_char_code = 65
    (0..total_lines - 1).each do |i|
      line = Array.new(total_lines, " ")                      # creates a line in each loop
      if i == 0 || i == total_lines - 1                      # placing the letter A in the beginning and end
        line[middle] = "A"
      else
        first_repeated_letter = middle - space_from_middle   # find position and place letters in positions
        second_repeated_letter = middle + space_from_middle
        line[first_repeated_letter] = letter_char_code.chr
        line[second_repeated_letter] = letter_char_code.chr
      end
      script += line.join("") + "\n"
      if i == middle                    #check if reached middle so it can start decrementing position of letters
        have_reached_middle = true
      end
      if !have_reached_middle
        space_from_middle += 1
        letter_char_code += 1
      else
        space_from_middle -= 1
        letter_char_code -= 1
      end
    end                       #end loop
    # puts script             #shows the outputted diamond
    script
  end                          #end method
end

Version data entries

329 entries across 329 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.138 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.137 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.136 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.135 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.134 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.133 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.132 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.131 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.130 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.129 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.128 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.127 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.126 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.125 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.124 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.123 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.122 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.121 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb
trackler-2.2.1.120 tracks/ruby/exercises/diamond/.meta/solutions/diamond.rb