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

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

Към профила на Цани Проданов

Резултати

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

Код

module Graphics
#ако исках да рисувам на пано
#щях да накарам майка ми да ме научи
class Canvas
attr_reader :width, :height
def initialize (width, height)
@width = width
@height = height
@buffer = Array.new(height) { "0"*width }
end
def draw (figure)
set_all(figure.get_pixels)
end
def set_pixel (x, y)
@buffer[y][x] = "1"
end
def set_all (pixels)
pixels.each do |pixel|
set_pixel(pixel.x, pixel.y)
end
end
def pixel_at (x, y)
@buffer[x][y] == "1"
end
def render_as (renderer)
renderer.render(self)
end
def get_parsed_buffer (separator, empty, filled)
@buffer.join(separator).gsub("0",empty).gsub("1",filled)
end
end
module Renderers
class Ascii
def self.render (canvas)
canvas.get_parsed_buffer("\n","-","@")
end
end
class Html
@@top_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">'
@@bottom_html ='</div>
</body>
</html>'
def self.render (canvas)
@@TOP_HTML + canvas.get_parsed_buffer("<br>","<i></i>","<b></b>") + @@BOTTOM_HTML
end
end
end
class Point
attr_reader :x, :y
def initialize (x, y)
@x = x
@y = y
end
def get_pixels
[self]
end
def hash
[x, y].hash
end
def distance other
@x - other.x + @y - other.y
end
def == other
hash == other.hash
end
alias_method :eql?, :==
end
class Line
attr_reader :from, :to
def initialize (a, b)
@from = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
@to = @from.equal?(a) ? b : a
@top = (a.y < b.y || (a.y == b.y && a.x < b.x)) ? a : b
@bottom = @top.equal?(a) ? b : a
end
def get_pixels
return get_pixels_by_row if @to.x == @from.x
slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
return get_pixels_by_column if slope.abs < 1
return get_pixels_by_row
end
def get_pixels_by_row
slope = (@to.x.to_f - @from.x)/(@to.y - @from.y)
pixels = []
(@top.y..@bottom.y).each do |row|
pixels << Point.new((slope*(row - @from.y) + @from.x).round, row)
end
pixels
end
def get_pixels_by_column
slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
pixels = []
(@from.x..@to.x).each do |column|
pixels << Point.new(column, (slope*(column - @from.x) + @from.y).round)
end
pixels
end
def hash
@from.hash + @from.distance(@to)*@to.hash
end
def == other
hash == other.hash
end
alias_method :eql?, :==
private :get_pixels_by_row, :get_pixels_by_column
end
class Rectangle
attr_reader :left, :right, :top_left, :top_right, :bottom_left, :bottom_right
def initialize (a, b)
@left = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
@right = @left == a ? b : a
@top_left = Point.new(@left.x, @left.y < @right.y ? @left.y : @right.y)
@top_right = Point.new(@right.x, @left.y < @right.y ? @left.y : @right.y)
@bottom_left = Point.new(@left.x, @left.y >= @right.y ? @left.y : @right.y)
@bottom_right = Point.new(@right.x, @left.y >= @right.y ? @left.y : @right.y)
end
def hash
hash = @left.hash
hash += @left.distance(@right)*@right.hash
walls_hash = @top_left.distance(@bottom_left)*@bottom_left.hash
walls_hash *= @top_left.distance(@top_right)*@top_right.hash
hash += walls_hash
end
def get_pixels
pixels = []
#add_top pixels, ама това се събира в 90 символа
(@top_left.x..@top_right.x).each {|column| pixels << Point.new(column,@top_left.y)}
add_walls pixels #<3 макс 6 реда и макс 90 символа
add_bottom pixels
pixels
end
def add_walls pixels
(@top_left.y+1..@bottom_left.y-1).each do |row|
pixels<<Point.new(@left.x,row)<<Point.new(@right.x,row)
end
end
def add_bottom pixels
(@bottom_left.x..@bottom_right.x).each do |column|
pixels << Point.new(column,@bottom_left.y)
end
end
def == other
hash == other.hash
end
alias_method :eql?, :==
private :add_walls, :add_bottom
end
end

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

.....F.F.FF.....F.......FFFF................................F..F.....

