Решение на Шеста задача от Георги Киряков

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

Към профила на Георги Киряков

Резултати

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

Код

module TurtleGraphics
class Turtle
DIRECTION = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
ORIENTATION = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows, @columns = rows, columns
@plane = Array.new(@rows) { Array.new(@columns, 0) }
@position = nil
@orientation = :right
end
def draw(canvas = nil, &block)
self.instance_eval(&block) if block_given?
if canvas.respond_to? :draw
canvas.draw(@plane)
else
@plane
end
end
def move
spawn_at(0, 0) if @position.nil?
direction = DIRECTION[@orientation]
@position = [(@position[0] + direction[0]) % @rows,
(@position[1] + direction[1]) % @columns]
@plane[@position[0]][@position[1]] += 1
end
def turn_right
direction = (ORIENTATION.index(@orientation).next) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def turn_left
direction = (ORIENTATION.index(@orientation).pred) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def spawn_at(row, column)
@position = [row, column]
@plane[row][column] += 1
end
def look(orientation)
@orientation = orientation
end
end # class Turtle
class Canvas
def self.find_intensity(canvas)
intensity = canvas.max
(intensity == 0) ? 1 : intensity
end
class ASCII
def initialize(symbols)
@symblos = symbols
end
def draw(plane)
columns = plane[0].size
canvas = plane.flatten
max_intensity = Canvas.find_intensity(canvas)
canvas = canvas.map do |pixel|
intensity = pixel.fdiv(max_intensity) * (@symblos.size - 1)
@symblos[intensity.ceil]
end
canvas.each_slice(columns).reduce do |row_a, row_b|
"#{row_a.join("")}\n#{row_b.join("")}"
end
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def head
"<!DOCTYPE html><html><head><title>Turtle graphics</title><style>"\
"table {border-spacing: 0;} tr{padding: 0;} "\
"td { width: #{@pixel_size}px; height: #{@pixel_size}px; "\
"background-color: black; padding: 0;} </style></head><body>"
end
def footer
"</body></html>"
end
def draw(plane)
max_intensity = Canvas.find_intensity(plane.flatten)
canvas = head
canvas += "<table>"
plane.each do |row|
canvas += "<tr>"
row.each do |pixel|
intensity = format '%.2f', pixel.fdiv(max_intensity)
canvas += "<td style=\"opacity: #{intensity}\"></td>"
end
canvas += "</tr>"
end
canvas += "</table>" + footer
end
end
end # module Canvas
end

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

.........FF...

Failures:

  1) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: ascii = TurtleGraphics::Turtle.new(3, 3).draw(ascii_canvas) do
     NoMethodError:
       undefined method `join' for "320\n110":String
     # /tmp/d20151203-5272-1nqolz0/solution.rb:75:in `block in draw'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each_slice'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `reduce'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `draw'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:16:in `draw'
     # /tmp/d20151203-5272-1nqolz0/spec.rb:114: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) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii = TurtleGraphics::Turtle.new(3, 3).draw(ascii_canvas) do
     NoMethodError:
       undefined method `join' for "ttz\nooz":String
     # /tmp/d20151203-5272-1nqolz0/solution.rb:75:in `block in draw'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each_slice'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `each'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `reduce'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:74:in `draw'
     # /tmp/d20151203-5272-1nqolz0/solution.rb:16:in `draw'
     # /tmp/d20151203-5272-1nqolz0/spec.rb:137: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.01192 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20151203-5272-1nqolz0/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-1nqolz0/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols

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

