Решение на Трета задача от Венцислав Велков

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

Към профила на Венцислав Велков

Резултати

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

Код

module Graphics
class Canvas
def initialize (width, height)
@width = width
@height = height
@pixels = []
end
def width
@width
end
def height
@height
end
def set_pixel (x, y)
@pixels = (@pixels + [[x, y]]).uniq
end
def pixel_at? (x, y)
@pixels.include? [x, y]
end
def draw (form)
@pixels = (@pixels + form.pixels).uniq
end
def pixels
@pixels
end
def render_as (renderer)
renderer.print(@pixels, @width, @height)
end
end
module Renderers
class Ascii
def self.print(pixels, columns, rows)
result = ""
matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
matrix.each do |i|
(pixels.include? i.reverse) ? result << "@" : result << "-"
end
rows.downto(1).each { |row| result.insert(columns*row, "\n") }
result.chop
end
end
class Html
def self.print(pixels, columns, rows)
result, 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">
'
matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
matrix.each do |i|
(pixels.include? i.reverse) ? result << "<b></b>" : result << "<i></i>"
end
rows.downto(1).each { |row| result.insert(columns*7*row, "<br>\n") }
html << result.chomp("<br>\n") << "\n" << '</div>
</body>
</html>'
end
end
end
class Point
def initialize (x, y)
@x = x
@y = y
@pixels = [[x, y]]
end
def x
@x
end
def y
@y
end
def pixels
@pixels
end
def ==(other)
@x == other.x and @y = other.y ? true : false
end
def eql?(other)
@x == other.x and @y = other.y ? true : false
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
end
class Line
def initialize (from, to)
@from = Point.new(from.x, from.y)
@to = Point.new(to.x, to.y)
@pixels = [[@from.x, @from.y]]
self.rasterize
end
def from
if @from.x < @to.x then return @from end
if @from.x > @to.x then return @to end
@from.y < @to.y ? @from : @to
end
def to
if @from.x > @to.x then return @from end
if @from.x < @to.x then return @to end
@from.y > @to.y ? @from : @to
end
def ==(other)
@from == other.from and @to == other.to ? true : false
end
def eql?(other)
@from == other.from and @to == other.to ? true : false
end
def pixels
@pixels
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
protected
def rasterize
x, y = @from.x, @from.y
delta_x, delta_y = @to.x - x, @to.y - y
until x.round == @to.x and y.round == @to.y do
x = x + delta_x.to_f/[delta_x.abs, delta_y.abs].max
y = y + delta_y.to_f/[delta_x.abs, delta_y.abs].max
@pixels << [x.round, y.round]
end
end
end
class Rectangle
def initialize (p_1, p_2)
@top_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].min))
@top_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].min))
@bottom_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].max))
@bottom_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].max))
self.set_pixels
end
def left
@top_left
end
def right
@bottom_right
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 pixels
@pixels
end
def ==(other)
@pixels.uniq.sort == other.pixels.uniq.sort
end
def eql?(other)
@pixels.uniq.sort == other.pixels.uniq.sort
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
protected
def set_pixels
@pixels = Line.new(@top_left, @top_right).pixels
@pixels = @pixels + Line.new(@top_right, @bottom_right).pixels
@pixels = @pixels + Line.new(@bottom_right, @bottom_left).pixels
@pixels = (@pixels + Line.new(@bottom_left, @top_left).pixels).uniq
end
end
end

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

.................................FF..........F..........F............

Failures:

  1) Graphics shapes Point comparison for equality is false if coordinates differ
     Failure/Error: (a1 == b).should be_false
       expected: false value
            got: true
     # /tmp/d20131223-4637-1tpo3aq/spec.rb:352: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)>'

  2) Graphics shapes Point comparison for equality works for eql? as well
     Failure/Error: a1.should_not eql b
       
       expected: value != #<Graphics::Point:0xb9d830d8 @x=4, @y=4, @pixels=[[4, 4]]>
            got: #<Graphics::Point:0xb9d88e20 @x=4, @y=true, @pixels=[[4, 5]]>
       
       (compared using eql?)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Graphics::Point:0xb9d830d8 @pixels=[[4, 4]], @x=4, @y=4>
       +#<Graphics::Point:0xb9d88e20 @pixels=[[4, 5]], @x=4, @y=true>
     # /tmp/d20131223-4637-1tpo3aq/spec.rb:357: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 shapes Line comparison for equality is false if any of the points differ
     Failure/Error: (a == b).should be_false
       expected: false value
            got: true
     # /tmp/d20131223-4637-1tpo3aq/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)>'

  4) Graphics shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: rect.left.y.should eq 1
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20131223-4637-1tpo3aq/spec.rb:508: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.13691 seconds
69 examples, 4 failures

