Решение на Шеста задача от Добромир Иванов

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

Към профила на Добромир Иванов

Резултати

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

Код

module TurtleGraphics
class Position < Struct.new(:x, :y)
end
class Orientation < Struct.new(:x, :y)
end
class Turtle
ORIENTATION = { up: Orientation.new(0, -1), down: Orientation.new(0, 1),
right: Orientation.new(1, 0), left: Orientation.new(-1, 0)
}
def initialize(x, y)
@spawned, @plain = false, Canvas::Plain.new(x, y)
@orientation, @position = ORIENTATION[:right], Position.new(0, 0)
end
def move
spawn_at(@position.x, @position.y) if not @spawned
x, y = @position.x + @orientation.x, @position.y + @orientation.y
new_position = reposition(Position.new(x, y))
spawn_at(new_position.x, new_position.y)
end
def turn_left
@orientation = Orientation.new(@orientation.y, -1 * @orientation.x)
end
def turn_right
@orientation = Orientation.new(-1 * @orientation.y, @orientation.x)
end
def spawn_at(x, y)
@spawned = true
@position = Position.new(x, y)
@plain.visit(@position)
end
def look(orientation)
@orientation = ORIENTATION[orientation]
end
def draw(canvas = nil, &block)
instance_eval(&block) if block_given?
if canvas != nil
canvas.draw @plain
else
@plain.plain
end
end
private
def reposition(current_position)
if @plain.outside_column?(current_position)
Position.new(0, current_position.y)
elsif @plain.outside_row?(current_position)
Position.new(current_position.x, 0)
else
current_position
end
end
end
module Canvas
class Plain
def initialize(x, y)
@rows, @columns = y, x
@canvas = []
y.times do |row|
@canvas[row] = []
x.times { |column| @canvas[row][column] = 0 }
end
end
def visit(position)
@canvas[position.y][position.x] += 1
end
def plain
@canvas.map(&:clone).clone
end
def outside_column?(position)
position.x >= @columns
end
def outside_row?(position)
position.y >= @rows
end
end
class ASCII
def initialize(symbols)
@delta = (1.to_f / (symbols.size - 1).to_f)
@symbols = {}
symbols.each_with_index do |symbol, index|
@symbols[Range.new((index - 1) * @delta, index * @delta)] = symbol
end
end
def draw(plain)
drawing = plain.plain.each.map do |row|
row.map do |value|
representation(intensity(value, plain.plain))
end
end
drawing.map(&:join).map {|str| str + "\n"}.join.strip
end
private
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
pixel.to_f / (max_visits plain).to_f
end
def representation(intensity)
@symbols.each do |range, value|
return value if range.include? intensity
end
end
end
class HTML
TEMPLATE = %{
<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>
<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
width: %dpx;
height: %dpx;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
<table>
%s
</table>
</body>
</html>
}
def initialize(cell_size)
@cell_size = cell_size
end
def draw(plain)
table = representation(plain).map do |row|
"<tr>#{row.join}</tr>"
end
TEMPLATE % [@cell_size, @cell_size, table.join]
end
private
def representation(plain)
template = "<td style='opacity: %.2f'></td>"
plain.plain.each.map do |row|
row.map do |pixel|
template % [intensity(pixel, plain.plain)]
end
end
end
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
pixel.to_f / (max_visits plain).to_f
end
end
end
end

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

.......F...F.F

Failures:

  1) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: expect(canvas).to eq [[0, 0], [1, 0]]
       
       expected: [[0, 0], [1, 0]]
            got: [[0, 1], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1r5wpfi/spec.rb:93: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) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-HTML.gsub(/\s+/, '')
       
       expected: "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:1.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table></body></html>"
            got: "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle='opacity:1.00'></td><tdstyle='opacity:1.00'></td><tdstyle='opacity:1.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr></table></body></html>"
       
       (compared using ==)
     # /tmp/d20151203-5272-1r5wpfi/spec.rb:171: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) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
       expected "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle='opacity:1.00'></td><tdstyle='opacity:0.67'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.33'></td><tdstyle='opacity:0.33'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr></table></body></html>" to include "<table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:0.67\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table>"
     # /tmp/d20151203-5272-1r5wpfi/spec.rb:242: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)>'

