Решение на Шеста задача от Георги Карапетров

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

Към профила на Георги Карапетров

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@pixel_map = Array.new(rows) { Array.new(columns) { 0 } }
@current_position = [0, 0]
@orientations = [:right, :down, :left, :up]
@orientation_index = 0
@directions = {
left: [0, -1], up: [-1, 0],
right: [0, 1], down: [1, 0]
}
mark_position_visited
end
def draw(canvas = nil, &block)
self.instance_eval(&block)
drawing = canvas.draw(@pixel_map) if canvas.respond_to? :draw
return drawing if drawing
@pixel_map
end
private
def turn_right
@orientation_index = (@orientation_index + 1) % @orientations.size
end
def turn_left
@orientation_index = (@orientation_index - 1) % @orientations.size
end
def orientation
@orientations[@orientation_index]
end
def move
from_row = @current_position.first
from_column = @current_position.last
to_row = from_row + @directions[orientation].first
to_column = from_column + @directions[orientation].last
to_row = 0 if to_row == @rows
to_column = 0 if to_column == @columns
@current_position = [to_row, to_column]
mark_position_visited
@current_position
end
def spawn_at(row, column)
@current_position = [row, column]
mark_position_visited
end
def look(direction)
@direction = direction
end
def mark_position_visited
row = @current_position.first
column = @current_position.last
@pixel_map[row][column] += 1
end
end #end of class Turtle
module Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
def draw(pixel_map)
highest_intensity = pixel_map.flatten.max
intensity_map = pixel_map.each do |row|
row.map! { |pixel| pixel.to_f / highest_intensity.to_f }
end
intensity_map.each do |row|
row.map! { |pixel| symbol_for_pixel(pixel) }
end
intensity_map
end
private
def symbol_for_pixel(pixel)
number_of_symbols = @symbols.size
approximate_index = (number_of_symbols - 1) * pixel
index = approximate_index.round
@symbols[index]
end
end #of class ASCII
class HTML
def initialize(cell_size)
@cell_size = cell_size
end
def draw(pixel_map)
html_document(pixel_map)
end
private
def html_document(pixel_map)
"<!DOCTYPE html>\n"\
"<html>\n"\
"#{html_head}"\
"#{html_body(pixel_map)}"\
"</html>"
end
def html_head
"<head>\n"\
" <title>Turtle graphics</title>\n\n"\
"#{html_style}\n"\
"</head>\n"
end
def html_style
" <style>\n"\
" table {\n"\
" border-spacing: 0;\n }\n\n"\
" tr {\n padding: 0;\n }\n\n"\
" td {\n"\
" width: #{@cell_size}px;\n"\
" height: #{@cell_size}px;\n\n"\
" background-color: black;\n"\
" padding: 0;\n }\n"\
" </style>"
end
def html_body(pixel_map)
"<body>\n"\
"#{construct_html_table(pixel_map)}"\
"</body>\n"
end
def html_cell(opacity)
" <td style=\"opacity: #{format('%.2f', opacity)}\"></td>"
end
def html_row(html_cells)
" <tr>\n"\
"#{html_cells.join("\n")}\n"\
" </tr>"
end
def html_table(html_rows)
" <table>\n"\
"#{html_rows.join("\n")}\n"\
" </table>\n"
end
def construct_html_table(pixel_map)
highest_intensity = pixel_map.flatten.max
intensity_map = pixel_map.each do |row|
row.map! { |pixel| pixel / highest_intensity }
end
intensity_map.each do |row|
row.map! { |pixel| html_cell(pixel) }
end
html_rows = intensity_map.map { |row| html_row(row) }
html_table(html_rows)
end
end #of class HTML
end #of module Canvas
end #of module TurtleGraphics

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

F......F.FF..F

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: expect(canvas).to eq [
       
       expected: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0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0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-miu2c6/spec.rb:284:in `block (2 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 Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: expect(canvas).to eq [[0, 0], [1, 0]]
       
       expected: [[0, 0], [1, 0]]
            got: [[1, 0], [1, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-miu2c6/spec.rb:93:in `block (5 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::ASCII renders the proper symbols depending on the intensity
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [["3", "2", "0"], ["1", "1", "0"], ["0", "0", "0"]]:Array
     # /tmp/d20151203-5272-miu2c6/spec.rb:128: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)>'

  4) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [["t", "o", "z"], ["o", "o", "z"], ["z", "z", "z"]]:Array
     # /tmp/d20151203-5272-miu2c6/spec.rb:151: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)>'

  5) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
       expected "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table></body></html>" to include "<table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:0.67\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table>"
     # /tmp/d20151203-5272-miu2c6/spec.rb:242: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.01341 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20151203-5272-miu2c6/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-miu2c6/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-miu2c6/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-miu2c6/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-miu2c6/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Георги обнови решението на 02.12.2015 02:23 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @pixel_map = Array.new(rows) { Array.new(columns) { 0 } }
