Решение на Трета задача от Ясен Трифонов

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

Към профила на Ясен Трифонов

Резултати

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

Код

module Graphics
class HtmlDrawer
def self.beginning
heading + script + additional
end
def self.ending
<<END
</body>
</html>
END
end
def self.heading
<<END
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
END
end
def self.script
<<END
<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>
END
end
def self.additional
<<END
</head>
<body>
<div class="canvas">
END
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(width, height)
@width = width
@height = height
@canvas = Array.new(height) { Array.new(width) {0} }
end
def set_pixel(x, y)
@canvas[y][x] = 1
end
def pixel_at?(x, y)
@canvas[y][x] == 1 ? true : false
end
def draw(figure)
@canvas[figure.y][figure.x] = 1 if figure.class.to_s == "Graphics::Point"
draw_line figure if figure.class.to_s == "Graphics::Line"
draw_rectangle figure if figure.class.to_s == "Graphics::Rectangle"
end
def render_as(renderer)
if renderer.to_s == "Graphics::Renderers::Ascii"
render_ascii
elsif renderer.to_s == "Graphics::Renderers::Html"
render_html
end
end
private
def draw_line(line)
end
def draw_rectangle(rectangle)
x_range = (rectangle.top_left.x .. rectangle.top_right.x)
y_range = (rectangle.top_left.y .. rectangle.bottom_left.y)
x_range.each { |x| @canvas[rectangle.top_left.y][x] = 1 }
x_range.each { |x| @canvas[rectangle.bottom_left.y][x] = 1 }
y_range.each { |y| @canvas[y][rectangle.top_left.x] = 1 }
y_range.each { |y| @canvas[y][rectangle.top_right.x] = 1 }
end
def render_ascii
puts @canvas.map { |row| row.map { |cell| ascii_cell cell }.join }.join
end
def render_html
puts HtmlDrawer.beginning
puts @canvas.map { |row| row.map { |cell| html_cell cell }.join }.join '<br>'
puts HtmlDrawer.ending
end
def ascii_cell(cell)
cell == 0 ? '-' : '@'
end
def html_cell(cell)
cell == 0 ? '<i></i>' : '<b></b>'
end
end
module Renderers
module Ascii
:ascii
end
module Html
:html
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
protected
def state
[@x, @y]
end
end
class Line
attr_reader :a, :b
def initialize(a, b)
@a = a
@b = b
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
def left
options = [@a, @b]
options[compare]
end
def right
options = [@a, @b]
options[1 - compare]
end
protected
def compare
if @a.x < @b.x
0
elsif @b.x < @a.x
1
else
@a.y < @b.y ? 0 : 1
end
end
def state
[min(@a.x, @b.x), min(@a.y, @b.y), max(@a.x, @b.x), max(@a.y, @b.y)]
end
end
class Rectangle
attr_reader :a, :b
def initialize(a, b)
@a = a
@b = b
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
def top_left
Point.new(min(@a.x, @b.x), min(@a.y, @b.y))
end
def top_right
Point.new(max(@a.x, @b.x), min(@a.y, @b.y))
end
def bottom_left
Point.new(min(@a.x, @b.x), max(@a.y, @b.y))
end
def bottom_right
Point.new(max(@a.x, @b.x), max(@a.y, @b.y))
end
def left
options = [@a, @b]
options[compare]
end
def right
options = [@a, @b]
options[1 - compare]
end
protected
def min(x, y)
x < y ? x : y
end
def max(x, y)
x < y ? y : x
end
def compare
if @a.x < @b.x
0
elsif @b.x < @a.x
1
else
@a.y < @b.y ? 0 : 1
end
end
def state
[left, right]
end
end
end

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

