Решение на Шеста задача от Стоян Желязков

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

Към профила на Стоян Желязков

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = initialize_matrix(rows, columns)
@position = {:row => 0, :column => 0}
@orientation = :right
end
def spawn_at(row, column)
@position[:row] = row
@position[:column] = column
@matrix[@position[:row]][@position[:column]] += 1
end
def look(orientation)
@orientation = orientation
end
def turn_left
@orientation = case @orientation
when :left
:down
when :down
:right
when :right
:up
when :up
:left
end
end
def turn_right
@orientation = case @orientation
when :left
:up
when :up
:right
when :right
:down
when :down
:left
end
end
def move
factors = MOVE_FACTORS[@orientation]
new_position = {
:row => @position[:row] + factors.first,
:column => @position[:column] + factors.last}
set_position(new_position)
end
def draw(canvas, &methods)
spawn_at(0, 0)
instance_eval &methods
canvas.apply_matrix(@matrix.clone)
canvas.get_canvas
end
private
MOVE_FACTORS = {
:left => [0, -1],
:right => [0, 1],
:up => [-1, 0],
:down => [1, 0]}
def set_position(position)
if position[:row] > @rows
@position[:row] = 0
else
@position[:row] = position[:row]
end
if position[:column] > @columns
@position[:column] = 0
else
@position[:column] = position[:column]
end
@matrix[@position[:row]][@position[:column]] += 1
end
def initialize_matrix(rows, columns)
matrix = []
rows.times do
matrix.push Array.new(columns, 0)
end
matrix
end
end
module Canvas
class ASCII
def initialize(palette)
@palette = palette
@canvas = ""
end
def apply_matrix(matrix)
map = create_intensity_map(matrix)
maximum = get_maximum_value(matrix)
matrix.each do |row|
row.each do |pixel|
@canvas << get_pixel_palette_value(pixel, maximum, map)
end
@canvas << "\n"
end
@canvas.chomp("\n")
end
def get_canvas
@canvas.clone
end
private def get_pixel_palette_value(pixel, highest_pixel, intensity_map)
intensity = get_pixel_intensity(pixel, highest_pixel)
value = ' '
intensity_map.keys.each do |range_maximum|
if intensity <= range_maximum
return intensity_map[range_maximum]
end
end
value
end
private def get_pixel_intensity(pixel, maximum)
pixel / maximum.to_f
end
private def create_intensity_map(matrix)
map = Hash.new
maximum = get_maximum_value(matrix)
step = 1.0 / (@palette.length - 1)
current = 0
@palette.each do |item|
map[current] = item
current += step
end
map
end
private def get_maximum_value(matrix)
maximum = 0
matrix.each do
|row|
highest_in_row = row.max
if highest_in_row > maximum
maximum = highest_in_row
end
end
maximum
end
end
end
end

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

FFFFFFFFF..FFF

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: canvas = TurtleGraphics::Turtle.new(20, 20).draw do
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:265:in `block (2 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 Turtle #draw #move marks where the turtle has moved
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:10: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) TurtleGraphics Turtle #draw #move moves the turtle to the start of row or column when we are at its end
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:15: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) TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:34: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) TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:46: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)>'

  6) TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:56: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)>'

  7) TurtleGraphics Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:74: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)>'

  8) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:92: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)>'

  9) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151203-5272-1r218bl/solution.rb:55:in `draw'
     # /tmp/d20151203-5272-1r218bl/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1r218bl/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)>'

  10) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1r218bl/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:166: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)>'

  11) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1r218bl/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:219: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)>'

  12) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-1r218bl/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1r218bl/spec.rb:228: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.00841 seconds
14 examples, 12 failures

Failed examples:

rspec /tmp/d20151203-5272-1r218bl/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-1r218bl/spec.rb:9 # TurtleGraphics Turtle #draw #move marks where the turtle has moved
rspec /tmp/d20151203-5272-1r218bl/spec.rb:14 # TurtleGraphics Turtle #draw #move moves the turtle to the start of row or column when we are at its end
rspec /tmp/d20151203-5272-1r218bl/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-1r218bl/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-1r218bl/spec.rb:55 # TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
rspec /tmp/d20151203-5272-1r218bl/spec.rb:73 # TurtleGraphics Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
rspec /tmp/d20151203-5272-1r218bl/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-1r218bl/spec.rb:98 # TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
rspec /tmp/d20151203-5272-1r218bl/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1r218bl/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-1r218bl/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Стоян обнови решението на 29.11.2015 15:23 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @matrix = initialize_matrix(rows, columns)
+ @position = {:row => 0, :column => 0}
+ @orientation = :right
+ end
+
+ def spawn_at(row, column)
+ @position[:row] = row
+ @position[:column] = column
+ @matrix[@position[:row]][@position[:column]] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def turn_left
+ @orientation = case @orientation
+ when :left
+ :down
+ when :down
+ :right
+ when :right
+ :up
+ when :up
+ :left
+ end
+ end
+
+ def turn_right
+ @orientation = case @orientation
+ when :left
+ :up
+ when :up
+ :right
+ when :right
+ :down
+ when :down
+ :left
+ end
+ end
+
+ def move
+ factors = get_move_factors
+ new_position = {
+ :row => @position[:row] + factors.first,
+ :column => @position[:column] + factors.last}
+ set_position(new_position)
+ end
+
+ def draw(canvas, &methods)
+ spawn_at(0, 0)
+ instance_eval &methods
+ canvas.apply_matrix(@matrix.clone)
+ canvas.get_canvas
+ end
+
+ private def get_move_factors
+ case @orientation
+ when :left
+ return [0, -1]
+ when :right
+ return [0, 1]
+ when :up
+ return [-1, 0]
+ when :down
+ return [1, 0]
+ end
+ end
+
+ private def set_position(position)
+ if position[:row] > @rows
+ @position[:row] = 0
+ else
+ @position[:row] = position[:row]
+ end
+
+ if position[:column] > @columns
+ @position[:column] = 0
+ else
+ @position[:column] = position[:column]
+ end
+
+ @matrix[@position[:row]][@position[:column]] += 1
+ end
+
+ private def initialize_matrix(rows, columns)
+ matrix = []
+ rows.times do
+ row = []
+ columns.times do
+ row.push 0
+ end
+ matrix.push row
+ end
+ matrix
+ end
+ end
+
+ module Canvas
+ class ASCII
+ def initialize(palette)
+ @palette = palette
+ @canvas = ""
+ end
+
+ def apply_matrix(matrix)
+ map = create_intensity_map(matrix)
+ maximum = get_maximum_value(matrix)
+ matrix.each do
+ |row|
+ row.each do
+ |pixel|
+ @canvas << get_pixel_palette_value(pixel, maximum, map)
+ end
+ @canvas << "\n"
+ end
+ @canvas.chomp("\n")
+ end
+
+ def get_canvas
+ @canvas.clone
+ end
+
+ private def get_pixel_palette_value(pixel, highest_pixel, intensity_map)
+ intensity = get_pixel_intensity(pixel, highest_pixel)
+ value = ' '
+ intensity_map.keys.each do
+ |range_maximum|
+ if intensity <= range_maximum
+ return intensity_map[range_maximum]
+ end
+ end
+ value
+ end
+
+ private def get_pixel_intensity(pixel, maximum)
+ pixel / maximum.to_f
+ end
+
+ private def create_intensity_map(matrix)
+ map = Hash.new
+ maximum = get_maximum_value(matrix)
+ step = 1.0 / (@palette.length - 1)
+ current = 0
+ @palette.each do
+ |item|
+ map[current] = item
+ current += step
+ end
+ map
+ end
+
+ private def get_maximum_value(matrix)
+ maximum = 0
+ matrix.each do
+ |row|
+ highest_in_row = row.max
+ if highest_in_row > maximum
+ maximum = highest_in_row
+ end
+ end
+ maximum
+ end
+ end
+ end
+end