+ @current_position = [0, 0]
+ @orientations = [:right, :down, :left, :up]
+ @orientation_index = 0
+ @directions = {:left => [0, -1], :up => [-1, 0] }
+ @directions[:right] = [0, 1]
+ @directions[:down] = [1, 0]
+
+ mark_position_visited
+ end
+
+ def draw(canvas = nil, &block)
+ self.instance_eval(&block)
+
+ drawing = canvas.draw(@pixel_map) if canvas.respond_to? :draw
+ return drawing if drawing
+ @pixel_map
+ end
+
+private
+ def turn_right
+ @orientation_index = (@orientation_index + 1) % @orientations.size
+ end
+
+ def turn_left
+ @orientation_index = (@orientation_index - 1) % @orientations.size
+ end
+
+ def orientation
+ @orientations[@orientation_index]
+ end
+
+ def move
+ from_row = @current_position.first
+ from_column = @current_position.last
+
+ to_row = from_row + @directions[orientation].first
+ to_column = from_column + @directions[orientation].last
+
+ to_row = 0 if to_row == @rows
+ to_column = 0 if to_column == @columns
+
+ @current_position = [to_row, to_column]
+
+ mark_position_visited
+
+ @current_position
+ end
+
+ def spawn_at(row, column)
+ @current_position = [row, column]
+ mark_position_visited
+ end
+
+ def look(direction)
+ @direction = direction
+ end
+
+ def mark_position_visited
+ row = @current_position.first
+ column = @current_position.last
+ @pixel_map[row][column] += 1
+ end
+
+ def to_s
+ @pixel_map.each { |row| p row }
+ end
+
+ end #end of class Turtle
+
+ module Canvas
+ class ASCII
+ def initialize(symbols)
+ @symbols = symbols
+ end
+
+ def symbol_for_pixel(pixel)
+ number_of_symbols = @symbols.size
+ approximate_index = (number_of_symbols - 1) * pixel
+ p "Appr index = #{approximate_index}"
+
+ index = approximate_index.round
+ p "Index = #{index}"
+ @symbols[index]
+ end
+
+ def draw(pixel_map)
+ highest_intensity = pixel_map.flatten.max
+ intensity_map = pixel_map.each do | row |
+ row.map! { | pixel | pixel.to_f / highest_intensity.to_f }
+ end
+ intensity_map.each do | row |
+ row.map! { | pixel | symbol_for_pixel(pixel) }
+ end
+
+ intensity_map
+ end
+
+ end #of class ASCII
+
+ class HTML
+ def initialize(cell_size)
+ @cell_size = cell_size
+ end
+
+ def html_document(pixel_map)
+ "<!DOCTYPE html>\n"\
+ "<html>\n"\
+ "#{html_head}"\
+ "#{html_body(pixel_map)}"\
+ "</html>"
+ end
+
+ def html_head
+ "<head>\n"\
+ " <title>Turtle graphics</title>\n\n"\
+ "#{html_style}\n"\
+ "</head>\n"
+ end
+
+ def html_style
+ " <style>\n"\
+ " table {\n"\
+ " border-spacing: 0;\n }\n\n"\
+ " tr {\n padding: 0;\n }\n\n"\
+ " td {\n"\
+ " width: #{@cell_size}px;\n"\
+ " height: #{@cell_size}px;\n\n"\
+ " background-color: black;\n"\
+ " padding: 0;\n }\n"\
+ " </style>"
+ end
+
+ def html_body(pixel_map)
+ "<body>\n"\
+ "#{construct_html_table(pixel_map)}"\
+ "</body>\n"
+ end
+
+ def html_cell(opacity)
+ " <td style=\"opacity: #{format('%.2f', opacity)}\"></td>"
+ end
+
+ def html_row(html_cells)
+ " <tr>\n"\
+ "#{html_cells.join("\n")}\n"\
+ " </tr>"
+ end
+
+ def html_table(html_rows)
+ " <table>\n"\
+ "#{html_rows.join("\n")}\n"\
+ " </table>\n"
+ end
+
+ def construct_html_table(pixel_map)
+
+ highest_intensity = pixel_map.flatten.max
+ intensity_map = pixel_map.each do | row |
+ row.map! { | pixel | pixel / highest_intensity }
+ end
+
+ intensity_map.each do | row |
+ row.map! { | pixel | html_cell(pixel) }
+ end
+
+ html_rows = intensity_map.map { | row | html_row(row) }
+
+ html_table(html_rows)
+ end
+
+ def draw(pixel_map)
+ html_document(pixel_map)
+ end
+ end #of class HTML
+ end #of module Canvas
+end #of module TurtleGraphics

