Решение на Шеста задача от Андрея Костов

Обратно към всички решения

Към профила на Андрея Костов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 11 успешни тест(а)
  • 3 неуспешни тест(а)

Код

module MatrixHelper
def empty_matrix(width, height, fill)
Array.new(width) { Array.new(height) { fill } }
end
end
module TurtleGraphics
class Turtle
include MatrixHelper
def initialize(width, height)
@width = width
@height = height
spawn_at(0, 0)
look(:right)
end
def draw(canvas = TurtleGraphics::Canvas::Default.new, &block)
instance_eval(&block) if block_given?
canvas.result(@matrix)
end
private
DIRECTIONS = { left: [-1, 0], up: [0, -1], right: [1, 0], down: [0, 1] }
def move
direction_mask = DIRECTIONS[@orientation]
@x = new_position(@x + direction_mask.first, @matrix[@y].length)
@y = new_position(@y + direction_mask.last, @matrix.length)
mark_position
end
def new_position(position, length)
if position >= length
0
elsif position < 0
length - 1
else
position
end
end
def turn_left
next_index = DIRECTIONS.keys.index(@orientation).pred
@orientation = DIRECTIONS.keys[next_index]
end
def turn_right
current_index = DIRECTIONS.keys.index(@orientation)
if current_index.next == DIRECTIONS.keys.length
@orientation = DIRECTIONS.keys[0]
else
@orientation = DIRECTIONS.keys[current_index.next]
end
end
def spawn_at(row, column)
@matrix = empty_matrix(@width, @height, 0)
@x = column
@y = row
mark_position
end
def look(orientation)
@orientation = orientation
end
def mark_position
@matrix[@y][@x] = @matrix[@y][@x].next
end
end
module Canvas
class Default
def result(matrix)
matrix
end
end
class ASCII
include MatrixHelper
def initialize(symbols)
symbols = symbols.dup
@symbols = {}
@zero_intensity_symbol = symbols.slice!(0)
step = 1.to_f / symbols.size
count = 0
while count < symbols.size
@symbols.store(((count * step)..((count + 1) * step)), symbols[count])
count = count.next
end
end
def result(matrix)
max_step = max_step(matrix)
if max_step == 0
empty_matrix(matrix.length, matrix[0].length, @zero_intensity_symbol)
else
generate_string(matrix, max_step)
end
end
private
def generate_string(matrix, max_step)
matrix.map { |row| row.map { |c| intensity(c.to_f / max_step) } }
.collect(&:join)
.join("\n")
end
def max_step(matrix)
matrix.max_by(&:max).max
end
def intensity(cell)
if cell == 0.0
@zero_intensity_symbol
else
@symbols.select { |range| range.include?(cell) }.values.first
end
end
end
end
end

Лог от изпълнението

...........FFF

Failures:

  1) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1ko002n/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ko002n/spec.rb:166:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1ko002n/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ko002n/spec.rb:219:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1ko002n/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ko002n/spec.rb:228:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01201 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20151203-5272-1ko002n/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1ko002n/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-1ko002n/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

История (1 версия и 0 коментара)

Андрея обнови решението на 02.12.2015 15:40 (преди около 9 години)

+module MatrixHelper
+ def empty_matrix(width, height, fill)
+ Array.new(width) { Array.new(height) { fill } }
+ end
+end
+
+module TurtleGraphics
+ class Turtle
+ include MatrixHelper
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ spawn_at(0, 0)
+ look(:right)
+ end
+
+ def draw(canvas = TurtleGraphics::Canvas::Default.new, &block)
+ instance_eval(&block) if block_given?
+ canvas.result(@matrix)
+ end
+
+ private
+
+ DIRECTIONS = { left: [-1, 0], up: [0, -1], right: [1, 0], down: [0, 1] }
+
+ def move
+ direction_mask = DIRECTIONS[@orientation]
+ @x = new_position(@x + direction_mask.first, @matrix[@y].length)
+ @y = new_position(@y + direction_mask.last, @matrix.length)
+ mark_position
+ end
+
+ def new_position(position, length)
+ if position >= length
+ 0
+ elsif position < 0
+ length - 1
+ else
+ position
+ end
+ end
+
+ def turn_left
+ next_index = DIRECTIONS.keys.index(@orientation).pred
+ @orientation = DIRECTIONS.keys[next_index]
+ end
+
+ def turn_right
+ current_index = DIRECTIONS.keys.index(@orientation)
+ if current_index.next == DIRECTIONS.keys.length
+ @orientation = DIRECTIONS.keys[0]
+ else
+ @orientation = DIRECTIONS.keys[current_index.next]
+ end
+ end
+
+ def spawn_at(row, column)
+ @matrix = empty_matrix(@width, @height, 0)
+ @x = column
+ @y = row
+ mark_position
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def mark_position
+ @matrix[@y][@x] = @matrix[@y][@x].next
+ end
+ end
+
+ module Canvas
+ class Default
+ def result(matrix)
+ matrix
+ end
+ end
+
+ class ASCII
+ include MatrixHelper
+
+ def initialize(symbols)
+ symbols = symbols.dup
+ @symbols = {}
+ @zero_intensity_symbol = symbols.slice!(0)
+ step = 1.to_f / symbols.size
+ count = 0
+ while count < symbols.size
+ @symbols.store(((count * step)..((count + 1) * step)), symbols[count])
+ count = count.next
+ end
+ end
+
+ def result(matrix)
+ max_step = max_step(matrix)
+ if max_step == 0
+ empty_matrix(matrix.length, matrix[0].length, @zero_intensity_symbol)
+ else
+ generate_string(matrix, max_step)
+ end
+ end
+
+ private
+
+ def generate_string(matrix, max_step)
+ matrix.map { |row| row.map { |c| intensity(c.to_f / max_step) } }
+ .collect(&:join)
+ .join("\n")
+ end
+
+ def max_step(matrix)
+ matrix.max_by(&:max).max
+ end
+
+ def intensity(cell)
+ if cell == 0.0
+ @zero_intensity_symbol
+ else
+ @symbols.select { |range| range.include?(cell) }.values.first
+ end
+ end
+ end
+ end
+end