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

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

Към профила на Методи Димитров

Резултати

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

Код

module TurtleGraphics
module Canvas
end
end
class TurtleGraphics::Turtle
attr_reader :drawboard
def initialize(rows, columns)
@row, @column = 0, 0
@direction = [0, 1]
@drawboard = Array.new(rows) { Array.new(columns) { 0 } }
@drawboard[@row][@column] = 1
end
def draw(style = self, &block)
instance_eval &block
style.generate(@drawboard)
end
def generate(drawboard)
drawboard
end
def move()
calculate_coordinates @row + @direction[0], @column + @direction[1]
@drawboard[@row][@column] += 1
end
def turn_left()
change_orientation(0, 1)
end
def turn_right()
change_orientation(1, 0)
end
def spawn_at(row, column)
@drawboard[0][0] -= 1
calculate_coordinates(row, column)
@drawboard[@row][@column] += 1
end
def look(direction)
@direction = case direction
when :left [ 0, -1]
when :right [ 0, 1]
when :up [-1, 0]
when :down [ 1, 0]
end
end
private
def change_orientation(vertical, horizontal)
if(@direction[vertical] != 0)
@direction = @direction.reverse
else
@direction[vertical] = @direction[vertical] - @direction[horizontal]
@direction[horizontal] = 0
end
end
def calculate_coordinates(row, column)
@row = row % @drawboard.size
@column = column % @drawboard[0].size
end
end
class TurtleGraphics::Canvas::ASCII
attr_reader :symbols
def initialize(symbols)
@symbols = symbols
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
coefficient = maximum_intensity.to_f / (@symbols.length - 1)
matrix.map do |raw|
raw.map do |pixel|
@symbols[pixel / coefficient]
end
end
end
end
class TurtleGraphics::Canvas::HTML
attr_accessor :content
def initialize(pixel_size)
@content = "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics" +
"</title>\n <style>\n table {\n border-spacing: 0;\n }\n" +
" tr {\n padding: 0;\n }\n td {\n width: " +
"#{pixel_size}px;\n height: #{pixel_size}px;\n " +
"background-color: black;\n padding: 0;\n }\n " +
"</style>\n</head>\n<body>\n <table>\n"
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
@content + matrix.map do |raw|
" <tr>\n" + raw.map do |pixel|
" <td style=\"opacity: " +
"#{format('%.2f', pixel.to_f / maximum_intensity)}\"></td>\n"
end.reduce(:+) + " </tr>\n"
end.reduce(:+) + " </table>\n</body>\n</html>"
end
end

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

........FFF...

Failures:

  1) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: move
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151203-5272-8tlfj8/solution.rb:26:in `move'
     # /tmp/d20151203-5272-8tlfj8/spec.rb:102:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-8tlfj8/solution.rb:17:in `instance_eval'
     # /tmp/d20151203-5272-8tlfj8/solution.rb:17:in `draw'
     # /tmp/d20151203-5272-8tlfj8/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-8tlfj8/spec.rb:99: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::ASCII renders the proper symbols depending on the intensity
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [["3", "2", "0"], ["1", "1", "0"], ["0", "0", "0"]]:Array
     # /tmp/d20151203-5272-8tlfj8/spec.rb:128: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::ASCII can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [["t", "o", "z"], ["z", "z", "z"], ["z", "z", "z"]]:Array
     # /tmp/d20151203-5272-8tlfj8/spec.rb:151: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.01209 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20151203-5272-8tlfj8/spec.rb:98 # TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
rspec /tmp/d20151203-5272-8tlfj8/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-8tlfj8/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols

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

Методи обнови решението на 30.11.2015 23:14 (преди над 8 години)

