Решение на Трета задача от Илия Ватахов

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

Към профила на Илия Ватахов

Резултати

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

Код

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width = 0, height = 0)
@width = width
@height = height
@pixels = {}
end
def set_pixel(x, y)
@pixels["[#{x},#{y}]"] = true
end
def pixel_at?(x, y)
@pixels.has_key? "[#{x},#{y}]"
end
def draw(figure)
figure.rasterization.each { |point| set_pixel *point }
end
def render_as(renderer)
renderer.new(@width, @height, @pixels).to_s
end
end
module Renderers
class Ascii
attr_reader :renderer
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
@renderer.map { |y| y.join }.join("\n")
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "@", "-" }
end
end
class Html
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
"<!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\">#{@renderer.map { |y| y.join }.join("<br>\n")}</div>
</body>
</html>"
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "<b></b>", "<i></i>" }
end
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def <=>(other)
[@x, @y] <=> [other.x, other.y]
end
def ==(other)
[@x, @y] == [other.x, other.y]
end
def eql?(other)
[@x, @y] == [other.x, other.y]
end
def rasterization
[[@x, @y]]
end
end
class Line
def initialize(a, b)
@a = a
@b = b
end
def from
(@a <=> @b) == -1 ? @a : @b
end
def to
(@a <=> @b) == 1 ? @a : @b
end
def ==(other)
[from, to] == [other.from, other.to]
end
def eql?(other)
[from, to] == [other.from, other.to]
end
def rasterization
x_1, y_1, x_2, y_2 = from.x, from.y, to.x, to.y
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)
x_1, x_2, y_1, y_2 = x_2, x_1, y_2, y_1 if x_1 > x_2
line_points x_1, y_1, x_2, y_2, steep
end
private
def line_points(x_1, y_1, x_2, y_2, steep)
error, ystep, y = (x_2 - x_1) / 2, y_1 < y_2 ? 1 : -1, y_1
x_1.upto(x_2).map do |x|
point, error = steep ? [y, x] : [x, y], error - (y_2 - y_1).abs
y += ystep and error += (x_2 - x_1) if error < 0
point
end
end
end
class Rectangle
def initialize(a, b)
@a = a
@b = b
@c = Point.new a.x, b.y
@d = Point.new b.x, a.y
end
def left
(@a <=> @b) == -1 ? @a : @b
end
def right
(@a <=> @b) == 1 ? @a : @b
end
def vertices
[@a, @b, @c, @d].sort
end
def top_left
vertices[0]
end
def top_right
vertices[2]
end
def bottom_left
vertices[1]
end
def bottom_right
vertices[3]
end
def ==(other)
vertices == other.vertices
end
def eql?(other)
vertices == other.vertices
end
def rasterization
points = [@a, @b, @c, @d].sort
lines = [[0,1], [1, 3], [2, 0], [2, 3]]
pixels = []
lines.each { |x, y| pixels.concat Line.new(points[x], points[y]).rasterization }
pixels
end
end
end

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

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

Failures:

  1) Graphics Canvas can't be constructed with no arguments
     Failure/Error: expect { Graphics::Canvas.new }.to raise_error(ArgumentError)
       expected ArgumentError but nothing was raised
     # /tmp/d20131223-4637-16u6zkr/spec.rb:13: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 shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: 601003116
            got: -17315064
       
       (compared using ==)
     # /tmp/d20131223-4637-16u6zkr/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)>'

  3) 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: 972613460
            got: 898964754
       
       (compared using ==)
     # /tmp/d20131223-4637-16u6zkr/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)>'

  4) 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: 577277380
            got: 252604948
       
       (compared using ==)
     # /tmp/d20131223-4637-16u6zkr/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)>'

  5) 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: 865891726
            got: -966295760
       
       (compared using ==)
     # /tmp/d20131223-4637-16u6zkr/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.10338 seconds
69 examples, 5 failures

Failed examples:

rspec /tmp/d20131223-4637-16u6zkr/spec.rb:12 # Graphics Canvas can't be constructed with no arguments
rspec /tmp/d20131223-4637-16u6zkr/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-16u6zkr/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-16u6zkr/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-16u6zkr/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

Илия обнови решението на 22.12.2013 17:07 (преди над 10 години)