........@@@@@@@@@@@@@@@@-------------@@-@@@@@@@@@@@-@@-@---------@-@@-@---------@-@@-@---------@-@@-@---------@-@@-@---------@-@@-@---------@-@@-@-@-------@-@@-@---------@-@@-@---------@-@@-@@@@@@@@@@@-@@-------------@@@@@@@@@@@@@@@@
F.F------------------------
F----------------------------------------------------------------
F--------------------------------------------------
F----------------------------------------------------------------------------------------------------
F--------------------------------------------------
F---------
F-----------@@@@@@@@--@------@--@@@@@@@@-----------
F-----------@@@@@@@@--@------@--@@@@@@@@-----------
F-----------@@@@@@@@-----------
F----@----
F------------
F------------
F@----------@
F<!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">
<i></i><i></i><i></i><i></i><br><i></i><i></i><i></i><i></i><br><i></i><i></i><i></i><i></i>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br><i></i><i></i><i></i><i></i><br><i></i><i></i><i></i><i></i>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br><i></i><b></b><i></i><i></i><br><i></i><b></b><i></i><i></i>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br><i></i><b></b><i></i><i></i><br><i></i><b></b><i></i><i></i>
</body>
</html>
F.......F...F.FFFFFFFFFF.........F.FF.....

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: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/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: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/solution.rb:54:in `set_pixel'
     # /tmp/d20131223-4637-xnco4h/spec.rb:57: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 simple horizontal lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "--------\n---@@@@-\n--------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:73: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 vertical lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:84: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 lines with a small slope
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@-------\n---@@@@---\n-------@@-\n----------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/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)>'

  6) 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: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/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)>'

  7) 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: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/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)>'

  8) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:145: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)>'

  9) Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:158: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)>'

  10) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:171: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)>'

  11) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n----------"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:184: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)>'

  12) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:195: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)>'

  13) Graphics Renderers Ascii renders a grid of the size of the canvas
     Failure/Error: lines = canvas.render_as(ascii).split("\n")
     NoMethodError:
       undefined method `split' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/spec.rb:238: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)>'

  14) Graphics Renderers Ascii renders blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:245: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)>'

  15) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "@---\n----\n---@"
            got: nil
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:256: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)>'

  16) Graphics Renderers Html returns html
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-xnco4h/spec.rb:272: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)>'

  17) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-xnco4h/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:281: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)>'

  18) Graphics Renderers Html renders simple canvases
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-xnco4h/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-xnco4h/spec.rb:291: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)>'

  19) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-xnco4h/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-xnco4h/spec.rb:302: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)>'

  20) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -576901031
            got: 798426865
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:361: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)>'

  21) Graphics shapes Line initialization returns its from and to points via getters
     Failure/Error: line.from.x.should eq 1
     NoMethodError:
       undefined method `from' for #<Graphics::Line:0xb9033aa4>
     # /tmp/d20131223-4637-xnco4h/spec.rb:383: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)>'

  22) Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
     Failure/Error: inverted_line.from.x.should eq 1
     NoMethodError:
       undefined method `from' for #<Graphics::Line:0xb90318d0>
     # /tmp/d20131223-4637-xnco4h/spec.rb:402:in `block (6 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)>'

  23) Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
     Failure/Error: inverted_line.to.x.should eq 25
     NoMethodError:
       undefined method `to' for #<Graphics::Line:0xb9030700>
     # /tmp/d20131223-4637-xnco4h/spec.rb:407:in `block (6 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)>'

  24) Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
     Failure/Error: vertical_line.from.x.should eq 1
     NoMethodError:
       undefined method `from' for #<Graphics::Line:0xb902b73c>
     # /tmp/d20131223-4637-xnco4h/spec.rb:412:in `block (6 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)>'

  25) Graphics shapes Line initialization with swapped points puts the bottom point of vertical lines in the to field
     Failure/Error: vertical_line.to.x.should eq 1
     NoMethodError:
       undefined method `to' for #<Graphics::Line:0xb9029d10>
     # /tmp/d20131223-4637-xnco4h/spec.rb:417:in `block (6 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)>'

  26) Graphics shapes Line comparison for equality is false if any of the points differ
     Failure/Error: (a == b).should be_false
     NoMethodError:
       undefined method `min' for #<Graphics::Line:0xb9029630>
     # /tmp/d20131223-4637-xnco4h/solution.rb:173:in `state'
     # /tmp/d20131223-4637-xnco4h/solution.rb:146:in `=='
     # /tmp/d20131223-4637-xnco4h/spec.rb:428: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)>'

  27) Graphics shapes Line comparison for equality is true if line ends are the same
     Failure/Error: (a == b).should be_true
     NoMethodError:
       undefined method `min' for #<Graphics::Line:0xb9022448>
     # /tmp/d20131223-4637-xnco4h/solution.rb:173:in `state'
     # /tmp/d20131223-4637-xnco4h/solution.rb:146:in `=='
     # /tmp/d20131223-4637-xnco4h/spec.rb:435: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)>'

  28) Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: (a == b).should be_true
     NoMethodError:
       undefined method `min' for #<Graphics::Line:0xb9021548>
     # /tmp/d20131223-4637-xnco4h/solution.rb:173:in `state'
     # /tmp/d20131223-4637-xnco4h/solution.rb:146:in `=='
     # /tmp/d20131223-4637-xnco4h/spec.rb:442: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)>'

  29) Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
     Failure/Error: (a == b).should be_true
     NoMethodError:
       undefined method `min' for #<Graphics::Line:0xb90203c8>
     # /tmp/d20131223-4637-xnco4h/solution.rb:173:in `state'
     # /tmp/d20131223-4637-xnco4h/solution.rb:146:in `=='
     # /tmp/d20131223-4637-xnco4h/spec.rb:449: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)>'

  30) Graphics shapes Line comparison for equality works with eql? as well
     Failure/Error: a.should eql b
     NoMethodError:
       undefined method `min' for #<Graphics::Line:0xb901b2c4>
     # /tmp/d20131223-4637-xnco4h/solution.rb:173:in `state'
     # /tmp/d20131223-4637-xnco4h/solution.rb:146:in `=='
     # /tmp/d20131223-4637-xnco4h/spec.rb:457: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)>'

  31) Graphics shapes Line comparison for equality returns the same hash if the lines are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: -213559733
            got: 718533275
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:465: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)>'

  32) 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-xnco4h/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)>'

  33) Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: -68485249
            got: 194402917
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/spec.rb:556: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)>'

  34) 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: -555619877
            got: 35661069
       
       (compared using ==)
     # /tmp/d20131223-4637-xnco4h/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.08743 seconds