Failures:

  1) Graphics Canvas allows checking if a pixel at a given x and y is set
     Failure/Error: canvas.pixel_at?(0, 0).should be_false
     NoMethodError:
       undefined method `pixel_at?' for #<Graphics::Canvas:0xb89ec524>
     # /tmp/d20131223-4637-1yk577c/spec.rb:29: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)>'

  2) Graphics Canvas allows setting a pixel at a given x and y
     Failure/Error: canvas.pixel_at?(3, 5).should be_false
     NoMethodError:
       undefined method `pixel_at?' for #<Graphics::Canvas:0xb89e623c>
     # /tmp/d20131223-4637-1yk577c/spec.rb:38: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)>'

  3) Graphics Canvas drawing of shapes and rasterization of points works for a single one
     Failure/Error: canvas.pixel_at?(2, 4).should be_false
     NoMethodError:
       undefined method `pixel_at?' for #<Graphics::Canvas:0xb89cd250>
     # /tmp/d20131223-4637-1yk577c/spec.rb:46: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 points works for multiple ones
     Failure/Error: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-1yk577c/solution.rb:18:in `set_pixel'
     # /tmp/d20131223-4637-1yk577c/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)>'

  5) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: canvas.draw make_line(make_point(1, 1), make_point(1, 1))
     FloatDomainError:
       NaN
     # /tmp/d20131223-4637-1yk577c/solution.rb:126:in `round'
     # /tmp/d20131223-4637-1yk577c/solution.rb:126:in `block in get_pixels_by_row'
     # /tmp/d20131223-4637-1yk577c/solution.rb:125:in `each'
     # /tmp/d20131223-4637-1yk577c/solution.rb:125:in `get_pixels_by_row'
     # /tmp/d20131223-4637-1yk577c/solution.rb:116:in `get_pixels'
     # /tmp/d20131223-4637-1yk577c/solution.rb:14:in `draw'
     # /tmp/d20131223-4637-1yk577c/spec.rb:143: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 Renderers Html returns html
     Failure/Error: rendering = normalize_html canvas.render_as(html)
     NameError:
       uninitialized class variable @@TOP_HTML in Graphics::Renderers::Html
     # /tmp/d20131223-4637-1yk577c/solution.rb:76:in `render'
     # /tmp/d20131223-4637-1yk577c/solution.rb:32:in `render_as'
     # /tmp/d20131223-4637-1yk577c/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)>'

  7) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: rendering = normalize_html canvas.render_as(html)
     NameError:
       uninitialized class variable @@TOP_HTML in Graphics::Renderers::Html
     # /tmp/d20131223-4637-1yk577c/solution.rb:76:in `render'
     # /tmp/d20131223-4637-1yk577c/solution.rb:32:in `render_as'
     # /tmp/d20131223-4637-1yk577c/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1yk577c/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)>'

  8) Graphics Renderers Html renders simple canvases
     Failure/Error: rendering = normalize_html canvas.render_as(html)
     NameError:
       uninitialized class variable @@TOP_HTML in Graphics::Renderers::Html
     # /tmp/d20131223-4637-1yk577c/solution.rb:76:in `render'
     # /tmp/d20131223-4637-1yk577c/solution.rb:32:in `render_as'
     # /tmp/d20131223-4637-1yk577c/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1yk577c/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)>'

  9) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: first_rendering  = normalize_html canvas.render_as(html)
     NameError:
       uninitialized class variable @@TOP_HTML in Graphics::Renderers::Html
     # /tmp/d20131223-4637-1yk577c/solution.rb:76:in `render'
     # /tmp/d20131223-4637-1yk577c/solution.rb:32:in `render_as'
     # /tmp/d20131223-4637-1yk577c/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)>'

  10) 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-1yk577c/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)>'

  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: -3669228260844549682
            got: -3669228255636136506
       
       (compared using ==)
     # /tmp/d20131223-4637-1yk577c/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.08425 seconds
69 examples, 11 failures

Failed examples:

rspec /tmp/d20131223-4637-1yk577c/spec.rb:28 # Graphics Canvas allows checking if a pixel at a given x and y is set
rspec /tmp/d20131223-4637-1yk577c/spec.rb:37 # Graphics Canvas allows setting a pixel at a given x and y
rspec /tmp/d20131223-4637-1yk577c/spec.rb:45 # Graphics Canvas drawing of shapes and rasterization of points works for a single one
rspec /tmp/d20131223-4637-1yk577c/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-1yk577c/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-1yk577c/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-1yk577c/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1yk577c/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-1yk577c/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-1yk577c/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-1yk577c/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

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

