Решение на Шеста задача от Димитър Терзиев

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

Към профила на Димитър Терзиев

Резултати

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

Код

class TurtleGraphics
DIRECTIONS = [[0, -1], [1, 0], [0, 1], [-1, 0]]
class Turtle
def initialize x, y
@width = x
@height = y
@canvas = Array.new(y) { Array.new(x, 0) }
@canvas[0][0] = 1
@turtle = [0,0]
look :right
end
def draw(special_canvas = false, &block)
instance_eval &block
return special_canvas.transform @canvas if special_canvas
@canvas
end
def look(orientation)
@direction = case orientation
when :left then DIRECTIONS[0]
when :down then DIRECTIONS[1]
when :right then DIRECTIONS[2]
when :up then DIRECTIONS[3]
end
end
def move
@turtle[0] = (@turtle[0] + @direction.first) % @height
@turtle[1] = (@turtle[1] + @direction.last) % @width
@canvas[@turtle.first][@turtle.last] += 1
end
def turn_right
@direction = DIRECTIONS[(DIRECTIONS.index(@direction) - 1) % 4]
end
def turn_left
@direction = DIRECTIONS[(DIRECTIONS.index(@direction) + 1) % 4]
end
def spawn_at(row, column)
@turtle[0] = row
@turtle[1] = column
end
end
class Canvas
class ASCII
def initialize(intensities)
@intensities = intensities
end
def transform(canvas)
max_value = canvas.map(&:max).max
intensity_step = max_value.to_f / (@intensities.size - 1)
canvas.map do |row|
row.map{ |value| @intensities[value.to_f / intensity_step] } << "\n"
end.join ""
end
end
class HTML
def initialize(square_edge)
@square_edge = square_edge
end
def get_table canvas, max_value
table = ""
canvas.map do |row|
table << "\t<tr>\n"
row.map do |value|
opacity = format('%.2f', value.to_f / max_value)
table << "\t\t<td style=\"opacity: #{opacity}\"></td>\n"
end
table << "\t</tr>\n"
end
table
end
def transform(canvas)
max_value = canvas.map(&:max).max
table = get_table canvas, max_value
<<-EOS
<!DOCTYPE html>
<html>
<head>
\t<title>Turtle graphics</title>
\t<style>
\t\ttable {
\t\t\tborder-spacing: 0;
\t\t}
\t\ttr {
\t\t\tpadding: 0;
\t\t}
\t\ttd {
\t\t\twidth: #{@square_edge}px;
\t\t\theight: #{@square_edge}px;
\t\t\tbackground-color: black;
\t\t\tpadding: 0;
\t\t}
\t</style>
</head>
<body>
\t<table>
#{table}
\t</table>
</body>
</html>
EOS
end
end
end
end

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

F......F..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, 0, 0, 0, 0, 0, 0, 0, 0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 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-1pe233w/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], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1pe233w/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 can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
       
       expected: "ttz\nooz\nzzz"
            got: "toz\nzzz\nzzz"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -ttz
       -ooz
       +toz
       +zzz
        zzz
     # /tmp/d20151203-5272-1pe233w/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.0134 seconds
14 examples, 3 failures

Failed examples:

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

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

Димитър обнови решението на 01.12.2015 00:32 (преди над 8 години)

+class TurtleGraphics
+ DIRECTIONS = [[0, -1], [1, 0], [0, 1], [-1, 0]]
+ class Turtle
+ def initialize x, y
+ @width = x
+ @height = y
+ @canvas = Array.new(y) { Array.new(x, 0) }
+ @canvas[0][0] = 1
+ @turtle = [0,0]
+ look :right
+ end
+
+ def draw(special_canvas = false, &block)
+ instance_eval &block
+ return special_canvas.transform @canvas if special_canvas
+ @canvas
+ end
+
+ def look(orientation)
+ @direction = case orientation
+ when :left then DIRECTIONS[0]
+ when :down then DIRECTIONS[1]
+ when :right then DIRECTIONS[2]
+ when :up then DIRECTIONS[3]
+ end
+ end
+
+ def move
+ @turtle[0] = (@turtle[0] + @direction.first) % @height
+ @turtle[1] = (@turtle[1] + @direction.last) % @width
+ @canvas[@turtle.first][@turtle.last] += 1
+ end
+
+ def turn_right
+ @direction = DIRECTIONS[(DIRECTIONS.index(@direction) - 1) % 4]
+ end
+
+ def turn_left
+ @direction = DIRECTIONS[(DIRECTIONS.index(@direction) + 1) % 4]
+ end
+
+ def spawn_at(row, column)
+ @turtle[0] = row
+ @turtle[1] = column
+ end
+ end
+
+ class Canvas
+ class ASCII
+ def initialize(intensities)
+ @intensities = intensities
+ end
+
+ def transform(canvas)
+ max_value = canvas.map(&:max).max
+ intensity_step = max_value.to_f / (@intensities.size - 1)
+ canvas.map do |row|
+ row.map{ |value| @intensities[value.to_f / intensity_step] } << "\n"
+ end.join ""
+ end
+ end
+
+ class HTML
+ def initialize(square_edge)
+ @square_edge = square_edge
+ end
+
+ def get_table canvas, max_value
+ table = ""
+ canvas.map do |row|
+ table << "\t<tr>\n"
+ row.map do |value|
+ opacity = format('%.2f', value.to_f / max_value)
+ table << "\t\t<td style=\"opacity: #{opacity}\"></td>\n"
+ end
+ table << "\t</tr>\n"
+ end
+ table
+ end
+
+ def transform(canvas)
+ max_value = canvas.map(&:max).max
+ table = get_table canvas, max_value
+ <<-EOS
+<!DOCTYPE html>
+<html>
+<head>
+\t<title>Turtle graphics</title>
+
+\t<style>
+\t\ttable {
+\t\t\tborder-spacing: 0;
+\t\t}
+
+\t\ttr {
+\t\t\tpadding: 0;
+\t\t}
+
+\t\ttd {
+\t\t\twidth: #{@square_edge}px;
+\t\t\theight: #{@square_edge}px;
+
+\t\t\tbackground-color: black;
+\t\t\tpadding: 0;
+\t\t}
+\t</style>
+</head>
+<body>
+\t<table>
+#{table}
+\t</table>
+</body>
+</html>
+ EOS
+ end
+ end
+ end
+end