69 examples, 34 failures

Failed examples:

rspec /tmp/d20131223-4637-xnco4h/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-xnco4h/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-xnco4h/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-xnco4h/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-xnco4h/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-xnco4h/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-xnco4h/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-xnco4h/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-xnco4h/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-xnco4h/spec.rb:167 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
rspec /tmp/d20131223-4637-xnco4h/spec.rb:180 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
rspec /tmp/d20131223-4637-xnco4h/spec.rb:191 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
rspec /tmp/d20131223-4637-xnco4h/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-xnco4h/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-xnco4h/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-xnco4h/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-xnco4h/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-xnco4h/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-xnco4h/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-xnco4h/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-xnco4h/spec.rb:382 # Graphics shapes Line initialization returns its from and to points via getters
rspec /tmp/d20131223-4637-xnco4h/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-xnco4h/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-xnco4h/spec.rb:411 # Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
rspec /tmp/d20131223-4637-xnco4h/spec.rb:416 # Graphics shapes Line initialization with swapped points puts the bottom point of vertical lines in the to field
rspec /tmp/d20131223-4637-xnco4h/spec.rb:424 # Graphics shapes Line comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-xnco4h/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-xnco4h/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-xnco4h/spec.rb:445 # Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
rspec /tmp/d20131223-4637-xnco4h/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-xnco4h/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-xnco4h/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-xnco4h/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-xnco4h/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

Ясен обнови решението на 22.12.2013 23:24 (преди почти 11 години)