+module Graphics
+ class Canvas #ако исках да рисувам на пано щях да кажа на майка ми да ме научи
+ attr_reader :width, :height
+
+ def initialize (width, height)
+ @width = width
+ @height = height
+ @buffer = Array.new(height) { "0"*width }
+ end
+
+ def draw (figure)
+ set_all(figure.get_pixels)
+ end
+
+ def set_pixel (x, y)
+ @buffer[y][x] = "1"
+ end
+
+ def set_all (pixels)
+ pixels.each do |pixel|
+ set_pixel(pixel.x, pixel.y)
+ end
+ end
+
+ def pixel_at (x, y)
+ @buffer[x][y] == "1"
+ end
+
+ def render_as (renderer)
+ renderer.render(self)
+ end
+
+ def get_parsed_buffer (separator, empty, filled)
+ @buffer.join(separator).gsub("0",empty).gsub("1",filled)
+ end
+ end
+ module Renderers
+ class Ascii
+ def self.render (canvas)
+ canvas.get_parsed_buffer("\n","-","@")
+ end
+ end
+ class Html
+ @@top_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">'
+ @@bottom_html ='</div>
+ </body>
+ </html>'
+
+ def self.render (canvas)
+ @@TOP_HTML + canvas.get_parsed_buffer("<br>","<i></i>","<b></b>") + @@BOTTOM_HTML
+ end
+ end
+ end
+ class Point
+ attr_reader :x, :y
+ def initialize (x, y)
+ @x = x
+ @y = y
+ end
+
+ def get_pixels
+ [self]
+ end
+
+ def hash
+ [x, y].hash
+ end
+
+ def distance other
+ @x - other.x + @y - other.y
+ end
+
+ def == other
+ hash == other.hash
+ end
+
+ alias_method :eql?, :==
+ end
+
+ class Line
+ attr_reader :from, :to
+ def initialize (a, b)
+ @from = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
+ @to = @from.equal?(a) ? b : a
+ @top = (a.y < b.y || (a.y == b.y && a.x < b.x)) ? a : b
+ @bottom = @top.equal?(a) ? b : a
+ end
+
+ def get_pixels
+ return get_pixels_by_row if @to.x == @from.x
+ slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
+ return get_pixels_by_column if slope.abs < 1
+ return get_pixels_by_row
+ end
+
+ def get_pixels_by_row
+ slope = (@to.x.to_f - @from.x)/(@to.y - @from.y)
+ pixels = []
+ (@top.y..@bottom.y).each do |row|
+ pixels << Point.new((slope*(row - @from.y) + @from.x).round, row)
+ end
+ pixels
+ end
+
+ def get_pixels_by_column
+ slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
+ pixels = []
+ (@from.x..@to.x).each do |column|
+ pixels << Point.new(column, (slope*(column - @from.x) + @from.y).round)
+ end
+ pixels
+ end
+
+ def hash
+ @from.hash + @from.distance(@to)*@to.hash
+ end
+ end
+ class Rectangle
+ attr_reader :left, :right, :top_left, :top_right, :bottom_left, :bottom_right
+ def initialize (a, b)
+ @left = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
+ @right = @left == a ? b : a
+ @top_left = Point.new(@left.x, @left.y < @right.y ? @left.y : @right.y)
+ @top_right = Point.new(@right.x, @left.y < @right.y ? @left.y : @right.y)
+ @bottom_left = Point.new(@left.x, @left.y >= @right.y ? @left.y : @right.y)
+ @bottom_right = Point.new(@right.x, @left.y >= @right.y ? @left.y : @right.y)
+ end
+
+ def hash
+ hash = @left.hash
+ hash += @left.distance(@right)*@right.hash
+ walls_hash = @top_left.distance(@bottom_left)*@bottom_left.hash
+ walls_hash *= @top_left.distance(@top_right)*@top_right.hash
+ hash += walls_hash
+ end
+
+ def get_pixels
+ pixels = []
+ #add_top pixels, ама това се събира в 90 символа
+ (@top_left.x..@top_right.x).each {|column| pixels << Point.new(column,@top_left.y)}
+ add_walls pixels #<3 макс 6 реда и макс 90 символа
+ add_bottom pixels
+ pixels
+ end
+
+ def add_walls pixels
+ (@top_left.y+1..@bottom_left.y-1).each do |row|
+ pixels<<Point.new(@left.x,row)<<Point.new(@right.x,row)
+ end
+ end
+
+ def add_bottom pixels
+ (@bottom_left.x..@bottom_right.x).each do |column|
+ pixels << Point.new(column,@bottom_left.y)
+ end
+ end
+
+ private :add_walls, :add_bottom
+ end
+end

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