Георги обнови решението на 01.12.2015 14:27 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ DIRECTION = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
+ ORIENTATION = [:left, :up, :right, :down]
+
+ def initialize(rows, columns)
+ @rows, @columns = rows, columns
+ @plane = Array.new(@rows) { Array.new(@columns, 0) }
+ @position = nil
+ @orientation = :right
+ end
+
+ def draw(canvas = nil, &block)
+ self.instance_eval(&block)
+ if canvas.respond_to? :draw
+ canvas.draw(@plane)
+ else
+ @plane
+ end
+ end
+
+ def move
+ spawn_at(0, 0) if @position.nil?
+
+ direction = DIRECTION[@orientation]
+ @position = [(@position[0] + direction[0]) % @rows,
+ (@position[1] + direction[1]) % @columns]
+
+ #@position[0] = @rows - 1 if @position[0] < 0
+ #@position[1] = @columns - 1 if @position[1] < 0
+ @plane[@position[0]][@position[1]] += 1
+ end
+
+ def turn_right
+ direction = (ORIENTATION.index(@orientation).next) % ORIENTATION.size
+ @orientation = ORIENTATION[direction]
+ end
+
+ def turn_left
+ direction = (ORIENTATION.index(@orientation).pred) % ORIENTATION.size
+ @orientation = ORIENTATION[direction]
+ end
+
+ def spawn_at(row, column)
+ @position = [row, column]
+ @plane[column][row] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end # class Turtle
+
+ module Canvas
+ class ASCII
+ def initialize(symbols)
+ @symblos = symbols
+ end
+
+ def draw(plane)
+ columns = plane[0].size
+ canvas = plane.flatten
+ max_intensity = canvas.max
+ canvas = canvas.map do |pixel|
+ intensity = pixel.fdiv(max_intensity) * (@symblos.size - 1)
+ @symblos[intensity.ceil]
+ end
+ canvas.each_slice(columns).inject([], :push)
+ end
+ end
+
+ class HTML
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+ end
+
+ def head
+ "<!DOCTYPE html><html><head><title>Turtle graphics</title><style>"\
+ "table {border-spacing: 0;} tr{padding: 0;} "\
+ "td { width: #{@pixel_size}px; height: #{@pixel_size}px; "\
+ "background-color: black; padding: 0;} </style></head><body>"
+ end
+
+ def footer
+ "</body></html>"
+ end
+
+ def draw(plane)
+ max_intensity = plane.flatten.max
+ canvas = head
+ canvas += "<table>"
+ plane.each do |row|
+ canvas += "<tr>"
+ row.each do |pixel|
+ intensity = format '%.2f', pixel.fdiv(max_intensity)
+ canvas += "<td style=\"opacity: #{intensity}\"></td>"
+ end
+ canvas += "</tr>"
+ end
+ canvas += "</table>" + footer
+ end
+ end
+ end # module Canvas
+end

Георги обнови решението на 01.12.2015 21:16 (преди над 8 години)

module TurtleGraphics
class Turtle
DIRECTION = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
ORIENTATION = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows, @columns = rows, columns
@plane = Array.new(@rows) { Array.new(@columns, 0) }
@position = nil
@orientation = :right
end
def draw(canvas = nil, &block)
- self.instance_eval(&block)
+ self.instance_eval(&block) if block_given?
if canvas.respond_to? :draw
canvas.draw(@plane)
else
@plane
end
end
def move
spawn_at(0, 0) if @position.nil?
direction = DIRECTION[@orientation]
@position = [(@position[0] + direction[0]) % @rows,
(@position[1] + direction[1]) % @columns]
#@position[0] = @rows - 1 if @position[0] < 0
#@position[1] = @columns - 1 if @position[1] < 0
@plane[@position[0]][@position[1]] += 1
end
def turn_right
direction = (ORIENTATION.index(@orientation).next) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def turn_left
direction = (ORIENTATION.index(@orientation).pred) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def spawn_at(row, column)
@position = [row, column]
- @plane[column][row] += 1
+ @plane[row][column] += 1
end
def look(orientation)
@orientation = orientation
end
end # class Turtle
module Canvas
+
+ def find_intensity(canvas)
+ intensity = canvas.max
+ intensity = 1 if intensity == 0
+ end
+
class ASCII
def initialize(symbols)
@symblos = symbols
end
def draw(plane)
columns = plane[0].size
canvas = plane.flatten
- max_intensity = canvas.max
+
+ max_intensity = find_intensity(canvas)
+
canvas = canvas.map do |pixel|
intensity = pixel.fdiv(max_intensity) * (@symblos.size - 1)
@symblos[intensity.ceil]
end
- canvas.each_slice(columns).inject([], :push)
+
+ drawing = canvas.each_slice(columns).inject("") do |drawing, row|
+ drawing + "\n" + row.join("")
+ end
+ drawing[0] = ''
+
+ drawing
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def head
"<!DOCTYPE html><html><head><title>Turtle graphics</title><style>"\
"table {border-spacing: 0;} tr{padding: 0;} "\
"td { width: #{@pixel_size}px; height: #{@pixel_size}px; "\
"background-color: black; padding: 0;} </style></head><body>"
end
def footer
"</body></html>"
end
def draw(plane)
- max_intensity = plane.flatten.max
+ max_intensity = find_intensity(plane.flatten)
canvas = head
canvas += "<table>"
plane.each do |row|
canvas += "<tr>"
row.each do |pixel|
intensity = format '%.2f', pixel.fdiv(max_intensity)
canvas += "<td style=\"opacity: #{intensity}\"></td>"
end
canvas += "</tr>"
end
canvas += "</table>" + footer
end
end
end # module Canvas
end

Георги обнови решението на 01.12.2015 21:25 (преди над 8 години)