+module Graphics
+ class HtmlDrawer
+ def self.beginning
+ heading + script + additional
+ end
+
+ def self.ending
+ <<END
+</body>
+</html>
+END
+ end
+
+ def self.heading
+ <<END
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Rendered Canvas</title>
+END
+ end
+
+ def self.script
+ <<END
+ <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>
+END
+ end
+
+ def self.additional
+ <<END
+</head>
+<body>
+ <div class="canvas">
+END
+ end
+ end
+
+ class Canvas
+ attr_reader :width, :height, :canvas
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+
+ @canvas = Array.new(height) { Array.new(width) {0} }
+ end
+
+ def set_pixel(x, y)
+ @canvas[y][x] = 1
+ end
+
+ def pixel_at?(x, y)
+ @canvas[y][x] == 1 ? true : false
+ end
+
+ def draw(figure)
+ @canvas[figure.y][figure.x] = 1 if figure.class.to_s == "Graphics::Point"
+ draw_line figure if figure.class.to_s == "Graphics::Line"
+ draw_rectangle figure if figure.class.to_s == "Graphics::Rectangle"
+ end
+
+ def render_as(renderer)
+ if renderer.to_s == "Graphics::Renderers::Ascii"
+ render_ascii
+ elsif renderer.to_s == "Graphics::Renderers::Html"
+ render_html
+ end
+ end
+
+ private
+
+ def draw_line(line)
+ end
+
+ def draw_rectangle(rectangle)
+ x_range = (rectangle.top_left.x .. rectangle.top_right.x)
+ y_range = (rectangle.top_left.y .. rectangle.bottom_left.y)
+ x_range.each { |x| @canvas[rectangle.top_left.y][x] = 1 }
+ x_range.each { |x| @canvas[rectangle.bottom_left.y][x] = 1 }
+ y_range.each { |y| @canvas[y][rectangle.top_left.x] = 1 }
+ y_range.each { |y| @canvas[y][rectangle.top_right.x] = 1 }
+ end
+
+ def render_ascii
+ puts @canvas.map { |row| row.map { |cell| ascii_cell cell }.join }.join
+ end
+
+ def render_html
+ puts HtmlDrawer.beginning
+ puts @canvas.map { |row| row.map { |cell| html_cell cell }.join }.join '<br>'
+ puts HtmlDrawer.ending
+ end
+
+ def ascii_cell(cell)
+ cell == 0 ? '-' : '@'
+ end
+
+ def html_cell(cell)
+ cell == 0 ? '<i></i>' : '<b></b>'
+ end
+ end
+
+ module Renderers
+ module Ascii
+ :ascii
+ end
+ module Html
+ :html
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def ==(o)
+ o.class == self.class && o.state == state
+ end
+ alias_method :eql?, :==
+
+ protected
+
+ def state
+ [@x, @y]
+ end
+ end
+
+ class Line
+ attr_reader :a, :b
+
+ def initialize(a, b)
+ @a = a
+ @b = b
+ end
+
+ def ==(o)
+ o.class == self.class && o.state == state
+ end
+ alias_method :eql?, :==
+
+ def left
+ options = [@a, @b]
+ options[compare]
+ end
+
+ def right
+ options = [@a, @b]
+ options[1 - compare]
+ end
+
+ protected
+
+ def compare
+ if @a.x < @b.x
+ 0
+ elsif @b.x < @a.x
+ 1
+ else
+ @a.y < @b.y ? 0 : 1
+ end
+ end
+
+ def state
+ [min(@a.x, @b.x), min(@a.y, @b.y), max(@a.x, @b.x), max(@a.y, @b.y)]
+ end
+ end
+
+ class Rectangle
+ attr_reader :a, :b
+
+ def initialize(a, b)
+ @a = a
+ @b = b
+ end
+
+ def ==(o)
+ o.class == self.class && o.state == state
+ end
+ alias_method :eql?, :==
+
+ def top_left
+ Point.new(min(@a.x, @b.x), min(@a.y, @b.y))
+ end
+
+ def top_right
+ Point.new(max(@a.x, @b.x), min(@a.y, @b.y))
+ end
+
+ def bottom_left
+ Point.new(min(@a.x, @b.x), max(@a.y, @b.y))
+ end
+
+ def bottom_right
+ Point.new(max(@a.x, @b.x), max(@a.y, @b.y))
+ end
+
+ def left
+ options = [@a, @b]
+ options[compare]
+ end
+
+ def right
+ options = [@a, @b]
+ options[1 - compare]
+ end
+
+ protected
+
+ def min(x, y)
+ x < y ? x : y
+ end
+
+ def max(x, y)
+ x < y ? y : x
+ end
+
+ def compare
+ if @a.x < @b.x
+ 0
+ elsif @b.x < @a.x
+ 1
+ else
+ @a.y < @b.y ? 0 : 1
+ end
+ end
+
+ def state
+ [left, right]
+ end
+ end
+end
+
+a = Graphics::Rectangle.new(Graphics::Point.new(1, 1), Graphics::Point.new(2, 4))
+c = Graphics::Canvas.new(3, 5)
+c.draw(a)
+c.render_as(Graphics::Renderers::Html)
+#puts Graphics::HtmlDrawer.beginning