+module TurtleGraphics
+ module Canvas
+ end
+end
+
+class TurtleGraphics::Turtle
+ attr_reader :drawboard
+
+ def initialize(rows, columns, &block)
+ @row, @column = 0, 0
+ @direction = [0,1]
+ @drawboard = Array.new(rows) { Array.new(columns) { 0 } }
+ @drawboard[@row][@column] = 1
+ end
+
+ def draw(style = self, &block)
+ instance_eval &block
+ style.generate(@drawboard)
+ end
+
+ def generate(drawboard)
+ drawboard
+ end
+
+ def move()
+ @row = (@row + @direction[0]) % @drawboard.size
+ @column = (@column + @direction[1]) % @drawboard[0].size
+ @drawboard[@row][@column] += 1
+ end
+
+ def turn_left()
+ change_orientation(0, 1)
+ end
+
+ def turn_right()
+ change_orientation(1,0)
+ end
+
+ def spawn_at(row, column)
+ @row, @column = row, column
+ end
+
+ def look(direction)
+ @direction = case direction
+ when :left [ 0, -1]
+ when :right [ 0, 1]
+ when :up [-1, 0]
+ when :down [ 1, 0]
+ end
+ end
+
+ private
+
+ def change_orientation(vertical, horizontal)
+ if(@direction[vertical] != 0)
+ @direction = @direction.reverse
+ else
+ @direction[vertical] = @direction[vertical] - @direction[horizontal]
+ @direction[horizontal] = 0
+ end
+ end
+end
+
+class TurtleGraphics::Canvas::ASCII
+ attr_reader :symbols
+
+ def initialize(symbols)
+ @symbols = symbols
+ end
+
+ def generate(matrix)
+ maximum_intensity = matrix.map(&:max).max
+ coefficient = maximum_intensity.to_f / (@symbols.length - 1)
+ matrix.map do |raw|
+ raw.map do |pixel|
+ @symbols[pixel / coefficient]
+ end
+ end
+ end
+end
+
+class TurtleGraphics::Canvas::HTML
+ attr_accessor :content
+ def initialize(pixel_size)
+ @content = "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics" +
+ "</title>\n <style>\n table {\n border-spacing: 0;\n }\n" +
+ " tr {\n padding: 0;\n }\n td {\n width: " +
+ "#{pixel_size}px;\n height: #{pixel_size}px;\n " +
+ "background-color: black;\n padding: 0;\n }\n " +
+ "</style>\n</head>\n<body>\n <table>\n"
+ end
+
+ def generate(matrix)
+ maximum_intensity = matrix.map(&:max).max
+ @content + matrix.map do |raw|
+ " <tr>\n" + raw.map do |pixel|
+ " <td style=\"opacity: " +
+ "#{format('%.2f', pixel.to_f / maximum_intensity)}\"></td>\n"
+ end.reduce(:+) + " </tr>\n"
+ end.reduce(:+) + " </table>\n</body>\n</html>"
+ end
+end

Методи обнови решението на 01.12.2015 06:58 (преди над 8 години)

module TurtleGraphics
module Canvas
end
end
class TurtleGraphics::Turtle
attr_reader :drawboard
- def initialize(rows, columns, &block)
+ def initialize(rows, columns)
@row, @column = 0, 0
@direction = [0,1]
@drawboard = Array.new(rows) { Array.new(columns) { 0 } }
@drawboard[@row][@column] = 1
end
def draw(style = self, &block)
instance_eval &block
style.generate(@drawboard)
end
def generate(drawboard)
drawboard
end
def move()
- @row = (@row + @direction[0]) % @drawboard.size
- @column = (@column + @direction[1]) % @drawboard[0].size
+ calculate_coordinates @row + @direction[0], @column + @direction[1]
@drawboard[@row][@column] += 1
end
def turn_left()
change_orientation(0, 1)
end
def turn_right()
- change_orientation(1,0)
+ change_orientation(1, 0)
end
def spawn_at(row, column)
- @row, @column = row, column
+ calculate_coordinates(row, column)
+ @drawboard[@row][@column] += 1
end
def look(direction)
@direction = case direction
when :left [ 0, -1]
when :right [ 0, 1]
when :up [-1, 0]
when :down [ 1, 0]
end
end
private
def change_orientation(vertical, horizontal)
if(@direction[vertical] != 0)
- @direction = @direction.reverse
+ @direction = @direction.reverse
else
@direction[vertical] = @direction[vertical] - @direction[horizontal]
@direction[horizontal] = 0
end
+ end
+
+ def calculate_coordinates(row, column)
+ @row = row % @drawboard.size
+ @column = column % @drawboard[0].size
end
end
class TurtleGraphics::Canvas::ASCII
attr_reader :symbols
def initialize(symbols)
@symbols = symbols
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
coefficient = maximum_intensity.to_f / (@symbols.length - 1)
matrix.map do |raw|
raw.map do |pixel|
@symbols[pixel / coefficient]
end
end
end
end
class TurtleGraphics::Canvas::HTML
attr_accessor :content
def initialize(pixel_size)
@content = "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics" +
"</title>\n <style>\n table {\n border-spacing: 0;\n }\n" +
" tr {\n padding: 0;\n }\n td {\n width: " +
"#{pixel_size}px;\n height: #{pixel_size}px;\n " +
"background-color: black;\n padding: 0;\n }\n " +
"</style>\n</head>\n<body>\n <table>\n"
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
@content + matrix.map do |raw|
" <tr>\n" + raw.map do |pixel|
" <td style=\"opacity: " +
"#{format('%.2f', pixel.to_f / maximum_intensity)}\"></td>\n"
end.reduce(:+) + " </tr>\n"
end.reduce(:+) + " </table>\n</body>\n</html>"
end
end

