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

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

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

Резултати

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

Код

module Graphics
START_HTML = "<!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_HTML = "</div>
</body>
</html>"
class Canvas
attr_reader :width, :height
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
end
def draw(figure)
if figure.instance_of? Point
@canvas[figure.y][figure.x] = 1
elsif figure.instance_of? Rectangle
draw_rectangle(figure)
else
draw_line(figure.from, figure.to)
end
end
def draw_rectangle(figure)
draw_line(figure.top_left, figure.top_right)
draw_line(figure.bottom_left, figure.bottom_right)
draw_line(figure.top_left, figure.bottom_left)
draw_line(figure.top_right, figure.bottom_right)
end
def draw_horizontal_line(from, to)
0.upto(width).each { |i| @canvas[from.y][i] = 1 if i >= from.x and i <= to.x }
end
def draw_vertical_line(from, to)
0.upto(height).each { |i| @canvas[i][from.x] = 1 if i >= from.y and i <= to.y}
end
def draw_line(from, to)
if from.y == to.y
draw_horizontal_line(from, to)
elsif from.x == to.x
draw_vertical_line(from, to)
else
bresenham(from.x, from.y, to.x, to.y)
end
end
#ne raboti kat horata
def bresenham(x_1, y_1, x_2, y_2)
x_1, y_1 ,x_2, y_2 = y_1, x_1, y_2, x_2 if steep = (y_2 - y_1).abs > (x_2 - x_1).abs
deltax, deltay, error, y = x_2 - x_1, y_2 - y_1, ((x_2-x_1) / 2).to_f, y_1
x_1.upto(x_2) do |a|
steep ? @canvas[a][y] = 1 : @canvas[y][a] = 1
error -= deltay
if error < 0 then y, error =y+1, error+deltax end
end
end
def render_as(renderer)
if renderer.to_s.match(/Ascii/)
render_as_ascii
else
render_as_html
end
end
def render_as_html
chars={0 => "<i></i>",1 => "<b></b>"}
html_string=@canvas.each_with_index.map do |row,i|
row.map {|e| chars[e] }.push("<br>").join
end.join
final=START_HTML + html_string + END_HTML
final[0..-31] + final[-26..-1]
end
def render_as_ascii
char_hash={0 => "-",1 => "@"}
@canvas.map {|row| row.map {|e| char_hash[e]}.push("\n").join}.join.chomp
end
end
module Renderers
class Ascii
end
class Html
end
end
class Point
attr_reader :x,:y
def initialize(x, y)
@x = x
@y = y
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
class Line
attr_reader :from, :to
def initialize(from, to)
if from.x == to.x
@from = from.y < to.y ? from : to
@to = @from == from ? to : from
else
@from = from.x < to.x ? from : to
@to = @from == from ? to : from
end
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].hash
end
end
class Rectangle
attr_reader :left, :right
def initialize(left, right)
if left.x == right.x
@left = left.y < right.y ? left : right
@right = @left == left ? right : left
else
@left = left.x < right.x ? left : right
@right = @left == left ? right : left
end
end
def top_left
Point.new([left.x, right.x].min,[left.y, right.y].min)
end
def top_right
Point.new(bottom_right.x, top_left.y)
end
def bottom_left
Point.new(top_left.x, bottom_right.y)
end
def bottom_right
Point.new([left.x, right.x].max, [left.y, right.y].max)
end
def ==(other)
top_left == other.top_left and bottom_right == other.bottom_right
end
def eql?(other)
top_left.eql? other.top_left and bottom_right.eql? other.bottom_right
end
def hash
[top_left, bottom_right].hash
end
end
end

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

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

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-y92a2t/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-y92a2t/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-y92a2t/solution.rb:42:in `set_pixel'
     # /tmp/d20131223-4637-y92a2t/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 Renderers Html returns html
     Failure/Error: rendering.should match /<divclass="canvas">/
       expected "<!doctypehtml><html><head><title>renderedcanvas</title><styletype='text/css'>.canvas{font-size:1px;line-height:1px;}.canvas*{display:inline-block;width:10px;height:10px;border-radius:5px;}.canvasi{background-color:#eee;}.canvasb{background-color:#333;}</style></head><body><divclass='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></div></body></html>" to match /<divclass="canvas">/
     # /tmp/d20131223-4637-y92a2t/spec.rb:277: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)>'

  4) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-y92a2t/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-y92a2t/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)>'

  5) Graphics Renderers Html renders simple canvases
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-y92a2t/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-y92a2t/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)>'

Finished in 0.08504 seconds
69 examples, 5 failures

Failed examples:

rspec /tmp/d20131223-4637-y92a2t/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-y92a2t/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-y92a2t/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-y92a2t/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-y92a2t/spec.rb:287 # Graphics Renderers Html renders simple canvases

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

Давид обнови решението на 22.12.2013 22:37 (преди почти 11 години)

+module Graphics
+ START_HTML="<!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_HTML="</div>
+ </body>
+ </html>"
+
+ class Canvas
+ attr_reader :width,:height, :matrix
+
+ def initialize(width,height)
+ @width=width
+ @height=height
+ @matrix=Array.new(@height) {Array.new(@width,0)}
+ end
+
+ def set_pixel(x,y)
+ @matrix[y][x]=1
+ end
+
+ def pixel_at?(x,y)
+ @matrix[y][x]==1
+ end
+
+ def draw(figure)
+ if figure.instance_of? Point
+ @matrix[figure.y][figure.x]=1
+ elsif figure.instance_of? Rectangle
+ draw_rectangle(figure)
+ else
+ draw_line(figure.from,figure.to)
+ end
+ end
+
+ def draw_rectangle(figure)
+ draw_line(figure.top_left,figure.top_right)
+ draw_line(figure.bottom_left,figure.bottom_right)
+ draw_line(figure.top_left,figure.bottom_left)
+ draw_line(figure.top_right,figure.bottom_right)
+ end
+
+ def draw_horizontal_line(from,to)
+ 0.upto(width).each { |i| @matrix[from.y][i]=1 if i>=from.x and i<=to.x }
+ end
+
+ def draw_vertical_line(from,to)
+ 0.upto(height).each { |i| @matrix[i][from.x]=1 if i>=from.y and i<=to.y}
+ end
+
+ def draw_line(from,to)
+ if from.y==to.y
+ draw_horizontal_line(from,to)
+ elsif from.x==to.x
+ draw_vertical_line(from,to)
+ else
+ bresenham(from.x,from.y,to.x,to.y)
+ end
+ end
+
+ #ne raboti kat horata
+ def bresenham(x_1,y_1,x_2,y_2)
+ x_1, y_1 ,x_2, y_2= y_1, x_1,y_2, x_2 if steep=(y_2 - y_1).abs > (x_2 - x_1).abs
+ deltax,deltay,error,y = x_2 - x_1,y_2 - y_1,((x_2-x_1) / 2).to_f,y_1
+ x_1.upto(x_2) do |a|
+ steep ? @matrix[a][y]=1 : @matrix[y][a]=1
+ error -= deltay
+ if error < 0 then y,error =y+1,error+deltax end
+ end
+ end
+
+ def render_as(renderer)
+ if renderer.to_s.match(/Ascii/)
+ render_as_ascii
+ else
+ render_as_html
+ end
+ end
+
+ def render_as_html
+ chars, max={0 => "<i></i>",1 => "<b></b>"},height-1
+ html_string=@matrix.each_with_index.map do |row,i|
+ row.map {|e| chars[e]}.push("<br>").join
+ end.join
+ START_HTML+html_string+END_HTML
+ end
+
+ def render_as_ascii
+ char_hash={0 => "-",1 => "@"}
+ @matrix.map {|row| row.map {|e| char_hash[e]}.push("\n").join}.join.chomp
+ end
+ end
+
+ module Renderers
+ class Ascii
+ end
+
+ class Html
+ end
+ end
+
+ class Point
+ attr_reader :x,:y
+ def initialize(x,y)
+ @x=x
+ @y=y
+ 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
+
+
+ class Line
+ attr_reader :from,:to
+ def initialize(from,to)
+ if from.x==to.x
+ @from=from.y<to.y ? from : to
+ @to= @from==from ? to : from
+ else
+ @from=from.x<to.x ? from : to
+ @to= @from==from ? to : from
+ end
+ 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].hash
+ end
+
+ end
+
+ class Rectangle
+ attr_reader :left,:right
+ def initialize(left,right)
+ if left.x==right.x
+ @left=left.y<right.y ? left : right
+ @right= @left==left ? right : left
+ else
+ @left=left.x<right.x ? left : right
+ @right= @left==left ? right : left
+ end
+ end
+
+ def top_left
+ Point.new([left.x,right.x].min,[left.y,right.y].min)
+ end
+
+ def top_right
+ Point.new(bottom_right.x,top_left.y)
+ end
+
+ def bottom_left
+ Point.new(top_left.x,bottom_right.y)
+ end
+
+ def bottom_right
+ Point.new([left.x,right.x].max,[left.y,right.y].max)
+ end
+
+
+ def ==(other)
+ top_left==other.top_left and bottom_right==other.bottom_right
+ end
+
+ def eql?(other)
+ top_left.eql? other.top_left and bottom_right.eql? other.bottom_right
+ end
+
+ def hash
+ [top_left,bottom_right].hash
+ end
+
+ end
+end

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

module Graphics
- START_HTML="<!DOCTYPE html>
+ START_HTML = "<!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_HTML="</div>
+ END_HTML = "</div>
</body>
</html>"
class Canvas
- attr_reader :width,:height, :matrix
+ attr_reader :width, :height
- def initialize(width,height)
- @width=width
- @height=height
- @matrix=Array.new(@height) {Array.new(@width,0)}
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @canvas = Array.new(@height) { Array.new(@width, 0) }
end
- def set_pixel(x,y)
- @matrix[y][x]=1
+ def set_pixel(x, y)
+ @canvas[y][x] = 1
end
- def pixel_at?(x,y)
- @matrix[y][x]==1
+ def pixel_at?(x, y)
+ @canvas[y][x] == 1
end
def draw(figure)
if figure.instance_of? Point
- @matrix[figure.y][figure.x]=1
+ @canvas[figure.y][figure.x] = 1
elsif figure.instance_of? Rectangle
draw_rectangle(figure)
else
- draw_line(figure.from,figure.to)
+ draw_line(figure.from, figure.to)
end
end
def draw_rectangle(figure)
- draw_line(figure.top_left,figure.top_right)
- draw_line(figure.bottom_left,figure.bottom_right)
- draw_line(figure.top_left,figure.bottom_left)
- draw_line(figure.top_right,figure.bottom_right)
+ draw_line(figure.top_left, figure.top_right)
+ draw_line(figure.bottom_left, figure.bottom_right)
+ draw_line(figure.top_left, figure.bottom_left)
+ draw_line(figure.top_right, figure.bottom_right)
end
- def draw_horizontal_line(from,to)
- 0.upto(width).each { |i| @matrix[from.y][i]=1 if i>=from.x and i<=to.x }
+ def draw_horizontal_line(from, to)
+ 0.upto(width).each { |i| @canvas[from.y][i] = 1 if i >= from.x and i <= to.x }
end
- def draw_vertical_line(from,to)
- 0.upto(height).each { |i| @matrix[i][from.x]=1 if i>=from.y and i<=to.y}
+ def draw_vertical_line(from, to)
+ 0.upto(height).each { |i| @canvas[i][from.x] = 1 if i >= from.y and i <= to.y}
end
- def draw_line(from,to)
- if from.y==to.y
- draw_horizontal_line(from,to)
- elsif from.x==to.x
- draw_vertical_line(from,to)
+ def draw_line(from, to)
+ if from.y == to.y
+ draw_horizontal_line(from, to)
+ elsif from.x == to.x
+ draw_vertical_line(from, to)
else
- bresenham(from.x,from.y,to.x,to.y)
+ bresenham(from.x, from.y, to.x, to.y)
end
end
#ne raboti kat horata
- def bresenham(x_1,y_1,x_2,y_2)
- x_1, y_1 ,x_2, y_2= y_1, x_1,y_2, x_2 if steep=(y_2 - y_1).abs > (x_2 - x_1).abs
- deltax,deltay,error,y = x_2 - x_1,y_2 - y_1,((x_2-x_1) / 2).to_f,y_1
+ def bresenham(x_1, y_1, x_2, y_2)
+ x_1, y_1 ,x_2, y_2 = y_1, x_1, y_2, x_2 if steep = (y_2 - y_1).abs > (x_2 - x_1).abs
+ deltax, deltay, error, y = x_2 - x_1, y_2 - y_1, ((x_2-x_1) / 2).to_f, y_1
x_1.upto(x_2) do |a|
- steep ? @matrix[a][y]=1 : @matrix[y][a]=1
+ steep ? @canvas[a][y] = 1 : @canvas[y][a] = 1
error -= deltay
- if error < 0 then y,error =y+1,error+deltax end
+ if error < 0 then y, error =y+1, error+deltax end
end
end
def render_as(renderer)
if renderer.to_s.match(/Ascii/)
render_as_ascii
else
render_as_html
end
end
def render_as_html
- chars, max={0 => "<i></i>",1 => "<b></b>"},height-1
- html_string=@matrix.each_with_index.map do |row,i|
- row.map {|e| chars[e]}.push("<br>").join
+ chars={0 => "<i></i>",1 => "<b></b>"}
+ html_string=@canvas.each_with_index.map do |row,i|
+ row.map {|e| chars[e] }.push("<br>").join
end.join
- START_HTML+html_string+END_HTML
+ final=START_HTML + html_string + END_HTML
+ final[0..-31] + final[-26..-1]
end
def render_as_ascii
char_hash={0 => "-",1 => "@"}
- @matrix.map {|row| row.map {|e| char_hash[e]}.push("\n").join}.join.chomp
+ @canvas.map {|row| row.map {|e| char_hash[e]}.push("\n").join}.join.chomp
end
end
module Renderers
class Ascii
end
class Html
end
end
class Point
attr_reader :x,:y
- def initialize(x,y)
- @x=x
- @y=y
+ def initialize(x, y)
+ @x = x
+ @y = y
end
def ==(other)
- x==other.x and y==other.y
+ 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
+ [x, y].hash
end
end
class Line
- attr_reader :from,:to
- def initialize(from,to)
- if from.x==to.x
- @from=from.y<to.y ? from : to
- @to= @from==from ? to : from
+ attr_reader :from, :to
+ def initialize(from, to)
+ if from.x == to.x
+ @from = from.y < to.y ? from : to
+ @to = @from == from ? to : from
else
- @from=from.x<to.x ? from : to
- @to= @from==from ? to : from
+ @from = from.x < to.x ? from : to
+ @to = @from == from ? to : from
end
end
def ==(other)
- from==other.from and to==other.to
+ 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].hash
+ [from, to].hash
end
end
class Rectangle
- attr_reader :left,:right
- def initialize(left,right)
- if left.x==right.x
- @left=left.y<right.y ? left : right
- @right= @left==left ? right : left
+ attr_reader :left, :right
+ def initialize(left, right)
+ if left.x == right.x
+ @left = left.y < right.y ? left : right
+ @right = @left == left ? right : left
else
- @left=left.x<right.x ? left : right
- @right= @left==left ? right : left
+ @left = left.x < right.x ? left : right
+ @right = @left == left ? right : left
end
end
def top_left
- Point.new([left.x,right.x].min,[left.y,right.y].min)
+ Point.new([left.x, right.x].min,[left.y, right.y].min)
end
def top_right
- Point.new(bottom_right.x,top_left.y)
+ Point.new(bottom_right.x, top_left.y)
end
def bottom_left
- Point.new(top_left.x,bottom_right.y)
+ Point.new(top_left.x, bottom_right.y)
end
def bottom_right
- Point.new([left.x,right.x].max,[left.y,right.y].max)
+ Point.new([left.x, right.x].max, [left.y, right.y].max)
end
def ==(other)
- top_left==other.top_left and bottom_right==other.bottom_right
+ top_left == other.top_left and bottom_right == other.bottom_right
end
def eql?(other)
top_left.eql? other.top_left and bottom_right.eql? other.bottom_right
end
def hash
- [top_left,bottom_right].hash
+ [top_left, bottom_right].hash
end
end
end