Решение на Шеста задача от Изтрит профил

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

Към профила на Изтрит профил

Резултати

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

Код

module TurtleGraphics
class TurtleGraphics::Turtle
ORIENTATIONS = [:left, :up, :right, :down]
DIRS = {left: [-1, 0], up: [0, -1], right: [1, 0], down: [0, 1]}
def initialize(rows, columns)
@rows = rows
@columns = columns
self.look(:right)
end
def draw(custom_canvas = nil, &f)
@canvas = Array.new(@rows) { Array.new(@columns, 0) }
self.instance_eval(&f) if block_given?
if custom_canvas
custom_canvas.convert(@canvas)
else
@canvas
end
end
def move
if @position == nil
self.spawn_at(0, 0)
mark_position
end
dir = DIRS[@orientation]
new_x, new_y = @position[0] + dir[0], @position[1] + dir[1]
@position = [new_x % @columns, new_y % @rows]
mark_position
end
def mark_position
@canvas[@position[1]][@position[0]] += 1
end
def spawn_at(row, column)
@position = [row, column]
end
def turn_left
turn(-1)
end
def turn_right
turn(1)
end
def turn(direction)
look(ORIENTATIONS[(ORIENTATIONS.index(@orientation) + direction) % 4])
end
def look(orientation)
@orientation = orientation
end
end
end
module TurtleGraphics::Canvas
class TurtleGraphics::Canvas::ASCII
def initialize(symbols)
@symbols = symbols
@step = 1.0 / (@symbols.length - 1)
end
def convert(canvas)
max = canvas.map { |row| row.max }.max
result = ""
canvas.map do |row|
a = row.map do |pixel|
intensity = pixel.to_f / max
@symbols[(intensity / @step).ceil]
end
result << a.join(" ") << "\n"
end
result
end
end
class TurtleGraphics::Canvas::HTML
TEMPLATE_HTML =
'<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>
<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
width: %dpx;
height: %dpx;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
<table>
%s </table>
</body>
</html>'
TEMPLATE_TD = " <td style=\"opacity: %s\"></td>\n"
def initialize(size)
@size = size
end
def convert(canvas)
max = canvas.map { |row| row.max }.max
result = ""
canvas.map do |row|
result << " <tr>\n"
row.map do |pixel|
intensity = pixel.to_f / max
result << TEMPLATE_TD % [format('%.2f', intensity)]
end
result << " </tr>\n"
end
TEMPLATE_HTML % [@size, @size, result]
end
end
end

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

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

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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 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-1kl0xo9/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: [[0, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1kl0xo9/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 [
       
       expected: "320\n110\n000"
            got: "3 2 0\n1 1 0\n0 0 0"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -320
       -110
       -000
       +3 2 0
       +1 1 0
       +0 0 0
     # /tmp/d20151203-5272-1kl0xo9/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 [
       
       expected: "ttz\nooz\nzzz"
            got: "t t z\no o z\nz z z"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -ttz
       -ooz
       -zzz
       +t t z
       +o o z
       +z z z
     # /tmp/d20151203-5272-1kl0xo9/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)>'

Finished in 0.01377 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20151203-5272-1kl0xo9/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-1kl0xo9/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-1kl0xo9/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-1kl0xo9/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols

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

Изтрит обнови решението на 29.11.2015 21:56 (преди около 9 години)

+module TurtleGraphics
+ class TurtleGraphics::Turtle
+
+ ORIENTATIONS = [:left, :up, :right, :down]
+ DIRS = {left: [-1, 0], up: [0, -1], right: [1, 0], down: [0, 1]}
+
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+
+ self.look(:right)
+ end
+
+ def draw(custom_canvas = nil, &f)
+ @canvas = Array.new(@rows) { Array.new(@columns, 0) }
+
+ self.instance_eval(&f) if block_given?
+
+ if custom_canvas
+ custom_canvas.convert(@canvas)
+ else
+ @canvas
+ end
+ end
+
+ def move
+ if @position == nil
+ self.spawn_at(0, 0)
+ mark_position
+ end
+
+ dir = DIRS[@orientation]
+ new_x, new_y = @position[0] + dir[0], @position[1] + dir[1]
+ @position = [new_x % @columns, new_y % @rows]
+ mark_position
+ end
+
+ def mark_position
+ @canvas[@position[1]][@position[0]] += 1
+ end
+
+ def spawn_at(row, column)
+ @position = [row, column]
+ end
+
+ def turn_left
+ turn(-1)
+ end
+
+ def turn_right
+ turn(1)
+ end
+
+ def turn(direction)
+ look(ORIENTATIONS[(ORIENTATIONS.index(@orientation) + direction) % 4])
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+end
+
+module TurtleGraphics::Canvas
+ class TurtleGraphics::Canvas::ASCII
+ def initialize(symbols)
+ @symbols = symbols
+ @step = 1.0 / (@symbols.length - 1)
+ end
+
+ def convert(canvas)
+ max = canvas.map { |row| row.max }.max
+ result = ""
+
+ canvas.map do |row|
+ a = row.map do |pixel|
+ intensity = pixel.to_f / max
+ @symbols[(intensity / @step).ceil]
+ end
+
+ result << a.join(" ") << "\n"
+ end
+
+ result
+ end
+ end
+
+ class TurtleGraphics::Canvas::HTML
+
+ TEMPLATE_HTML =
+'<!DOCTYPE html>
+<html>
+<head>
+ <title>Turtle graphics</title>
+
+ <style>
+ table {
+ border-spacing: 0;
+ }
+
+ tr {
+ padding: 0;
+ }
+
+ td {
+ width: %dpx;
+ height: %dpx;
+
+ background-color: black;
+ padding: 0;
+ }
+ </style>
+</head>
+<body>
+ <table>
+%s </table>
+</body>
+</html>'
+
+ TEMPLATE_TD = " <td style=\"opacity: %s\"></td>\n"
+
+ def initialize(size)
+ @size = size
+ end
+
+ def convert(canvas)
+
+ max = canvas.map { |row| row.max }.max
+ result = ""
+ canvas.map do |row|
+ result << " <tr>\n"
+ row.map do |pixel|
+ intensity = pixel.to_f / max
+ result << TEMPLATE_TD % [format('%.2f', intensity)]
+ end
+ result << " </tr>\n"
+ end
+
+ TEMPLATE_HTML % [@size, @size, result]
+ end
+ end
+end

Здравей :)

Малко стилови коментари:

  • Паралелното присвояване не се използва толкова често на практика. Няма ползи от него в този му вид new_x, new_y = @position[0] + dir[0], @position[1] + dir[1]. Много по-чисто би било на два отделни реда.
  • В TEMPLATE_TD и другите низове няма нужда да слагаш нови редове и празни места - няма да ги взимаме под внимание.
  • Правиш едно излишно форматиране в convert. Първо форматираш число като %.2f, после резултата го форматираш като низ. Защо не сложиш директно %.2f в TEMPLATE_TD?
  • Конкатенирането на низове, особено in-place не е добра практика в Ruby. Опитай да направиш нещата без <<.
  • Използвай heredoc за многоредови низове. Така случайна кавичка в самото съдържание няма да затвори низа.