Решение на Трета задача от Емануела Моллова

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

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

Резултати

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

Код

module Graphics
class Canvas
attr_reader :width,:height
def initialize(width, height)
@width = width
@height = height
@filled_pixels = []
end
def set_pixel(x, y)
@filled_pixels << [x, y]
end
def pixel_at?(x, y)
@filled_pixels.include? [x, y]
end
def render
canvas = Array.new(@width * @height, 0)
@filled_pixels.each { |pixel| canvas[pixel.last * @width + pixel.first] = 1 }
canvas.each_slice(@width).to_a.map(&:join).join('2')
end
def render_as(renderer)
rendered = render.gsub(/[012]/, renderer.dictionary)
if renderer == Graphics::Renderers::Html
renderer.header + rendered + renderer.footer
else
rendered
end
end
def draw(figure)
figure.pixels.each { |pixel| set_pixel(pixel.x, pixel.y) }
end
end
module Renderers
class Ascii
class << self
def dictionary
{'0' => '-', '1' => '@', '2' => "\n"}
end
end
end
class Html
class << self
def dictionary
{'0' => '<i></i>', '1' => '<b></b>', '2' => '<br>'}
end
def header
'<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="text/css">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="canvas">'
end
def footer
'</div>
</body>
</html>'
end
end
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def pixels
[self]
end
def ==(other)
@x == other.x and @y == other.y
end
def eql?(other)
@x.eql? other.x and @y.eql? other.y
end
def hash
[@x, @y].hash
end
end
module Figure
def sort_points(*points)
points.sort_by { |point| [point.x, point.y] }
end
end
class Line
include Figure
attr_reader :from, :to
def initialize(from, to)
points = sort_points(from, to)
@from = points.first
@to = points.last
end
def pixels
([@from, @to] + rasterize_line).uniq
end
def rasterize_line(from = @from, to = @to, points = [])
point = Point.new(((from.x + to.x)/2.0).ceil, ((from.y + to.y)/2.0).ceil)
return points.uniq if (point == from or point == to)
points << point
rasterize_line(from, point, points) if point != from
rasterize_line(point, to, points) if point != to
end
def ==(other)
@from == other.from and @to == other.to
end
def eql?(other)
@from.eql? other.from and @to.eql? other.to
end
def hash
[@from, @to].map(&:hash).reduce(:+)
end
end
class Rectangle
include Figure
attr_reader :left, :right
def initialize(left, right)
points = sort_points(left, right)
@left = points.first
@right = points.last
end
def vertexes
sort_points(
@left,
@right,
Point.new(@left.x, @right.y),
Point.new(@right.x, @left.y)
)
end
def top_left
vertexes[0]
end
def bottom_left
vertexes[1]
end
def top_right
vertexes[2]
end
def bottom_right
vertexes[3]
end
def pixels
(Line.new(bottom_left, bottom_right).pixels +
Line.new(top_right, bottom_right).pixels +
Line.new(top_left, top_right).pixels +
Line.new(top_left, bottom_left).pixels).uniq
end
def ==(other)
@left == other.left and @right == other.right
end
def eql?(other)
@left.eql? other.left and @right.eql? other.right
end
def hash
[@left, @right].map(&:hash).reduce(:+)
end
end
end

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

........F.F..FFF............................................F..F.....

Failures:

  1) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@------@@-@-@\n@-@---@@@---@-@\n@-@-@@------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
            got: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@-------@-@-@\n@-@----@@@@-@-@\n@-@-@@@@----@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
       
       (compared using ==)
       
       Diff:
       @@ -2,9 +2,9 @@
        @-------------@
        @-@@@@@@@@@@@-@
        @-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       +@-@-------@-@-@
       +@-@----@@@@-@-@
       +@-@-@@@@----@-@
        @-@---------@-@
        @-@-@@@@----@-@
        @-@-@-------@-@
     # /tmp/d20131223-4637-22w854/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-22w854/spec.rb:211:in `block (4 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) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@---\n@---\n-@@-\n----"
            got: "@---\n@---\n-@@-\n----\n\n@"
       
       (compared using ==)
       
       Diff:
       @@ -2,4 +2,6 @@
        @---
        -@@-
        ----
       +
       +@
     # /tmp/d20131223-4637-22w854/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-22w854/spec.rb:59: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) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@-------\n---@@@@---\n-------@@-\n----------"
            got: "----------\n-@--------\n--@@@@----\n------@@@-\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@-------
       ----@@@@---
       --------@@-
       +-@--------
       +--@@@@----
       +------@@@-
        ----------
     # /tmp/d20131223-4637-22w854/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-22w854/spec.rb:100: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) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@--------\n-@--------\n--@-------\n--@-------\n--@-------\n--@-------\n---@------\n---@------\n----------"
            got: "----------\n-@--------\n--@-------\n--@-------\n--@-------\n--@-------\n---@------\n---@------\n---@------\n----------"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,10 +1,10 @@
        ----------
        -@--------
       --@--------
        --@-------
        --@-------
        --@-------
        --@-------
       +---@------
        ---@------
        ---@------
        ----------
     # /tmp/d20131223-4637-22w854/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-22w854/spec.rb:113: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)>'

  5) Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@--------\n-@@-------\n-@-@@@@---\n-@-----@@-\n----------"
            got: "-@--------\n-@--------\n-@@@@@----\n-@----@@@-\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        -@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       +-@--------
       +-@@@@@----
       +-@----@@@-
        ----------
     # /tmp/d20131223-4637-22w854/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-22w854/spec.rb:132: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)>'

  6) Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-22w854/spec.rb:540: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)>'

  7) Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
     Failure/Error: a.hash.should eq b.hash
       
       expected: 837988460
            got: -937287572
       
       (compared using ==)
     # /tmp/d20131223-4637-22w854/spec.rb:563: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)>'