+module Graphics
+
+ class Canvas
+ attr_reader :width, :height
+
+ def initialize(width = 0, height = 0)
+ @width = width
+ @height = height
+ @pixels = {}
+ end
+
+ def set_pixel(x, y)
+ @pixels["[#{x},#{y}]"] = true
+ end
+
+ def pixel_at?(x, y)
+ @pixels.has_key? "[#{x},#{y}]"
+ end
+
+ def draw(figure)
+ figure.rasterization.each { |point| set_pixel *point }
+ end
+
+ def render_as(renderer)
+ renderer.new(@width, @height, @pixels).to_s
+ end
+ end
+
+ module Renderers
+
+ class Ascii
+ attr_reader :renderer
+
+ def initialize(width, height, pixels)
+ @renderer = rendering width, height, pixels
+ end
+
+ def to_s
+ @renderer.map { |y| y.join }.join("\n")
+ end
+
+ private
+
+ def replace(array, pixels, first_string, second_string)
+ array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
+ end
+
+ def rendering(width, height, pixels)
+ all_pixels = 0.upto(height - 1).map do |y|
+ 0.upto(width - 1).map { |x| "[#{x},#{y}]" }
+ end
+
+ all_pixels.map { |row| replace row, pixels, "@", "-" }
+ end
+ end
+
+ class Html
+ def initialize(width, height, pixels)
+ @renderer = rendering width, height, pixels
+ end
+
+ def to_s
+ "<!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\">#{@renderer.map { |y| y.join }.join("<br>\n")}</div>
+ </body>
+ </html>"
+ end
+
+ private
+
+ def replace(array, pixels, first_string, second_string)
+ array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
+ end
+
+ def rendering(width, height, pixels)
+ all_pixels = 0.upto(height - 1).map do |y|
+ 0.upto(width - 1).map { |x| "[#{x},#{y}]" }
+ end
+
+ all_pixels.map { |row| replace row, pixels, "<b></b>", "<i></i>" }
+ end
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def <=>(other)
+ [@x, @y] <=> [other.x, other.y]
+ end
+
+ def ==(other)
+ [@x, @y] == [other.x, other.y]
+ end
+
+ def eql?(other)
+ [@x, @y] == [other.x, other.y]
+ end
+
+ def rasterization
+ [[@x, @y]]
+ end
+ end
+
+ class Line
+ def initialize(a, b)
+ @a = a
+ @b = b
+ end
+
+ def from
+ (@a <=> @b) == -1 ? @a : @b
+ end
+
+ def to
+ (@a <=> @b) == 1 ? @a : @b
+ end
+
+ def ==(other)
+ [from, to] == [other.from, other.to]
+ end
+
+ def eql?(other)
+ [from, to] == [other.from, other.to]
+ end
+
+ def line_points(x_1, y_1, x_2, y_2, steep)
+ error, ystep, y = (x_2 - x_1) / 2, y_1 < y_2 ? 1 : -1, y_1
+ x_1.upto(x_2).map do |x|
+ point, error = steep ? [y, x] : [x, y], error - (y_2 - y_1).abs
+ y += ystep and error += (x_2 - x_1) if error < 0
+ point
+ end
+ end
+
+ def rasterization
+ x_1, y_1, x_2, y_2 = from.x, from.y, to.x, to.y
+ 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)
+ x_1, x_2, y_1, y_2 = x_2, x_1, y_2, y_1 if x_1 > x_2
+ line_points x_1, y_1, x_2, y_2, steep
+ end
+ end
+
+ class Rectangle
+ def initialize(a, b)
+ @a = a
+ @b = b
+ @c = Point.new a.x, b.y
+ @d = Point.new b.x, a.y
+ end
+
+ def left
+ (@a <=> @b) == -1 ? @a : @b
+ end
+
+ def right
+ (@a <=> @b) == 1 ? @a : @b
+ end
+
+ def vertices
+ [@a, @b, @c, @d].sort
+ end
+
+ def top_left
+ vertices[0]
+ end
+
+ def top_right
+ vertices[2]
+ end
+
+ def bottom_left
+ vertices[1]
+ end
+
+ def bottom_right
+ vertices[3]
+ end
+
+ def ==(other)
+ vertices == other.vertices
+ end
+
+ def eql?(other)
+ vertices == other.vertices
+ end
+
+ def rasterization
+ points = [@a, @b, @c, @d].sort
+ lines = [[0,1], [1, 3], [2, 0], [2, 3]]
+ pixels = []
+ lines.each { |x, y| pixels.concat Line.new(points[x], points[y]).rasterization }
+ pixels
+ end
+ end
+end