Finished in 0.01403 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20151203-5272-1r5wpfi/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-1r5wpfi/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1r5wpfi/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Добромир обнови решението на 29.11.2015 23:22 (преди над 8 години)

+module TurtleGraphics
+ class Position < Struct.new(:x, :y)
+ end
+
+ class Orientation < Struct.new(:x, :y)
+ def self.up
+ Orientation.new(0, -1)
+ end
+
+ def self.down
+ Orientation.new(0, 1)
+ end
+
+ def self.right
+ Orientation.new(1, 0)
+ end
+
+ def self.left
+ Orientation.new(-1, 0)
+ end
+ end
+
+ class Turtle
+ def initialize(x, y)
+ @spawned, @plain = false, Canvas::Plain.new(x, y)
+ @orientation, @position = Orientation.right, Position.new(0, 0)
+ end
+
+ def move
+ spawn_at(@position.x, @position.y) if not @spawned
+ x, y = @position.x + @orientation.x, @position.y + @orientation.y
+ new_position = reposition(Position.new(x, y))
+ spawn_at(new_position.x, new_position.y)
+ end
+
+ def turn_left
+ @orientation = Orientation.new(@orientation.y, -1 * @orientation.x)
+ end
+
+ def turn_right
+ @orientation = Orientation.new(-1 * @orientation.y, @orientation.x)
+ end
+
+ def spawn_at(x, y)
+ @spawned = true
+ @position = Position.new(x, y)
+ @plain.visit(@position)
+ end
+
+ def look(orientation)
+ @orientation = Orientation.send(orientation)
+ end
+
+ def draw(canvas = nil, &block)
+ instance_eval(&block) if block_given?
+ if canvas != nil
+ canvas.draw @plain
+ else
+ @plain.plain
+ end
+ end
+
+ private
+
+ def reposition(current_position)
+ if @plain.outside_column?(current_position)
+ Position.new(0, current_position.y)
+ elsif @plain.outside_row?(current_position)
+ Position.new(current_position.x, 0)
+ else
+ current_position
+ end
+ end
+ end
+
+ module Canvas
+ class Plain
+ def initialize(x, y)
+ @rows, @columns = y, x
+ @canvas = []
+ y.times do |row|
+ @canvas[row] = []
+ x.times { |column| @canvas[row][column] = 0 }
+ end
+ end
+
+ def visit(position)
+ @canvas[position.y][position.x] += 1
+ end
+
+ def plain
+ @canvas.map(&:clone).clone
+ end
+
+ def outside_column?(position)
+ position.x >= @columns
+ end
+
+ def outside_row?(position)
+ position.y >= @rows
+ end
+ end
+
+ class ASCII
+ def initialize(symbols)
+ @delta = (Float(1) / Float(symbols.size - 1))
+ @symbols = {}
+ symbols.each_with_index do |symbol, index|
+ @symbols[Range.new((index - 1) * @delta, index * @delta)] = symbol
+ end
+ end
+
+ def draw(plain)
+ drawing = plain.plain.each.map do |row|
+ row.map do |value|
+ representation(intensity(value, plain.plain))
+ end
+ end
+
+ drawing.map(&:join).map {|str| str + "\n"}.join.strip
+ end
+
+ private
+
+ def max_visits(plain)
+ plain.map(&:max).max
+ end
+
+ def intensity(pixel, plain)
+ Float(pixel) / Float(max_visits plain)
+ end
+
+ def representation(intensity)
+ @symbols.each do |range, value|
+ return value if range.include? intensity
+ end
+ end
+ end
+
+ class HTML
+ TEMPLATE = %{
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Turtle graphics</title>
+
+ <style>
+ table {
+ border-spacing: 0;
+ }
+
+ tr {
+ padding: 0;
+ }
+
+ td {
+ width: %spx;
+ height: %spx;
+
+ background-color: black;
+ padding: 0;
+ }
+ </style>
+ </head>
+ <body>
+ <table>
+ %s
+ </table>
+ </body>
+ </html>
+ }
+
+ def initialize(cell_size)
+ @cell_size = cell_size
+ end
+
+ def draw(plain)
+ table = representation(plain).map do |row|
+ "<tr>#{row.join}</tr>"
+ end
+ TEMPLATE % [@cell_size, @cell_size, table.join]
+ end
+
+ private
+
+ def representation(plain)
+ template = "<td style='opacity: %s'></td>"
+ plain.plain.each.map do |row|
+ row.map do |pixel|
+ template % [format("%.2f", intensity(pixel, plain.plain))]
+ end
+ end
+ end
+
+ def max_visits(plain)
+ plain.map(&:max).max
+ end
+
+ def intensity(pixel, plain)
+ Float(pixel) / Float(max_visits plain)
+ end
+ end
+ end
+end