module TurtleGraphics
class Turtle
DIRECTION = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
ORIENTATION = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows, @columns = rows, columns
@plane = Array.new(@rows) { Array.new(@columns, 0) }
@position = nil
@orientation = :right
end
def draw(canvas = nil, &block)
self.instance_eval(&block) if block_given?
if canvas.respond_to? :draw
canvas.draw(@plane)
else
@plane
end
end
def move
spawn_at(0, 0) if @position.nil?
direction = DIRECTION[@orientation]
@position = [(@position[0] + direction[0]) % @rows,
(@position[1] + direction[1]) % @columns]
#@position[0] = @rows - 1 if @position[0] < 0
#@position[1] = @columns - 1 if @position[1] < 0
@plane[@position[0]][@position[1]] += 1
end
def turn_right
direction = (ORIENTATION.index(@orientation).next) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def turn_left
direction = (ORIENTATION.index(@orientation).pred) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def spawn_at(row, column)
@position = [row, column]
@plane[row][column] += 1
end
def look(orientation)
@orientation = orientation
end
end # class Turtle
- module Canvas
-
- def find_intensity(canvas)
+ class Canvas
+ def self.find_intensity(canvas)
intensity = canvas.max
- intensity = 1 if intensity == 0
+ (intensity == 0) ? 1 : intensity
end
class ASCII
def initialize(symbols)
@symblos = symbols
end
def draw(plane)
columns = plane[0].size
canvas = plane.flatten
- max_intensity = find_intensity(canvas)
+ max_intensity = Canvas.find_intensity(canvas)
canvas = canvas.map do |pixel|
intensity = pixel.fdiv(max_intensity) * (@symblos.size - 1)
@symblos[intensity.ceil]
end
drawing = canvas.each_slice(columns).inject("") do |drawing, row|
drawing + "\n" + row.join("")
end
drawing[0] = ''
drawing
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def head
"<!DOCTYPE html><html><head><title>Turtle graphics</title><style>"\
"table {border-spacing: 0;} tr{padding: 0;} "\
"td { width: #{@pixel_size}px; height: #{@pixel_size}px; "\
"background-color: black; padding: 0;} </style></head><body>"
end
def footer
"</body></html>"
end
def draw(plane)
- max_intensity = find_intensity(plane.flatten)
+ max_intensity = Canvas.find_intensity(plane.flatten)
canvas = head
canvas += "<table>"
plane.each do |row|
canvas += "<tr>"
row.each do |pixel|
intensity = format '%.2f', pixel.fdiv(max_intensity)
canvas += "<td style=\"opacity: #{intensity}\"></td>"
end
canvas += "</tr>"
end
canvas += "</table>" + footer
end
end
end # module Canvas
end

Георги обнови решението на 02.12.2015 16:21 (преди над 8 години)

module TurtleGraphics
class Turtle
DIRECTION = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
ORIENTATION = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows, @columns = rows, columns
@plane = Array.new(@rows) { Array.new(@columns, 0) }
@position = nil
@orientation = :right
end
def draw(canvas = nil, &block)
self.instance_eval(&block) if block_given?
if canvas.respond_to? :draw
canvas.draw(@plane)
else
@plane
end
end
def move
spawn_at(0, 0) if @position.nil?
direction = DIRECTION[@orientation]
@position = [(@position[0] + direction[0]) % @rows,
(@position[1] + direction[1]) % @columns]
- #@position[0] = @rows - 1 if @position[0] < 0
- #@position[1] = @columns - 1 if @position[1] < 0
@plane[@position[0]][@position[1]] += 1
end
def turn_right
direction = (ORIENTATION.index(@orientation).next) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def turn_left
direction = (ORIENTATION.index(@orientation).pred) % ORIENTATION.size
@orientation = ORIENTATION[direction]
end
def spawn_at(row, column)
@position = [row, column]
@plane[row][column] += 1
end
def look(orientation)
@orientation = orientation
end
end # class Turtle
class Canvas
def self.find_intensity(canvas)
intensity = canvas.max
(intensity == 0) ? 1 : intensity
end
class ASCII
def initialize(symbols)
@symblos = symbols
end
def draw(plane)
columns = plane[0].size
canvas = plane.flatten
max_intensity = Canvas.find_intensity(canvas)
canvas = canvas.map do |pixel|
intensity = pixel.fdiv(max_intensity) * (@symblos.size - 1)
@symblos[intensity.ceil]
end
- drawing = canvas.each_slice(columns).inject("") do |drawing, row|
- drawing + "\n" + row.join("")
+ canvas.each_slice(columns).reduce do |row_a, row_b|
+ "#{row_a.join("")}\n#{row_b.join("")}"
end
- drawing[0] = ''
-
- drawing
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def head
"<!DOCTYPE html><html><head><title>Turtle graphics</title><style>"\
"table {border-spacing: 0;} tr{padding: 0;} "\
"td { width: #{@pixel_size}px; height: #{@pixel_size}px; "\
"background-color: black; padding: 0;} </style></head><body>"
end
def footer
"</body></html>"
end
def draw(plane)
max_intensity = Canvas.find_intensity(plane.flatten)
canvas = head
canvas += "<table>"
plane.each do |row|
canvas += "<tr>"
row.each do |pixel|
intensity = format '%.2f', pixel.fdiv(max_intensity)
canvas += "<td style=\"opacity: #{intensity}\"></td>"
end
canvas += "</tr>"
end
canvas += "</table>" + footer
end
end
end # module Canvas
end