Илия обнови решението на 22.12.2013 17:10 (преди над 10 години)

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width = 0, height = 0)
@width = width
@height = height
@pixels = {}
end
def set_pixel(x, y)
@pixels["[#{x},#{y}]"] = true
end
def pixel_at?(x, y)
@pixels.has_key? "[#{x},#{y}]"
end
def draw(figure)
figure.rasterization.each { |point| set_pixel *point }
end
def render_as(renderer)
renderer.new(@width, @height, @pixels).to_s
end
end
module Renderers
class Ascii
attr_reader :renderer
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
@renderer.map { |y| y.join }.join("\n")
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "@", "-" }
end
end
class Html
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
"<!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\">#{@renderer.map { |y| y.join }.join("<br>\n")}</div>
</body>
</html>"
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "<b></b>", "<i></i>" }
end
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def <=>(other)
[@x, @y] <=> [other.x, other.y]
end
def ==(other)
[@x, @y] == [other.x, other.y]
end
def eql?(other)
[@x, @y] == [other.x, other.y]
end
def rasterization
[[@x, @y]]
end
end
class Line
def initialize(a, b)
@a = a
@b = b
end
def from
(@a <=> @b) == -1 ? @a : @b
end
def to
(@a <=> @b) == 1 ? @a : @b
end
def ==(other)
[from, to] == [other.from, other.to]
end
def eql?(other)
[from, to] == [other.from, other.to]
end
def line_points(x_1, y_1, x_2, y_2, steep)
error, ystep, y = (x_2 - x_1) / 2, y_1 < y_2 ? 1 : -1, y_1
x_1.upto(x_2).map do |x|
point, error = steep ? [y, x] : [x, y], error - (y_2 - y_1).abs
- y += ystep and error += (x_2 - x_1) if error < 0
+ y += ystep and error += (x_2 - x_1) if error <= 0
point
end
end
def rasterization
x_1, y_1, x_2, y_2 = from.x, from.y, to.x, to.y
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)
x_1, x_2, y_1, y_2 = x_2, x_1, y_2, y_1 if x_1 > x_2
line_points x_1, y_1, x_2, y_2, steep
end
end
class Rectangle
def initialize(a, b)
@a = a
@b = b
@c = Point.new a.x, b.y
@d = Point.new b.x, a.y
end
def left
(@a <=> @b) == -1 ? @a : @b
end
def right
(@a <=> @b) == 1 ? @a : @b
end
def vertices
[@a, @b, @c, @d].sort
end
def top_left
vertices[0]
end
def top_right
vertices[2]
end
def bottom_left
vertices[1]
end
def bottom_right
vertices[3]
end
def ==(other)
vertices == other.vertices
end
def eql?(other)
vertices == other.vertices
end
def rasterization
points = [@a, @b, @c, @d].sort
lines = [[0,1], [1, 3], [2, 0], [2, 3]]
pixels = []
lines.each { |x, y| pixels.concat Line.new(points[x], points[y]).rasterization }
pixels
end
end
+end
+
+module Graphics
+ canvas = Canvas.new 30, 30
+
+ # Door frame and window
+ canvas.draw Rectangle.new(Point.new(3, 3), Point.new(18, 12))
+ canvas.draw Rectangle.new(Point.new(1, 1), Point.new(20, 28))
+
+ # Door knob
+ canvas.draw Line.new(Point.new(4, 15), Point.new(7, 15))
+ canvas.draw Point.new(4, 16)
+
+ # Big "R"
+ canvas.draw Line.new(Point.new(8, 5), Point.new(8, 10))
+ canvas.draw Line.new(Point.new(9, 5), Point.new(12, 5))
+ canvas.draw Line.new(Point.new(9, 7), Point.new(12, 7))
+ canvas.draw Point.new(13, 6)
+ canvas.draw Line.new(Point.new(12, 8), Point.new(13, 10))
+
+ puts canvas.render_as(Renderers::Ascii)
end