Ясен обнови решението на 22.12.2013 23:25 (преди почти 11 години)

module Graphics
class HtmlDrawer
def self.beginning
heading + script + additional
end
def self.ending
<<END
</body>
</html>
END
end
def self.heading
<<END
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
END
end
def self.script
<<END
<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>
END
end
def self.additional
<<END
</head>
<body>
<div class="canvas">
END
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(width, height)
@width = width
@height = height
@canvas = Array.new(height) { Array.new(width) {0} }
end
def set_pixel(x, y)
@canvas[y][x] = 1
end
def pixel_at?(x, y)
@canvas[y][x] == 1 ? true : false
end
def draw(figure)
@canvas[figure.y][figure.x] = 1 if figure.class.to_s == "Graphics::Point"
draw_line figure if figure.class.to_s == "Graphics::Line"
draw_rectangle figure if figure.class.to_s == "Graphics::Rectangle"
end
def render_as(renderer)
if renderer.to_s == "Graphics::Renderers::Ascii"
render_ascii
elsif renderer.to_s == "Graphics::Renderers::Html"
render_html
end
end
private
def draw_line(line)
end
def draw_rectangle(rectangle)
x_range = (rectangle.top_left.x .. rectangle.top_right.x)
y_range = (rectangle.top_left.y .. rectangle.bottom_left.y)
x_range.each { |x| @canvas[rectangle.top_left.y][x] = 1 }
x_range.each { |x| @canvas[rectangle.bottom_left.y][x] = 1 }
y_range.each { |y| @canvas[y][rectangle.top_left.x] = 1 }
y_range.each { |y| @canvas[y][rectangle.top_right.x] = 1 }
end
def render_ascii
puts @canvas.map { |row| row.map { |cell| ascii_cell cell }.join }.join
end
def render_html
puts HtmlDrawer.beginning
puts @canvas.map { |row| row.map { |cell| html_cell cell }.join }.join '<br>'
puts HtmlDrawer.ending
end
def ascii_cell(cell)
cell == 0 ? '-' : '@'
end
def html_cell(cell)
cell == 0 ? '<i></i>' : '<b></b>'
end
end
module Renderers
module Ascii
:ascii
end
module Html
:html
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
protected
def state
[@x, @y]
end
end
class Line
attr_reader :a, :b
def initialize(a, b)
@a = a
@b = b
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
def left
options = [@a, @b]
options[compare]
end
def right
options = [@a, @b]
options[1 - compare]
end
protected
def compare
if @a.x < @b.x
0
elsif @b.x < @a.x
1
else
@a.y < @b.y ? 0 : 1
end
end
def state
[min(@a.x, @b.x), min(@a.y, @b.y), max(@a.x, @b.x), max(@a.y, @b.y)]
end
end
class Rectangle
attr_reader :a, :b
def initialize(a, b)
@a = a
@b = b
end
def ==(o)
o.class == self.class && o.state == state
end
alias_method :eql?, :==
def top_left
Point.new(min(@a.x, @b.x), min(@a.y, @b.y))
end
def top_right
Point.new(max(@a.x, @b.x), min(@a.y, @b.y))
end
def bottom_left
Point.new(min(@a.x, @b.x), max(@a.y, @b.y))
end
def bottom_right
Point.new(max(@a.x, @b.x), max(@a.y, @b.y))
end
def left
options = [@a, @b]
options[compare]
end
def right
options = [@a, @b]
options[1 - compare]
end
protected
def min(x, y)
x < y ? x : y
end
def max(x, y)
x < y ? y : x
end
def compare
if @a.x < @b.x
0
elsif @b.x < @a.x
1
else
@a.y < @b.y ? 0 : 1
end
end
def state
[left, right]
end
end
-end
-
+end
-a = Graphics::Rectangle.new(Graphics::Point.new(1, 1), Graphics::Point.new(2, 4))
-c = Graphics::Canvas.new(3, 5)
-c.draw(a)
-c.render_as(Graphics::Renderers::Html)
-#puts Graphics::HtmlDrawer.beginning

Бтв, за някакви такива дълги задачи (като гледам и на другите дължината на кода им гравитира около 200-300 реда) не е ли по-добре да се поставя просто линк към решението? Така ми се струва, че коментарите се губят, малко или много.