Здравей :)

Ето няколко коментара:

  • Float не се прави така. Има един метод на всички видове числа, който ги превръща във Float
  • Правиш едно излишно форматиране в representation. Първо форматираш число като %.2f, после резултата го форматираш като низ. Защо не сложиш директно %.2f в template?
  • Не ми харесва употребата на send в look. Мога да ти подам някакъв друг символ и то ще го изпълни като метод. Като цяло мисля, че само си усложнил нещата с класа Orientation. Реално не прави нищо, което не може да го направи една константа с подходящо съдържание :)

Добромир обнови решението на 02.12.2015 11:18 (преди над 8 години)

module TurtleGraphics
class Position < Struct.new(:x, :y)
end
class Orientation < Struct.new(:x, :y)
- def self.up
- Orientation.new(0, -1)
- end
-
- def self.down
- Orientation.new(0, 1)
- end
-
- def self.right
- Orientation.new(1, 0)
- end
-
- def self.left
- Orientation.new(-1, 0)
- end
end
class Turtle
+ ORIENTATION = { up: Orientation.new(0, -1), down: Orientation.new(0, 1),
+ right: Orientation.new(1, 0), left: Orientation.new(-1, 0)
+ }
+
def initialize(x, y)
@spawned, @plain = false, Canvas::Plain.new(x, y)
- @orientation, @position = Orientation.right, Position.new(0, 0)
+ @orientation, @position = ORIENTATION[:right], Position.new(0, 0)
end
def move
spawn_at(@position.x, @position.y) if not @spawned
x, y = @position.x + @orientation.x, @position.y + @orientation.y
new_position = reposition(Position.new(x, y))
spawn_at(new_position.x, new_position.y)
end
def turn_left
@orientation = Orientation.new(@orientation.y, -1 * @orientation.x)
end
def turn_right
@orientation = Orientation.new(-1 * @orientation.y, @orientation.x)
end
def spawn_at(x, y)
@spawned = true
@position = Position.new(x, y)
@plain.visit(@position)
end
def look(orientation)
- @orientation = Orientation.send(orientation)
+ @orientation = ORIENTATION[orientation]
end
def draw(canvas = nil, &block)
instance_eval(&block) if block_given?
if canvas != nil
canvas.draw @plain
else
@plain.plain
end
end
private
def reposition(current_position)
if @plain.outside_column?(current_position)
Position.new(0, current_position.y)
elsif @plain.outside_row?(current_position)
Position.new(current_position.x, 0)
else
current_position
end
end
end
module Canvas
class Plain
def initialize(x, y)
@rows, @columns = y, x
@canvas = []
y.times do |row|
@canvas[row] = []
x.times { |column| @canvas[row][column] = 0 }
end
end
def visit(position)
@canvas[position.y][position.x] += 1
end
def plain
@canvas.map(&:clone).clone
end
def outside_column?(position)
position.x >= @columns
end
def outside_row?(position)
position.y >= @rows
end
end
class ASCII
def initialize(symbols)
- @delta = (Float(1) / Float(symbols.size - 1))
+ @delta = (1.to_f / (symbols.size - 1).to_f)
@symbols = {}
symbols.each_with_index do |symbol, index|
@symbols[Range.new((index - 1) * @delta, index * @delta)] = symbol
end
end
def draw(plain)
drawing = plain.plain.each.map do |row|
row.map do |value|
representation(intensity(value, plain.plain))
end
end
drawing.map(&:join).map {|str| str + "\n"}.join.strip
end
private
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
- Float(pixel) / Float(max_visits plain)
+ pixel.to_f / (max_visits plain).to_f
end
def representation(intensity)
@symbols.each do |range, value|
return value if range.include? intensity
end
end
end
class HTML
TEMPLATE = %{
<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>
<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
- width: %spx;
- height: %spx;
+ width: %.2fpx;
+ height: %.2fpx;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
<table>
%s
</table>
</body>
</html>
}
def initialize(cell_size)
@cell_size = cell_size
end
def draw(plain)
table = representation(plain).map do |row|
"<tr>#{row.join}</tr>"
end
TEMPLATE % [@cell_size, @cell_size, table.join]
end
private
def representation(plain)
template = "<td style='opacity: %s'></td>"
plain.plain.each.map do |row|
row.map do |pixel|
- template % [format("%.2f", intensity(pixel, plain.plain))]
+ template % [intensity(pixel, plain.plain)]
end
end
end
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
- Float(pixel) / Float(max_visits plain)
+ pixel.to_f / (max_visits plain).to_f
end
end
end
end