Finished in 0.097 seconds
69 examples, 7 failures

Failed examples:

rspec /tmp/d20131223-4637-22w854/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-22w854/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-22w854/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-22w854/spec.rb:109 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
rspec /tmp/d20131223-4637-22w854/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-22w854/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-22w854/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

Емануела обнови решението на 22.12.2013 23:18 (преди почти 11 години)

+module Graphics
+ class Canvas
+ attr_reader :width,:height
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @filled_pixels = []
+ end
+
+ def set_pixel(x, y)
+ @filled_pixels << [x, y]
+ end
+
+ def pixel_at?(x, y)
+ @filled_pixels.include? [x, y]
+ end
+
+ def render
+ canvas = Array.new(@width * @height, 0)
+ @filled_pixels.each { |pixel| canvas[pixel.last * @width + pixel.first] = 1 }
+ canvas.each_slice(@width).to_a.map(&:join).join('2')
+ end
+
+ def render_as(renderer)
+ rendered = render.gsub(/[012]/, renderer.dictionary)
+ if renderer == Graphics::Renderers::Html
+ renderer.header + rendered + renderer.footer
+ else
+ rendered
+ end
+ end
+
+ def draw(figure)
+ figure.pixels.each { |pixel| set_pixel(pixel.x, pixel.y) }
+ end
+ end
+
+ module Renderers
+ class Ascii
+ class << self
+ def dictionary
+ {'0' => '-', '1' => '@', '2' => "\n"}
+ end
+ end
+ end
+
+ class Html
+ class << self
+ def dictionary
+ {'0' => '<i></i>', '1' => '<b></b>', '2' => '<br>'}
+ end
+
+ def header
+ '<!DOCTYPE html>
+ <html>
+ <head>
+ <title>Rendered Canvas</title>
+ <style type="text/css">
+ .canvas {
+ font-size: 1px;
+ line-height: 1px;
+ }
+ .canvas * {
+ display: inline-block;
+ width: 10px;
+ height: 10px;
+ border-radius: 5px;
+ }
+ .canvas i {
+ background-color: #eee;
+ }
+ .canvas b {
+ background-color: #333;
+ }
+ </style>
+ </head>
+ <body>
+ <div class="canvas">'
+ end
+
+ def footer
+ '</div>
+ </body>
+ </html>'
+ end
+ end
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def pixels
+ [self]
+ end
+
+ def ==(other)
+ @x == other.x and @y == other.y
+ end
+
+ def eql?(other)
+ @x.eql? other.x and @y.eql? other.y
+ end
+
+ def hash
+ [@x, @y].hash
+ end
+ end
+
+ module Figure
+ def sort_points(*points)
+ points.sort_by { |point| [point.x, point.y] }
+ end
+ end
+
+ class Line
+ include Figure
+ attr_reader :from, :to
+
+ def initialize(from, to)
+ points = sort_points(from, to)
+ @from = points.first
+ @to = points.last
+ end
+
+ def pixels
+ ([@from, @to] + rasterize_line).uniq
+ end
+
+ def rasterize_line(from = @from, to = @to, points = [])
+ point = Point.new(((from.x + to.x)/2.0).ceil, ((from.y + to.y)/2.0).ceil)
+ return points.uniq if (point == from or point == to)
+ points << point
+ rasterize_line(from, point, points) if point != from
+ rasterize_line(point, to, points) if point != to
+ end
+
+ def ==(other)
+ @from == other.from and @to == other.to
+ end
+
+ def eql?(other)
+ @from.eql? other.from and @to.eql? other.to
+ end
+
+ def hash
+ [@from, @to].map(&:hash).reduce(:+)
+ end
+ end
+
+ class Rectangle
+ include Figure
+ attr_reader :left, :right
+
+ def initialize(left, right)
+ points = sort_points(left, right)
+ @left = points.first
+ @right = points.last
+ end
+
+ def vertexes
+ sort_points(
+ @left,
+ @right,
+ Point.new(@left.x, @right.y),
+ Point.new(@right.x, @left.y)
+ )
+ end
+
+ def top_left
+ vertexes[0]
+ end
+
+ def bottom_left
+ vertexes[1]
+ end
+
+ def top_right
+ vertexes[2]
+ end
+
+ def bottom_right
+ vertexes[3]
+ end
+
+ def pixels
+ (Line.new(bottom_left, bottom_right).pixels +
+ Line.new(top_right, bottom_right).pixels +
+ Line.new(top_left, top_right).pixels +
+ Line.new(top_left, bottom_left).pixels).uniq
+ end
+
+ def ==(other)
+ @left == other.left and @right == other.right
+ end
+
+ def eql?(other)
+ @left.eql? other.left and @right.eql? other.right
+ end
+
+ def hash
+ [@left, @right].map(&:hash).reduce(:+)
+ end
+ end
+end