Илия обнови решението на 22.12.2013 18:11 (преди над 10 години)

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width = 0, height = 0)
@width = width
@height = height
@pixels = {}
end
def set_pixel(x, y)
@pixels["[#{x},#{y}]"] = true
end
def pixel_at?(x, y)
@pixels.has_key? "[#{x},#{y}]"
end
def draw(figure)
figure.rasterization.each { |point| set_pixel *point }
end
def render_as(renderer)
renderer.new(@width, @height, @pixels).to_s
end
end
module Renderers
class Ascii
attr_reader :renderer
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
@renderer.map { |y| y.join }.join("\n")
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "@", "-" }
end
end
class Html
def initialize(width, height, pixels)
@renderer = rendering width, height, pixels
end
def to_s
"<!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\">#{@renderer.map { |y| y.join }.join("<br>\n")}</div>
</body>
</html>"
end
private
def replace(array, pixels, first_string, second_string)
array.map { |coordinates| pixels[coordinates] ? first_string : second_string }
end
def rendering(width, height, pixels)
all_pixels = 0.upto(height - 1).map do |y|
0.upto(width - 1).map { |x| "[#{x},#{y}]" }
end
all_pixels.map { |row| replace row, pixels, "<b></b>", "<i></i>" }
end
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def <=>(other)
[@x, @y] <=> [other.x, other.y]
end
def ==(other)
[@x, @y] == [other.x, other.y]
end
def eql?(other)
[@x, @y] == [other.x, other.y]
end
def rasterization
[[@x, @y]]
end
end
class Line
def initialize(a, b)
@a = a
@b = b
end
def from
(@a <=> @b) == -1 ? @a : @b
end
def to
(@a <=> @b) == 1 ? @a : @b
end
def ==(other)
[from, to] == [other.from, other.to]
end
def eql?(other)
[from, to] == [other.from, other.to]
end
+ def rasterization
+ x_1, y_1, x_2, y_2 = from.x, from.y, to.x, to.y
+ 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)
+ x_1, x_2, y_1, y_2 = x_2, x_1, y_2, y_1 if x_1 > x_2
+ line_points x_1, y_1, x_2, y_2, steep
+ end
+
+ private
+
def line_points(x_1, y_1, x_2, y_2, steep)
error, ystep, y = (x_2 - x_1) / 2, y_1 < y_2 ? 1 : -1, y_1
x_1.upto(x_2).map do |x|
point, error = steep ? [y, x] : [x, y], error - (y_2 - y_1).abs
- y += ystep and error += (x_2 - x_1) if error <= 0
+ y += ystep and error += (x_2 - x_1) if error < 0
point
end
end
-
- def rasterization
- x_1, y_1, x_2, y_2 = from.x, from.y, to.x, to.y
- 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)
- x_1, x_2, y_1, y_2 = x_2, x_1, y_2, y_1 if x_1 > x_2
- line_points x_1, y_1, x_2, y_2, steep
- end
end
class Rectangle
def initialize(a, b)
@a = a
@b = b
@c = Point.new a.x, b.y
@d = Point.new b.x, a.y
end
def left
(@a <=> @b) == -1 ? @a : @b
end
def right
(@a <=> @b) == 1 ? @a : @b
end
def vertices
[@a, @b, @c, @d].sort
end
def top_left
vertices[0]
end
def top_right
vertices[2]
end
def bottom_left
vertices[1]
end
def bottom_right
vertices[3]
end
def ==(other)
vertices == other.vertices
end
def eql?(other)
vertices == other.vertices
end
def rasterization
points = [@a, @b, @c, @d].sort
lines = [[0,1], [1, 3], [2, 0], [2, 3]]
pixels = []
lines.each { |x, y| pixels.concat Line.new(points[x], points[y]).rasterization }
pixels
end
end
-end
-
-module Graphics
- canvas = Canvas.new 30, 30
-
- # Door frame and window
- canvas.draw Rectangle.new(Point.new(3, 3), Point.new(18, 12))
- canvas.draw Rectangle.new(Point.new(1, 1), Point.new(20, 28))
-
- # Door knob
- canvas.draw Line.new(Point.new(4, 15), Point.new(7, 15))
- canvas.draw Point.new(4, 16)
-
- # Big "R"
- canvas.draw Line.new(Point.new(8, 5), Point.new(8, 10))
- canvas.draw Line.new(Point.new(9, 5), Point.new(12, 5))
- canvas.draw Line.new(Point.new(9, 7), Point.new(12, 7))
- canvas.draw Point.new(13, 6)
- canvas.draw Line.new(Point.new(12, 8), Point.new(13, 10))
-
- puts canvas.render_as(Renderers::Ascii)
end