Добромир обнови решението на 02.12.2015 11:32 (преди над 8 години)

module TurtleGraphics
class Position < Struct.new(:x, :y)
end
class Orientation < Struct.new(:x, :y)
end
class Turtle
ORIENTATION = { up: Orientation.new(0, -1), down: Orientation.new(0, 1),
right: Orientation.new(1, 0), left: Orientation.new(-1, 0)
}
def initialize(x, y)
@spawned, @plain = false, Canvas::Plain.new(x, y)
@orientation, @position = ORIENTATION[:right], Position.new(0, 0)
end
def move
spawn_at(@position.x, @position.y) if not @spawned
x, y = @position.x + @orientation.x, @position.y + @orientation.y
new_position = reposition(Position.new(x, y))
spawn_at(new_position.x, new_position.y)
end
def turn_left
@orientation = Orientation.new(@orientation.y, -1 * @orientation.x)
end
def turn_right
@orientation = Orientation.new(-1 * @orientation.y, @orientation.x)
end
def spawn_at(x, y)
@spawned = true
@position = Position.new(x, y)
@plain.visit(@position)
end
def look(orientation)
@orientation = ORIENTATION[orientation]
end
def draw(canvas = nil, &block)
instance_eval(&block) if block_given?
if canvas != nil
canvas.draw @plain
else
@plain.plain
end
end
private
def reposition(current_position)
if @plain.outside_column?(current_position)
Position.new(0, current_position.y)
elsif @plain.outside_row?(current_position)
Position.new(current_position.x, 0)
else
current_position
end
end
end
module Canvas
class Plain
def initialize(x, y)
@rows, @columns = y, x
@canvas = []
y.times do |row|
@canvas[row] = []
x.times { |column| @canvas[row][column] = 0 }
end
end
def visit(position)
@canvas[position.y][position.x] += 1
end
def plain
@canvas.map(&:clone).clone
end
def outside_column?(position)
position.x >= @columns
end
def outside_row?(position)
position.y >= @rows
end
end
class ASCII
def initialize(symbols)
@delta = (1.to_f / (symbols.size - 1).to_f)
@symbols = {}
symbols.each_with_index do |symbol, index|
@symbols[Range.new((index - 1) * @delta, index * @delta)] = symbol
end
end
def draw(plain)
drawing = plain.plain.each.map do |row|
row.map do |value|
representation(intensity(value, plain.plain))
end
end
drawing.map(&:join).map {|str| str + "\n"}.join.strip
end
private
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
pixel.to_f / (max_visits plain).to_f
end
def representation(intensity)
@symbols.each do |range, value|
return value if range.include? intensity
end
end
end
class HTML
TEMPLATE = %{
<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>
<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
- width: %.2fpx;
- height: %.2fpx;
+ width: %dpx;
+ height: %dpx;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
<table>
%s
</table>
</body>
</html>
}
def initialize(cell_size)
@cell_size = cell_size
end
def draw(plain)
table = representation(plain).map do |row|
"<tr>#{row.join}</tr>"
end
TEMPLATE % [@cell_size, @cell_size, table.join]
end
private
def representation(plain)
- template = "<td style='opacity: %s'></td>"
+ template = "<td style='opacity: %.2f'></td>"
plain.plain.each.map do |row|
row.map do |pixel|
template % [intensity(pixel, plain.plain)]
end
end
end
def max_visits(plain)
plain.map(&:max).max
end
def intensity(pixel, plain)
pixel.to_f / (max_visits plain).to_f
end
end
end
end