module Graphics
- class Canvas #ако исках да рисувам на пано щях да кажа на майка ми да ме научи
+ #ако исках да рисувам на пано
+ #щях да накарам майка ми да ме научи
+ class Canvas
attr_reader :width, :height
def initialize (width, height)
@width = width
@height = height
@buffer = Array.new(height) { "0"*width }
end
def draw (figure)
set_all(figure.get_pixels)
end
def set_pixel (x, y)
@buffer[y][x] = "1"
end
def set_all (pixels)
pixels.each do |pixel|
set_pixel(pixel.x, pixel.y)
end
end
def pixel_at (x, y)
@buffer[x][y] == "1"
end
def render_as (renderer)
renderer.render(self)
end
def get_parsed_buffer (separator, empty, filled)
@buffer.join(separator).gsub("0",empty).gsub("1",filled)
end
end
module Renderers
class Ascii
def self.render (canvas)
canvas.get_parsed_buffer("\n","-","@")
end
end
class Html
@@top_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">'
@@bottom_html ='</div>
</body>
</html>'
def self.render (canvas)
@@TOP_HTML + canvas.get_parsed_buffer("<br>","<i></i>","<b></b>") + @@BOTTOM_HTML
end
end
end
class Point
attr_reader :x, :y
def initialize (x, y)
@x = x
@y = y
end
def get_pixels
[self]
end
def hash
[x, y].hash
end
def distance other
@x - other.x + @y - other.y
end
def == other
hash == other.hash
end
alias_method :eql?, :==
end
class Line
attr_reader :from, :to
def initialize (a, b)
@from = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
@to = @from.equal?(a) ? b : a
@top = (a.y < b.y || (a.y == b.y && a.x < b.x)) ? a : b
@bottom = @top.equal?(a) ? b : a
end
def get_pixels
return get_pixels_by_row if @to.x == @from.x
slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
return get_pixels_by_column if slope.abs < 1
return get_pixels_by_row
end
def get_pixels_by_row
slope = (@to.x.to_f - @from.x)/(@to.y - @from.y)
pixels = []
(@top.y..@bottom.y).each do |row|
pixels << Point.new((slope*(row - @from.y) + @from.x).round, row)
end
pixels
end
def get_pixels_by_column
slope = (@to.y.to_f - @from.y)/(@to.x - @from.x)
pixels = []
(@from.x..@to.x).each do |column|
pixels << Point.new(column, (slope*(column - @from.x) + @from.y).round)
end
pixels
end
def hash
@from.hash + @from.distance(@to)*@to.hash
end
+
+ def == other
+ hash == other.hash
+ end
+
+ alias_method :eql?, :==
+ private :get_pixels_by_row, :get_pixels_by_column
end
class Rectangle
attr_reader :left, :right, :top_left, :top_right, :bottom_left, :bottom_right
def initialize (a, b)
@left = (a.x < b.x || (a.x == b.x && a.y < b.y)) ? a : b
@right = @left == a ? b : a
@top_left = Point.new(@left.x, @left.y < @right.y ? @left.y : @right.y)
@top_right = Point.new(@right.x, @left.y < @right.y ? @left.y : @right.y)
@bottom_left = Point.new(@left.x, @left.y >= @right.y ? @left.y : @right.y)
@bottom_right = Point.new(@right.x, @left.y >= @right.y ? @left.y : @right.y)
end
def hash
hash = @left.hash
hash += @left.distance(@right)*@right.hash
walls_hash = @top_left.distance(@bottom_left)*@bottom_left.hash
walls_hash *= @top_left.distance(@top_right)*@top_right.hash
hash += walls_hash
end
def get_pixels
pixels = []
#add_top pixels, ама това се събира в 90 символа
(@top_left.x..@top_right.x).each {|column| pixels << Point.new(column,@top_left.y)}
add_walls pixels #<3 макс 6 реда и макс 90 символа
add_bottom pixels
pixels
end
def add_walls pixels
(@top_left.y+1..@bottom_left.y-1).each do |row|
pixels<<Point.new(@left.x,row)<<Point.new(@right.x,row)
end
end
def add_bottom pixels
(@bottom_left.x..@bottom_right.x).each do |column|
pixels << Point.new(column,@bottom_left.y)
end
end
+ def == other
+ hash == other.hash
+ end
+
+ alias_method :eql?, :==
private :add_walls, :add_bottom
end
end