Георги обнови решението на 02.12.2015 02:45 (преди над 8 години)

module TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@pixel_map = Array.new(rows) { Array.new(columns) { 0 } }
@current_position = [0, 0]
@orientations = [:right, :down, :left, :up]
@orientation_index = 0
- @directions = {:left => [0, -1], :up => [-1, 0] }
+ @directions = {:left => [0, -1], :up => [-1, 0]}
@directions[:right] = [0, 1]
@directions[:down] = [1, 0]
mark_position_visited
end
def draw(canvas = nil, &block)
self.instance_eval(&block)
drawing = canvas.draw(@pixel_map) if canvas.respond_to? :draw
return drawing if drawing
@pixel_map
end
-private
+ private
def turn_right
@orientation_index = (@orientation_index + 1) % @orientations.size
end
def turn_left
@orientation_index = (@orientation_index - 1) % @orientations.size
end
def orientation
@orientations[@orientation_index]
end
def move
from_row = @current_position.first
from_column = @current_position.last
to_row = from_row + @directions[orientation].first
to_column = from_column + @directions[orientation].last
to_row = 0 if to_row == @rows
to_column = 0 if to_column == @columns
@current_position = [to_row, to_column]
mark_position_visited
@current_position
end
def spawn_at(row, column)
@current_position = [row, column]
mark_position_visited
end
def look(direction)
@direction = direction
end
def mark_position_visited
row = @current_position.first
column = @current_position.last
@pixel_map[row][column] += 1
end
-
- def to_s
- @pixel_map.each { |row| p row }
- end
-
end #end of class Turtle
module Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
- def symbol_for_pixel(pixel)
- number_of_symbols = @symbols.size
- approximate_index = (number_of_symbols - 1) * pixel
- p "Appr index = #{approximate_index}"
-
- index = approximate_index.round
- p "Index = #{index}"
- @symbols[index]
- end
-
def draw(pixel_map)
highest_intensity = pixel_map.flatten.max
intensity_map = pixel_map.each do | row |
row.map! { | pixel | pixel.to_f / highest_intensity.to_f }
end
intensity_map.each do | row |
row.map! { | pixel | symbol_for_pixel(pixel) }
end
intensity_map
end
+ private
+ def symbol_for_pixel(pixel)
+ number_of_symbols = @symbols.size
+ approximate_index = (number_of_symbols - 1) * pixel
+ index = approximate_index.round
+
+ @symbols[index]
+ end
end #of class ASCII
class HTML
def initialize(cell_size)
@cell_size = cell_size
end
+ def draw(pixel_map)
+ html_document(pixel_map)
+ end
+
+ private
def html_document(pixel_map)
"<!DOCTYPE html>\n"\
"<html>\n"\
"#{html_head}"\
"#{html_body(pixel_map)}"\
"</html>"
end
def html_head
"<head>\n"\
" <title>Turtle graphics</title>\n\n"\
"#{html_style}\n"\
"</head>\n"
end
def html_style
" <style>\n"\
" table {\n"\
" border-spacing: 0;\n }\n\n"\
" tr {\n padding: 0;\n }\n\n"\
" td {\n"\
" width: #{@cell_size}px;\n"\
" height: #{@cell_size}px;\n\n"\
" background-color: black;\n"\
" padding: 0;\n }\n"\
" </style>"
end
def html_body(pixel_map)
"<body>\n"\
"#{construct_html_table(pixel_map)}"\
"</body>\n"
end
def html_cell(opacity)
" <td style=\"opacity: #{format('%.2f', opacity)}\"></td>"
end
def html_row(html_cells)
" <tr>\n"\
"#{html_cells.join("\n")}\n"\
" </tr>"
end
def html_table(html_rows)
" <table>\n"\
"#{html_rows.join("\n")}\n"\
" </table>\n"
end
def construct_html_table(pixel_map)
highest_intensity = pixel_map.flatten.max
intensity_map = pixel_map.each do | row |
- row.map! { | pixel | pixel / highest_intensity }
+ row.map! { | pixel | pixel / highest_intensity }
end
intensity_map.each do | row |
row.map! { | pixel | html_cell(pixel) }
end
html_rows = intensity_map.map { | row | html_row(row) }
html_table(html_rows)
- end
-
- def draw(pixel_map)
- html_document(pixel_map)
end
end #of class HTML
end #of module Canvas
end #of module TurtleGraphics