Методи обнови решението на 02.12.2015 07:22 (преди над 8 години)

module TurtleGraphics
module Canvas
end
end
class TurtleGraphics::Turtle
attr_reader :drawboard
def initialize(rows, columns)
@row, @column = 0, 0
- @direction = [0,1]
+ @direction = [0, 1]
@drawboard = Array.new(rows) { Array.new(columns) { 0 } }
@drawboard[@row][@column] = 1
end
def draw(style = self, &block)
instance_eval &block
style.generate(@drawboard)
end
def generate(drawboard)
drawboard
end
def move()
calculate_coordinates @row + @direction[0], @column + @direction[1]
@drawboard[@row][@column] += 1
end
def turn_left()
change_orientation(0, 1)
end
def turn_right()
change_orientation(1, 0)
end
def spawn_at(row, column)
+ @drawboard[0][0] -= 1
calculate_coordinates(row, column)
@drawboard[@row][@column] += 1
end
def look(direction)
@direction = case direction
when :left [ 0, -1]
when :right [ 0, 1]
when :up [-1, 0]
when :down [ 1, 0]
end
end
private
def change_orientation(vertical, horizontal)
if(@direction[vertical] != 0)
@direction = @direction.reverse
else
@direction[vertical] = @direction[vertical] - @direction[horizontal]
@direction[horizontal] = 0
end
end
def calculate_coordinates(row, column)
@row = row % @drawboard.size
@column = column % @drawboard[0].size
end
end
class TurtleGraphics::Canvas::ASCII
attr_reader :symbols
def initialize(symbols)
@symbols = symbols
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
coefficient = maximum_intensity.to_f / (@symbols.length - 1)
matrix.map do |raw|
raw.map do |pixel|
@symbols[pixel / coefficient]
end
end
end
end
class TurtleGraphics::Canvas::HTML
attr_accessor :content
def initialize(pixel_size)
@content = "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics" +
- "</title>\n <style>\n table {\n border-spacing: 0;\n }\n" +
- " tr {\n padding: 0;\n }\n td {\n width: " +
- "#{pixel_size}px;\n height: #{pixel_size}px;\n " +
- "background-color: black;\n padding: 0;\n }\n " +
- "</style>\n</head>\n<body>\n <table>\n"
+ "</title>\n <style>\n table {\n border-spacing: 0;\n }\n" +
+ " tr {\n padding: 0;\n }\n td {\n width: " +
+ "#{pixel_size}px;\n height: #{pixel_size}px;\n " +
+ "background-color: black;\n padding: 0;\n }\n " +
+ "</style>\n</head>\n<body>\n <table>\n"
end
def generate(matrix)
maximum_intensity = matrix.map(&:max).max
@content + matrix.map do |raw|
" <tr>\n" + raw.map do |pixel|
" <td style=\"opacity: " +
"#{format('%.2f', pixel.to_f / maximum_intensity)}\"></td>\n"
end.reduce(:+) + " </tr>\n"
end.reduce(:+) + " </table>\n</body>\n</html>"
end
end