Failed examples:

rspec /tmp/d20131223-4637-1tpo3aq/spec.rb:351 # Graphics shapes Point comparison for equality is false if coordinates differ
rspec /tmp/d20131223-4637-1tpo3aq/spec.rb:355 # Graphics shapes Point comparison for equality works for eql? as well
rspec /tmp/d20131223-4637-1tpo3aq/spec.rb:424 # Graphics shapes Line comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-1tpo3aq/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field

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

Венцислав обнови решението на 22.12.2013 16:34 (преди почти 11 години)

+module Graphics
+ class Canvas
+ def initialize (width, height)
+ @width = width
+ @height = height
+ @pixels = []
+ end
+
+ def width
+ @width
+ end
+
+ def height
+ @height
+ end
+
+ def set_pixel (x, y)
+ @pixels = (@pixels + [[x, y]]).uniq
+ end
+
+ def pixel_at? (x, y)
+ @pixels.include? [x, y]
+ end
+
+ def draw (form)
+ @pixels = (@pixels + form.pixels).uniq
+ end
+
+ def pixels
+ @pixels
+ end
+
+ def render_as (renderer)
+ renderer.print(@pixels, @width, @height)
+ end
+ end
+
+ module Renderers
+ class Ascii
+ def self.print(pixels, columns, rows)
+ result = ""
+ matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
+ matrix.each { |i| (pixels.include? i.reverse) ? result << "@" : result << "-" }
+ rows.downto(1).each { |row| result.insert(columns*row, "\n") }
+ result.chop
+ end
+ end
+
+ class Html
+ def self.print(pixels, columns, rows)
+ result, 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">
+'
+ matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
+ matrix.each do |i|
+ (pixels.include? i.reverse) ? result << "<b></b>" : result << "<i></i>"
+ end
+ rows.downto(1).each { |row| result.insert(columns*7*row, "<br>\n") }
+ html << result.chomp("<br>\n") << "\n" << '</div>
+ </body>
+ </html>'
+ end
+ end
+ end
+
+ class Point
+ def initialize (x, y)
+ @x = x
+ @y = y
+ @pixels = [[x, y]]
+ end
+
+ def x
+ @x
+ end
+
+ def y
+ @y
+ end
+
+ def pixels
+ @pixels
+ end
+
+ def ==(other)
+ @x == other.x and @y = other.y ? true : false
+ end
+
+ def eql?(other)
+ @x == other.x and @y = other.y ? true : false
+ end
+
+ def hash
+ hash_code = 0
+ @pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
+ hash_code.hash
+ end
+ end
+
+ class Line
+ def initialize (from, to)
+ @from = Point.new(from.x, from.y)
+ @to = Point.new(to.x, to.y)
+ @pixels = [[@from.x, @from.y]]
+ self.rasterize
+ end
+
+ def from
+ if @from.x < @to.x then return @from end
+ if @from.x > @to.x then return @to end
+ @from.y < @to.y ? @from : @to
+ end
+
+ def to
+ if @from.x > @to.x then return @from end
+ if @from.x < @to.x then return @to end
+ @from.y > @to.y ? @from : @to
+ end
+
+ def ==(other)
+ @from == other.from and @to == other.to ? true : false
+ end
+
+ def eql?(other)
+ @from == other.from and @to == other.to ? true : false
+ end
+
+ def pixels
+ @pixels
+ end
+
+ def hash
+ hash_code = 0
+ @pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
+ hash_code.hash
+ end
+
+ protected
+ def rasterize
+ x, y = @from.x, @from.y
+ delta_x, delta_y = @to.x - x, @to.y - y
+ until x.round == @to.x and y.round == @to.y do
+ x = x + delta_x.to_f/[delta_x.abs, delta_y.abs].max
+ y = y + delta_y.to_f/[delta_x.abs, delta_y.abs].max
+ @pixels << [x.round, y.round]
+ end
+ end
+ end
+
+ class Rectangle
+ def initialize (p_1, p_2)
+ @top_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].min))
+ @top_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].min))
+ @bottom_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].max))
+ @bottom_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].max))
+ self.set_pixels
+ end
+
+ def left
+ @top_left
+ end
+
+ def right
+ @bottom_right
+ 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 pixels
+ @pixels
+ end
+
+ def ==(other)
+ @pixels.uniq.sort == other.pixels.uniq.sort
+ end
+ def eql?(other)
+ @pixels.uniq.sort == other.pixels.uniq.sort
+ end
+
+ def hash
+ hash_code = 0
+ @pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
+ hash_code.hash
+ end
+
+ protected
+ def set_pixels
+ @pixels = Line.new(@top_left, @top_right).pixels
+ @pixels = @pixels + Line.new(@top_right, @bottom_right).pixels
+ @pixels = @pixels + Line.new(@bottom_right, @bottom_left).pixels
+ @pixels = (@pixels + Line.new(@bottom_left, @top_left).pixels).uniq
+ end
+ end
+end