Георги обнови решението на 02.12.2015 14:52 (преди над 8 години)

module TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@pixel_map = Array.new(rows) { Array.new(columns) { 0 } }
@current_position = [0, 0]
@orientations = [:right, :down, :left, :up]
@orientation_index = 0
- @directions = {:left => [0, -1], :up => [-1, 0]}
- @directions[:right] = [0, 1]
- @directions[:down] = [1, 0]
+ @directions = {
+ left: [0, -1], up: [-1, 0],
+ right: [0, 1], down: [1, 0]
+ }
mark_position_visited
end
def draw(canvas = nil, &block)
self.instance_eval(&block)
drawing = canvas.draw(@pixel_map) if canvas.respond_to? :draw
return drawing if drawing
@pixel_map
end
private
def turn_right
@orientation_index = (@orientation_index + 1) % @orientations.size
end
def turn_left
@orientation_index = (@orientation_index - 1) % @orientations.size
end
def orientation
@orientations[@orientation_index]
end
def move
from_row = @current_position.first
from_column = @current_position.last
to_row = from_row + @directions[orientation].first
to_column = from_column + @directions[orientation].last
to_row = 0 if to_row == @rows
to_column = 0 if to_column == @columns
@current_position = [to_row, to_column]
mark_position_visited
@current_position
end
def spawn_at(row, column)
@current_position = [row, column]
mark_position_visited
end
def look(direction)
@direction = direction
end
def mark_position_visited
row = @current_position.first
column = @current_position.last
@pixel_map[row][column] += 1
end
end #end of class Turtle
module Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
def draw(pixel_map)
highest_intensity = pixel_map.flatten.max
- intensity_map = pixel_map.each do | row |
- row.map! { | pixel | pixel.to_f / highest_intensity.to_f }
+ intensity_map = pixel_map.each do |row|
+ row.map! { |pixel| pixel.to_f / highest_intensity.to_f }
end
- intensity_map.each do | row |
- row.map! { | pixel | symbol_for_pixel(pixel) }
+ intensity_map.each do |row|
+ row.map! { |pixel| symbol_for_pixel(pixel) }
end
intensity_map
end
private
def symbol_for_pixel(pixel)
number_of_symbols = @symbols.size
approximate_index = (number_of_symbols - 1) * pixel
index = approximate_index.round
@symbols[index]
end
end #of class ASCII
class HTML
def initialize(cell_size)
@cell_size = cell_size
end
def draw(pixel_map)
html_document(pixel_map)
end
private
def html_document(pixel_map)
"<!DOCTYPE html>\n"\
"<html>\n"\
"#{html_head}"\
"#{html_body(pixel_map)}"\
"</html>"
end
def html_head
"<head>\n"\
" <title>Turtle graphics</title>\n\n"\
"#{html_style}\n"\
"</head>\n"
end
def html_style
" <style>\n"\
" table {\n"\
" border-spacing: 0;\n }\n\n"\
" tr {\n padding: 0;\n }\n\n"\
" td {\n"\
" width: #{@cell_size}px;\n"\
" height: #{@cell_size}px;\n\n"\
" background-color: black;\n"\
" padding: 0;\n }\n"\
" </style>"
end
def html_body(pixel_map)
"<body>\n"\
"#{construct_html_table(pixel_map)}"\
"</body>\n"
end
def html_cell(opacity)
" <td style=\"opacity: #{format('%.2f', opacity)}\"></td>"
end
def html_row(html_cells)
" <tr>\n"\
"#{html_cells.join("\n")}\n"\
" </tr>"
end
def html_table(html_rows)
" <table>\n"\
"#{html_rows.join("\n")}\n"\
" </table>\n"
end
def construct_html_table(pixel_map)
highest_intensity = pixel_map.flatten.max
- intensity_map = pixel_map.each do | row |
- row.map! { | pixel | pixel / highest_intensity }
+ intensity_map = pixel_map.each do |row|
+ row.map! { |pixel| pixel / highest_intensity }
end
- intensity_map.each do | row |
- row.map! { | pixel | html_cell(pixel) }
+ intensity_map.each do |row|
+ row.map! { |pixel| html_cell(pixel) }
end
- html_rows = intensity_map.map { | row | html_row(row) }
+ html_rows = intensity_map.map { |row| html_row(row) }
html_table(html_rows)
end
end #of class HTML
end #of module Canvas
end #of module TurtleGraphics