Решение на Трета задача от Петър Добрев

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

Към профила на Петър Добрев

Резултати

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

Код

module Graphics
module Renderers
class Ascii
end
class Html
end
end
class Point
def initialize(x, y)
@x = x
@y = y
end
def x
@x
end
def y
@y
end
def ==(other)
return true if other.x == @x and other.y == @y
false
end
alias_method :eql?, :==
def distance(other)
Math.sqrt((@x-other.x)*(@x-other.x) + (@y-other.y)*(@y-other.y))
end
end
class Line
def initialize(from, to)
set_from from, to
set_to from, to
end
def set_from(from, to)
@from = Point.new(from.x,[from.y,to.y].min) if from.x == to.x
@from = from if from.x < to.x
@from = to if from.x > to.x
end
def set_to(from, to)
@to = Point.new(from.x,[from.y,to.y].max) if from.x == to.x
@to = from if from.x > to.x
@to = to if from.x < to.x
end
def from
@from
end
def to
@to
end
def ==(other)
return true if other.from == @from and other.to == @to
false
end
alias_method :eql?, :==
private :set_from, :set_to
end
class Rectangle
def initialize(first, second)
set_left first, second
set_right first, second
set_vertices
end
def set_left(first, second)
@left = Point.new(first.x,[first.y,second.y].min) if first.x == second.x
@left = first if first.x < second.x
@left = second if first.x > second.x
end
def set_right(first, second)
@right = Point.new(first.x,[first.y,second.y].max) if first.x == second.x
@right = first if first.x > second.x
@right = second if first.x < second.x
end
def set_vertices
@top_left = Point.new([@left.x,@right.x].min,[@left.y,@right.y].min)
@top_right = Point.new([@left.x,@right.x].max,[@left.y,@right.y].min)
@bottom_left = Point.new([@left.x,@right.x].min,[@left.y,@right.y].max)
@bottom_right = Point.new([@left.x,@right.x].max,[@left.y,@right.y].max)
end
def top_left
@top_left
end
def top_right
@top_right
end
def bottom_left
@bottom_left
end
def bottom_right
@bottom_right
end
def ==(other)
return true if other.top_left == @top_left and other.bottom_right == @bottom_right
false
end
alias_method :eql?, :==
private :set_left, :set_right, :set_vertices
end
class Canvas
HTML_SYMBOL_LENGTH = 7
ASCII_CODE = {0 => '-', 1 => '@'}
HTML_CODE = {0 => '<i></i>', 1 => '<b></b>'}
HTML_BEGIN = ' <!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">'
HTML_END = ' </div>
</body>
</html>'
def initialize(width, height)
@width = width
@height = height
@pixels = Array.new(width*height, 0)
end
def width
@width
end
def height
@height
end
def set_pixel(x, y)
@pixels[x + @width*y] = 1
end
def pixel_at?(x, y)
@pixels[x + @width*y] == 1
end
def draw(figure)
set_pixel(figure.x,figure.y) if figure.class == Point
draw_line(figure.from,figure.to) if figure.class == Line
draw_rectangle(figure) if figure.class == Rectangle
end
def draw_line(from, to)
set_pixel(from.x, from.y)
set_pixel(to.x,to.y)
draw_sub_line(from, to)
end
def draw_sub_line(from, to)
return if from.distance(to) <= 1.5
return if from == to
mid_point = Point.new((from.x + to.x)/2, (from.y+to.y)/2)
set_pixel(mid_point.x, mid_point.y)
if not from == mid_point then draw_sub_line(from, mid_point) end
if not to == mid_point then draw_sub_line(mid_point, to) end
end
def draw_rectangle(rectangle)
draw_line(rectangle.top_left, rectangle.top_right)
draw_line(rectangle.top_right, rectangle.bottom_right)
draw_line(rectangle.bottom_right, rectangle.bottom_left)
draw_line(rectangle.bottom_left, rectangle.top_left)
end
def render_as(render_type)
return render_ascii if render_type == Renderers::Ascii
return render_html if render_type == Renderers::Html
end
def render_ascii
pixels_ascii = @pixels.map{ |x| ASCII_CODE[x]}.reduce(:+)
pixels_ascii.scan(/.{#{@width}}|.+/).join("\n")[0..-1]
end
def render_html
html_line_length = @width * HTML_SYMBOL_LENGTH
pixels_html = @pixels.map{ |x| HTML_CODE[x]}.reduce(:+)
pixels_html = pixels_html.scan(/.{#{html_line_length}}|.+/).join("<br>")[0..-1]
HTML_BEGIN + pixels_html + HTML_END
end
private :draw_line, :draw_rectangle, :draw_sub_line, :render_html, :render_ascii
end
end

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

........F.F..FFF...................F..............F...F.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: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@-----@@@-@-@\n@-@--@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
       
       (compared using ==)
       
       Diff:
       @@ -2,9 +2,9 @@
        @-------------@
        @-@@@@@@@@@@@-@
        @-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       +@-@-----@@@-@-@
       +@-@--@@@----@-@
       +@-@-@-------@-@
        @-@---------@-@
        @-@-@@@@----@-@
        @-@-@-------@-@
     # /tmp/d20131223-4637-gz1h0m/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gz1h0m/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 = canvas.render_as(Graphics::Renderers::Ascii)
     TypeError:
       no implicit conversion of nil into String
     # /tmp/d20131223-4637-gz1h0m/solution.rb:213:in `+'
     # /tmp/d20131223-4637-gz1h0m/solution.rb:213:in `each'
     # /tmp/d20131223-4637-gz1h0m/solution.rb:213:in `reduce'
     # /tmp/d20131223-4637-gz1h0m/solution.rb:213:in `render_ascii'
     # /tmp/d20131223-4637-gz1h0m/solution.rb:208:in `render_as'
     # /tmp/d20131223-4637-gz1h0m/spec.rb:623:in `check_rendering_of'
     # /tmp/d20131223-4637-gz1h0m/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-gz1h0m/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gz1h0m/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,11 +1,11 @@
        ----------
        -@--------
        -@--------
       +-@--------
        --@-------
        --@-------
        --@-------
        --@-------
       ----@------
        ---@------
        ----------
     # /tmp/d20131223-4637-gz1h0m/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gz1h0m/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-gz1h0m/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gz1h0m/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 Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -711854
            got: 219066780
       
       (compared using ==)
     # /tmp/d20131223-4637-gz1h0m/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)>'

  7) 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: 478916106
            got: -71789610
       
       (compared using ==)
     # /tmp/d20131223-4637-gz1h0m/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)>'

  8) Graphics shapes Rectangle initialization allows accessing its left and right points via getters
     Failure/Error: rect.left.x.should eq 3
     NoMethodError:
       undefined method `left' for #<Graphics::Rectangle:0xb9cba598>
     # /tmp/d20131223-4637-gz1h0m/spec.rb:490: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 shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: rect.left.x.should eq 4
     NoMethodError:
       undefined method `left' for #<Graphics::Rectangle:0xb9cb2ba4>
     # /tmp/d20131223-4637-gz1h0m/spec.rb:507: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 shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: -594323988
            got: -877012656
       
       (compared using ==)
     # /tmp/d20131223-4637-gz1h0m/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)>'

  11) 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: 858362464
            got: 246319716
       
       (compared using ==)
     # /tmp/d20131223-4637-gz1h0m/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.0883 seconds
69 examples, 11 failures

Failed examples:

rspec /tmp/d20131223-4637-gz1h0m/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-gz1h0m/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-gz1h0m/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:487 # Graphics shapes Rectangle initialization allows accessing its left and right points via getters
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-gz1h0m/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-gz1h0m/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 15:15 (преди почти 11 години)

+module Graphics
+
+ module Renderers
+ class Ascii
+ end
+ class Html
+ end
+ end
+
+ class Point
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def x
+ @x
+ end
+
+ def y
+ @y
+ end
+
+ def ==(other)
+ return true if other.x == @x and other.y == @y
+ false
+ end
+
+ alias_method :eql?, :==
+
+ def distance(other)
+ Math.sqrt((@x-other.x)*(@x-other.x) + (@y-other.y)*(@y-other.y))
+ end
+
+ end
+
+ class Line
+ def initialize(from, to)
+ set_from from, to
+ set_to from, to
+ end
+
+ def set_from(from, to)
+ @from = Point.new(from.x,[from.y,to.y].min) if from.x == to.x
+ @from = from if from.x < to.x
+ @from = to if from.x > to.x
+ end
+
+ def set_to(from, to)
+ @to = Point.new(from.x,[from.y,to.y].max) if from.x == to.x
+ @to = from if from.x > to.x
+ @to = to if from.x < to.x
+ end
+
+ def from
+ @from
+ end
+
+ def to
+ @to
+ end
+
+ def ==(other)
+ return true if other.from == @from and other.to == @to
+ false
+ end
+
+ alias_method :eql?, :==
+
+ private :set_from, :set_to
+ end
+
+ class Rectangle
+ def initialize(first, second)
+ set_left first, second
+ set_right first, second
+ set_vertices
+ end
+
+ def set_left(first, second)
+ @left = Point.new(first.x,[first.y,second.y].min) if first.x == second.x
+ @left = first if first.x < second.x
+ @left = second if first.x > second.x
+ end
+
+ def set_right(first, second)
+ @right = Point.new(first.x,[first.y,second.y].max) if first.x == second.x
+ @right = first if first.x > second.x
+ @right = second if first.x < second.x
+ end
+
+ def set_vertices
+ @top_left = Point.new([@left.x,@right.x].min,[@left.y,@right.y].min)
+ @top_right = Point.new([@left.x,@right.x].max,[@left.y,@right.y].min)
+ @bottom_left = Point.new([@left.x,@right.x].min,[@left.y,@right.y].max)
+ @bottom_right = Point.new([@left.x,@right.x].max,[@left.y,@right.y].max)
+ end
+
+ def top_left
+ @top_left
+ end
+
+ def top_right
+ @top_right
+ end
+
+ def bottom_left
+ @bottom_left
+ end
+
+ def bottom_right
+ @bottom_right
+ end
+
+ def ==(other)
+ return true if other.top_left == @top_left and other.bottom_right == @bottom_right
+ false
+ end
+
+ alias_method :eql?, :==
+
+ private :set_left, :set_right, :set_vertices
+ end
+
+ class Canvas
+ HTML_SYMBOL_LENGTH = 7
+ ASCII_CODE = {0 => '-', 1 => '@'}
+ HTML_CODE = {0 => '<i></i>', 1 => '<b></b>'}
+ HTML_BEGIN = ' <!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">'
+ HTML_END = ' </div>
+ </body>
+ </html>'
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @pixels = Array.new(width*height, 0)
+ end
+
+ def width
+ @width
+ end
+
+ def height
+ @height
+ end
+
+ def set_pixel(x, y)
+ @pixels[x + @width*y] = 1
+ end
+
+ def pixel_at?(x, y)
+ @pixels[x + @width*y] == 1
+ end
+
+ def draw(figure)
+ set_pixel(figure.x,figure.y) if figure.class == Point
+ draw_line(figure.from,figure.to) if figure.class == Line
+ draw_rectangle(figure) if figure.class == Rectangle
+ end
+
+ def draw_line(from, to)
+ set_pixel(from.x, from.y)
+ set_pixel(to.x,to.y)
+ draw_sub_line(from, to)
+ end
+
+ def draw_sub_line(from, to)
+ return if from.distance(to) <= 1.5
+ return if from == to
+ mid_point = Point.new((from.x + to.x)/2, (from.y+to.y)/2)
+ set_pixel(mid_point.x, mid_point.y)
+ if not from == mid_point then draw_sub_line(from, mid_point) end
+ if not to == mid_point then draw_sub_line(mid_point, to) end
+ end
+
+ def draw_rectangle(rectangle)
+ draw_line(rectangle.top_left, rectangle.top_right)
+ draw_line(rectangle.top_right, rectangle.bottom_right)
+ draw_line(rectangle.bottom_right, rectangle.bottom_left)
+ draw_line(rectangle.bottom_left, rectangle.top_left)
+ end
+
+ def render_as(render_type)
+ return render_ascii if render_type == Renderers::Ascii
+ return render_html if render_type == Renderers::Html
+ end
+
+ def render_ascii
+ pixels_ascii = @pixels.map{ |x| ASCII_CODE[x]}.reduce(:+)
+ pixels_ascii.scan(/.{#{@width}}|.+/).join("\n")[0..-1]
+ end
+
+ def render_html
+ html_line_length = @width * HTML_SYMBOL_LENGTH
+ pixels_html = @pixels.map{ |x| HTML_CODE[x]}.reduce(:+)
+ pixels_html = pixels_html.scan(/.{#{html_line_length}}|.+/).join("<br>")[0..-1]
+ HTML_BEGIN + pixels_html + HTML_END
+ end
+
+ private :draw_line, :draw_rectangle, :draw_sub_line, :render_html, :render_ascii
+
+ end
+end