Венцислав обнови решението на 22.12.2013 16:35 (преди почти 11 години)

module Graphics
class Canvas
def initialize (width, height)
@width = width
@height = height
@pixels = []
end
def width
@width
end
def height
@height
end
def set_pixel (x, y)
@pixels = (@pixels + [[x, y]]).uniq
end
def pixel_at? (x, y)
@pixels.include? [x, y]
end
def draw (form)
@pixels = (@pixels + form.pixels).uniq
end
def pixels
@pixels
end
def render_as (renderer)
renderer.print(@pixels, @width, @height)
end
end
module Renderers
class Ascii
def self.print(pixels, columns, rows)
result = ""
matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
- matrix.each { |i| (pixels.include? i.reverse) ? result << "@" : result << "-" }
+ matrix.each do |i|
+ (pixels.include? i.reverse) ? result << "@" : result << "-"
+ end
rows.downto(1).each { |row| result.insert(columns*row, "\n") }
result.chop
end
end
class Html
def self.print(pixels, columns, rows)
result, 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">
'
matrix = 0.upto(rows-1).to_a.product(0.upto(columns-1).to_a)
matrix.each do |i|
(pixels.include? i.reverse) ? result << "<b></b>" : result << "<i></i>"
end
rows.downto(1).each { |row| result.insert(columns*7*row, "<br>\n") }
html << result.chomp("<br>\n") << "\n" << '</div>
</body>
</html>'
end
end
end
class Point
def initialize (x, y)
@x = x
@y = y
@pixels = [[x, y]]
end
def x
@x
end
def y
@y
end
def pixels
@pixels
end
def ==(other)
@x == other.x and @y = other.y ? true : false
end
def eql?(other)
@x == other.x and @y = other.y ? true : false
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
end
class Line
def initialize (from, to)
@from = Point.new(from.x, from.y)
@to = Point.new(to.x, to.y)
@pixels = [[@from.x, @from.y]]
self.rasterize
end
def from
if @from.x < @to.x then return @from end
if @from.x > @to.x then return @to end
@from.y < @to.y ? @from : @to
end
def to
if @from.x > @to.x then return @from end
if @from.x < @to.x then return @to end
@from.y > @to.y ? @from : @to
end
def ==(other)
@from == other.from and @to == other.to ? true : false
end
def eql?(other)
@from == other.from and @to == other.to ? true : false
end
def pixels
@pixels
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
protected
def rasterize
x, y = @from.x, @from.y
delta_x, delta_y = @to.x - x, @to.y - y
until x.round == @to.x and y.round == @to.y do
x = x + delta_x.to_f/[delta_x.abs, delta_y.abs].max
y = y + delta_y.to_f/[delta_x.abs, delta_y.abs].max
@pixels << [x.round, y.round]
end
end
end
class Rectangle
def initialize (p_1, p_2)
@top_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].min))
@top_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].min))
@bottom_left = Point.new(([p_1.x, p_2.x].min), ([p_1.y, p_2.y].max))
@bottom_right = Point.new(([p_1.x, p_2.x].max), ([p_1.y, p_2.y].max))
self.set_pixels
end
def left
@top_left
end
def right
@bottom_right
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 pixels
@pixels
end
def ==(other)
@pixels.uniq.sort == other.pixels.uniq.sort
end
def eql?(other)
@pixels.uniq.sort == other.pixels.uniq.sort
end
def hash
hash_code = 0
@pixels.uniq.sort.flatten.each { |i| hash_code = 10*hash_code + i}
hash_code.hash
end
protected
def set_pixels
@pixels = Line.new(@top_left, @top_right).pixels
@pixels = @pixels + Line.new(@top_right, @bottom_right).pixels
@pixels = @pixels + Line.new(@bottom_right, @bottom_left).pixels
@pixels = (@pixels + Line.new(@bottom_left, @top_left).pixels).uniq
end
end
end