Решение на Шеста задача от Мила Русева

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

Към профила на Мила Русева

Резултати

  • 3 точки от тестове
  • 1 отнета точка
  • 2 точки общо
  • 7 успешни тест(а)
  • 7 неуспешни тест(а)

Код

module TurtleGraphics
class Turtle
attr_accessor :row, :column, :orientation,
:canvas, :x, :y
def initialize(row, column)
@row, @column = row, column
@orientation = :right
@canvas = Array.new(@row) { Array.new(@column, 0) }
spawn_at(0, 0)
end
def draw(*arg, &block)
instance_eval &block
if arg[0]
arg[0].draw(@canvas)
else
@canvas
end
end
def look(orientation)
@orientation = orientation
end
def spawn_at(row, column)
@y = row
@x = column
visit
end
def turn_left
@orientation = case @orientation
when :up then :left
when :left then :down
when :down then :right
when :right then :up
end
end
def turn_right
@orientation = case @orientation
when :up then :right
when :right then :down
when :down then :left
when :left then :up
end
end
def move
movements = {
right: [ 1, 0 ],
down: [ 0, 1],
left: [ -1, 0 ],
up: [ 0, -1 ]
}
@x += movements[@orientation][0]
@y += movements[@orientation][1]
inbound
visit
end
def inbound
if @x > @column - 1 or @x < 0
@x = 0
elsif @y > @row - 1 or @y < 0
@y = 0
end
end
def visit
@canvas[@y][@x] += 1
end
end
module Canvas
class ASCII
attr_accessor :symbols, :intensive
def initialize(symbols)
@symbols = symbols
end
def intensive_symbol(intensive)
index = (intensive * (symbols.length - 1)).ceil
@symbols[index]
end
def draw(canvas)
max_value = canvas.flatten.max
canvas.each do |row|
row.each do |value|
intensive = value.to_f / max_value
print intensive_symbol(intensive)
end
print "\n"
end
end
end
class HTML
def initialize(pixels)
@pixels = pixels
end
def draw(canvas)
html = draw_header
html += draw_css
html += draw_table(canvas)
html += draw_footer
end
def draw_header
return "<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>"
end
def draw_css
return "<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
width: #{@pixels}px;
height: #{@pixels}px;
background-color: black;
padding: 0;
}
</style>"
end
def draw_table(canvas)
max_value, table = canvas.flatten.max, "<body><table>"
canvas.each do |row|
table += "<tr>" + "\n"
row.each do |value|
intensive = value.to_f / max_value
table += "<td style= \"opacity: #{intensive.round(2)}\"></td>" + "\n"
end
table += "</tr>" + "\n"
end
table += "</table>"
return table
end
def draw_footer
return "</body>
</html>"
end
end
end
end

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

F.....FF.320
110
000
Fttz
ooz
zzz
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, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 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, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 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, 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,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0, 0, 1, 1, 1, 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, 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, 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]]
       
       (compared using ==)
     # /tmp/d20151203-5272-mnmzw4/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 #turn_left turns the orienation of the turtle left of where we stand
     Failure/Error: expect(canvas).to eq [[1, 0], [1, 0]]
       
       expected: [[1, 0], [1, 0]]
            got: [[2, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-mnmzw4/spec.rb:79: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 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-mnmzw4/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)>'

  4) 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-mnmzw4/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)>'

  5) 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 [[3, 2, 0], [1, 1, 0], [0, 0, 0]]:Array
     # /tmp/d20151203-5272-mnmzw4/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)>'

  6) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-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:1.00\"></td><tdstyle=\"opacity:1.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>"
            got: "<!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><body><table><tr><tdstyle=\"opacity:1.0\"></td><tdstyle=\"opacity:1.0\"></td><tdstyle=\"opacity:1.0\"></td></tr><tr><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></td></tr><tr><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></td></tr></table></body></html>"
       
       (compared using ==)
     # /tmp/d20151203-5272-mnmzw4/spec.rb:171: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)>'

  7) 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><body><table><tr><tdstyle=\"opacity:1.0\"></td><tdstyle=\"opacity:0.67\"></td><tdstyle=\"opacity:0.0\"></td></tr><tr><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.0\"></td></tr><tr><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></td><tdstyle=\"opacity:0.0\"></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-mnmzw4/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.01927 seconds