Здравей :) Малко коментари:

  • Имаш стилови проблеми. Например, по конвенция, private не се използва така. Също, аргументите на блока не се слагат на нов ред.
  • initialize_matrix изглежда прекалено като на C/Java. Опитай да го направиш без да мутираш масив. Има методи, различни от each в езика. :)
  • get_maximum_value може да стане много по-ясно на един ред. Виж какво има в стандартната библиотека.
  • turn_left и turn_right са доста подобни. Можеш ли да измислиш как да не ти се налага да правиш case?
  • get_move_factors плаче да бъде изнесен като константа :)

Стоян обнови решението на 02.12.2015 12:16 (преди над 8 години)

module TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = initialize_matrix(rows, columns)
@position = {:row => 0, :column => 0}
@orientation = :right
end
def spawn_at(row, column)
@position[:row] = row
@position[:column] = column
@matrix[@position[:row]][@position[:column]] += 1
end
def look(orientation)
@orientation = orientation
end
def turn_left
@orientation = case @orientation
when :left
:down
when :down
:right
when :right
:up
when :up
:left
end
end
def turn_right
@orientation = case @orientation
when :left
:up
when :up
:right
when :right
:down
when :down
:left
end
end
def move
- factors = get_move_factors
+ factors = MOVE_FACTORS[@orientation]
new_position = {
:row => @position[:row] + factors.first,
:column => @position[:column] + factors.last}
set_position(new_position)
end
def draw(canvas, &methods)
spawn_at(0, 0)
instance_eval &methods
canvas.apply_matrix(@matrix.clone)
canvas.get_canvas
end
- private def get_move_factors
- case @orientation
- when :left
- return [0, -1]
- when :right
- return [0, 1]
- when :up
- return [-1, 0]
- when :down
- return [1, 0]
- end
- end
+ private
- private def set_position(position)
+ MOVE_FACTORS = {
+ :left => [0, -1],
+ :right => [0, 1],
+ :up => [-1, 0],
+ :down => [1, 0]}
+
+ def set_position(position)
if position[:row] > @rows
@position[:row] = 0
else
@position[:row] = position[:row]
end
if position[:column] > @columns
@position[:column] = 0
else
@position[:column] = position[:column]
end
@matrix[@position[:row]][@position[:column]] += 1
end
- private def initialize_matrix(rows, columns)
+ def initialize_matrix(rows, columns)
matrix = []
rows.times do
- row = []
- columns.times do
- row.push 0
- end
- matrix.push row
+ matrix.push Array.new(columns, 0)
end
matrix
end
end
module Canvas
class ASCII
def initialize(palette)
@palette = palette
@canvas = ""
end
def apply_matrix(matrix)
map = create_intensity_map(matrix)
maximum = get_maximum_value(matrix)
- matrix.each do
- |row|
- row.each do
- |pixel|
+ matrix.each do |row|
+ row.each do |pixel|
@canvas << get_pixel_palette_value(pixel, maximum, map)
end
@canvas << "\n"
end
@canvas.chomp("\n")
end
def get_canvas
@canvas.clone
end
private def get_pixel_palette_value(pixel, highest_pixel, intensity_map)
intensity = get_pixel_intensity(pixel, highest_pixel)
value = ' '
- intensity_map.keys.each do
- |range_maximum|
+ intensity_map.keys.each do |range_maximum|
if intensity <= range_maximum
return intensity_map[range_maximum]
end
end
value
end
private def get_pixel_intensity(pixel, maximum)
pixel / maximum.to_f
end
private def create_intensity_map(matrix)
map = Hash.new
maximum = get_maximum_value(matrix)
step = 1.0 / (@palette.length - 1)
current = 0
- @palette.each do
- |item|
+ @palette.each do |item|
map[current] = item
current += step
end
map
end
private def get_maximum_value(matrix)
maximum = 0
matrix.each do
|row|
highest_in_row = row.max
if highest_in_row > maximum
maximum = highest_in_row
end
end
maximum
end
end
end
-end
+end