14 examples, 7 failures

Failed examples:

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

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

Мила обнови решението на 01.12.2015 23:36 (преди над 8 години)

+module TurtleGraphics
+
+ class Turtle
+
+ attr_accessor :row, :column, :orientation,
+ :canvas, :x, :y
+
+ def initialize(row, column)
+ @row, @column = row, column
+ @orientation = :right
+ @canvas = Array.new(@row) { Array.new(@column, 0) }
+ spawn_at(0, 0)
+ end
+
+ def draw(*arg, &block)
+ instance_eval &block
+ if arg[0]
+ arg[0].draw(@canvas)
+ else
+ @canvas
+ end
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def spawn_at(row, column)
+ @y = row
+ @x = column
+ visit
+ end
+
+ def turn_left
+ @orientation = case @orientation
+ when :up then :left
+ when :left then :down
+ when :down then :right
+ when :right then :up
+ end
+ end
+
+ def turn_right
+
+ @orientation = case @orientation
+ when :up then :right
+ when :right then :down
+ when :down then :left
+ when :left then :up
+ end
+
+ end
+
+ def move
+
+ movements = {
+ right: [ 1, 0 ],
+ down: [ 0, 1],
+ left: [ -1, 0 ],
+ up: [ 0, -1 ]
+ }
+ @x += movements[@orientation][0]
+ @y += movements[@orientation][1]
+
+ inbound
+
+ visit
+ end
+
+ def inbound
+
+ if @x > @column - 1 or @x < 0
+ @x = 0
+ elsif @y > @row - 1 or @y < 0
+ @y = 0
+ end
+
+ end
+
+ def visit
+ @canvas[@y][@x] += 1
+ end
+
+ end
+
+ module Canvas
+
+ class ASCII
+
+ attr_accessor :symbols, :intensive
+
+ def initialize(symbols)
+ @symbols = symbols
+ end
+
+ def intensive_symbol(intensive)
+
+ index = (intensive * (symbols.length - 1)).ceil
+ @symbols[index]
+
+ end
+
+ def draw(canvas)
+
+ max_value = canvas.flatten.max
+ canvas.each do |row|
+ row.each do |value|
+ intensive = value.to_f / max_value
+ print intensive_symbol(intensive)
+ end
+ print "\n"
+ end
+
+ end
+
+ end
+
+ class HTML
+
+ def initialize(pixels)
+ @pixels = pixels
+ end
+
+ def draw(canvas)
+ html = draw_header
+ html += draw_css
+ html += draw_table(canvas)
+ html += draw_footer
+ end
+
+ def draw_header
+ return "<!DOCTYPE html>
+<html>
+<head>
+ <title>Turtle graphics</title>"
+ end
+
+ def draw_css
+ return "<style>
+ table {
+ border-spacing: 0;
+ }
+
+ tr {
+ padding: 0;
+ }
+
+ td {
+ width: #{@pixels}px;
+ height: #{@pixels}px;
+
+ background-color: black;
+ padding: 0;
+ }
+</style>"
+ end
+
+ def draw_table(canvas)
+ max_value, table = canvas.flatten.max, "<body><table>"
+ canvas.each do |row|
+ table += "<tr>" + "\n"
+ row.each do |value|
+ intensive = value.to_f / max_value
+ table += "<td style= \"opacity: #{intensive.round(2)}\"></td>" + "\n"
+ end
+ table += "</tr>" + "\n"
+ end
+ table += "</table>"
+ return table
+ end
+
+ def draw_footer
+ return "</body>
+</html>"
+ end
+
+ end
+
+ end
+end

Здравей,

Имаш стилови проблеми. Например, използваш ненужни return-ове и празни редови в началото и края на методите.

Прегледай пак ръководството по стил и нашето решение на задачата.