Решение на Шеста задача от Георги Стефанов

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

Към профила на Георги Стефанов

Резултати

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

Код

class Array
def to_cells(tag)
self.map { |c| "<#{tag}>#{c}</#{tag}>" }.join
end
end
class TurtleGraphics
def work(word, args)
counter = 0
while counter < word.length
if counter <= 3
word.gsub!("#{counter}", args[counter])
else
word.gsub!("#{counter}", args[3])
end
counter += 1
end
puts format(word)
end
def format(word)
count, counter = 0, 1
while counter < word.length
if counter % @row == 0
word.insert(counter + count, "\n")
counter, count = counter + 1, count + 1
else
counter += 1
end
end
word
end
def generate_table(number, field)
rows = [{:a => "", :b => ""}, {:c => "", :d => ""}]
cells = rows.map do |row|
"<tr>#{row.values.to_cells('td')}</tr>"
end.join("\n ")
table = "<table>\n#{cells}\n</table>"
puts table
end
class Canvas
class ASCII
def initialize(*args)
@symbols = args
end
def get_symbols
@symbols.flatten
end
end
class HTML
def initialize(number)
@number = number
end
def get_number
@number
end
end
end
class Turtle < TurtleGraphics
def initialize(row = 0, column = 0)
@row = row
@column = column
@position = [0, 0]
@next_position = [0, 1]
@field = Array.new(row){Array.new(column){0}}
#@field[@position[0]][@position[1]] += 1
end
def get_field
@field
end
def next_position(row, column)
@next_position[0] = row
@next_position[1] = column
end
def spawn_at(row = 0, column = 0)
@position = [row, column]
@field[@position[0]][@position[1]] += 1
end
def look(orientation)
case orientation
when :up then next_position(1, 0)
when :down then next_position(-1, 0)
when :left then next_position(0, -1)
when :right then next_position(0, 1)
end
end
def draw(*args, &block)
self.instance_eval(&block)
word = @field.flatten.join
if args == []
self.get_field
elsif args[0].class == TurtleGraphics::Canvas::HTML
generate_table(args[0].get_number, self.get_field)
else
work(word, args[0].get_symbols)
end
end
def move
if @field[@position[0]][@position[1]] == 0
@field[@position[0]][@position[1]] += 1
end
@position[0] += @next_position[0]
@position[1] += @next_position[1]
@field[@position[0]][@position[1]] += 1
@field
end
def turn_right
case @next_position
when [0, 1] then @next_position = [1, 0]
when [1, 0] then @next_position = [0, -1]
when [0, -1] then @next_position = [-1, 0]
when [-1, 0] then @next_position = [0, 1]
end
end
def turn_left
case @next_position
when [0, 1] then @next_position = [-1, 0]
when [-1, 0] then @next_position = [0, -1]
when [0, -1] then @next_position = [1, 0]
when [1, 0] then @next_position = [0, 1]
end
end
end
end

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

..FFF....320
110
000
FF<table>
<tr><td></td><td></td></tr>
  <tr><td></td><td></td></tr>
</table>
F<table>
<tr><td></td><td></td></tr>
  <tr><td></td><td></td></tr>
</table>
F<table>
<tr><td></td><td></td></tr>
  <tr><td></td><td></td></tr>
</table>
F

Failures:

  1) TurtleGraphics Turtle #draw #move moves the turtle to the start of row or column when we are at its end
     Failure/Error: 3.times { move }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/solution.rb:117:in `move'
     # /tmp/d20151203-5272-z5qnud/spec.rb:16:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/spec.rb:16:in `times'
     # /tmp/d20151203-5272-z5qnud/spec.rb:16:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `instance_eval'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `draw'
     # /tmp/d20151203-5272-z5qnud/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-z5qnud/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)>'

  2) TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
     Failure/Error: 4.times { move }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/solution.rb:117:in `move'
     # /tmp/d20151203-5272-z5qnud/spec.rb:36:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/spec.rb:36:in `times'
     # /tmp/d20151203-5272-z5qnud/spec.rb:36:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `instance_eval'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `draw'
     # /tmp/d20151203-5272-z5qnud/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-z5qnud/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)>'

  3) TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
     Failure/Error: 2.times { move }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/solution.rb:117:in `move'
     # /tmp/d20151203-5272-z5qnud/spec.rb:47:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/spec.rb:47:in `times'
     # /tmp/d20151203-5272-z5qnud/spec.rb:47:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `instance_eval'
     # /tmp/d20151203-5272-z5qnud/solution.rb:100:in `draw'
     # /tmp/d20151203-5272-z5qnud/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-z5qnud/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)>'

  4) 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 nil:NilClass
     # /tmp/d20151203-5272-z5qnud/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)>'

  5) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii = TurtleGraphics::Turtle.new(3, 3).draw(ascii_canvas) do
     TypeError:
       no implicit conversion of nil into String
     # /tmp/d20151203-5272-z5qnud/solution.rb:13:in `gsub!'
     # /tmp/d20151203-5272-z5qnud/solution.rb:13:in `work'
     # /tmp/d20151203-5272-z5qnud/solution.rb:107:in `draw'
     # /tmp/d20151203-5272-z5qnud/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)>'

  6) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/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)>'

  7) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/spec.rb:220: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)>'

  8) 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+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-z5qnud/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.0095 seconds
14 examples, 8 failures

Failed examples:

rspec /tmp/d20151203-5272-z5qnud/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-z5qnud/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-z5qnud/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-z5qnud/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-z5qnud/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-z5qnud/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-z5qnud/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-z5qnud/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

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

+class Array
+ def to_cells(tag)
+ self.map { |c| "<#{tag}>#{c}</#{tag}>" }.join
+ end
+end
+
+class TurtleGraphics
+
+ def work(word, args)
+ counter = 0
+ while counter < word.length
+ if counter <= 3
+ word.gsub!("#{counter}", args[counter])
+ else
+ word.gsub!("#{counter}", args[3])
+ end
+ counter += 1
+ end
+ puts format(word)
+ end
+
+ def format(word)
+ count, counter = 0, 1
+ while counter < word.length
+ if counter % @row == 0
+ word.insert(counter + count, "\n")
+ counter, count = counter + 1, count + 1
+ else
+ counter += 1
+ end
+ end
+ word
+ end
+
+ def generate_table(number, field)
+ rows = [{:a => "", :b => ""}, {:c => "", :d => ""}]
+ cells = rows.map do |row|
+ "<tr>#{row.values.to_cells('td')}</tr>"
+ end.join("\n ")
+ table = "<table>\n#{cells}\n</table>"
+ puts table
+ end
+
+ class Canvas
+ class ASCII
+ def initialize(*args)
+ @symbols = args
+ end
+
+ def get_symbols
+ @symbols.flatten
+ end
+ end
+
+ class HTML
+ def initialize(number)
+ @number = number
+ end
+
+ def get_number
+ @number
+ end
+ end
+ end
+
+ class Turtle < TurtleGraphics
+ def initialize(row = 0, column = 0)
+ @row = row
+ @column = column
+ @position = [0, 0]
+ @next_position = [0, 1]
+ @field = Array.new(row){Array.new(column){0}}
+ #@field[@position[0]][@position[1]] += 1
+ end
+
+ def get_field
+ @field
+ end
+
+ def next_position(row, column)
+ @next_position[0] = row
+ @next_position[1] = column
+ end
+
+ def spawn_at(row = 0, column = 0)
+ @position = [row, column]
+ @field[@position[0]][@position[1]] += 1
+ end
+
+ def look(orientation)
+ case orientation
+ when :up then next_position(1, 0)
+ when :down then next_position(-1, 0)
+ when :left then next_position(0, -1)
+ when :right then next_position(0, 1)
+ end
+ end
+
+ def draw(*args, &block)
+ self.instance_eval(&block)
+ word = @field.flatten.join
+ if args == []
+ self.get_field
+ elsif args[0].class == TurtleGraphics::Canvas::HTML
+ generate_table(args[0].get_number, self.get_field)
+ else
+ work(word, args[0].get_symbols)
+ end
+ end
+
+ def move
+ if @field[@position[0]][@position[1]] == 0
+ @field[@position[0]][@position[1]] += 1
+ end
+ @position[0] += @next_position[0]
+ @position[1] += @next_position[1]
+ @field[@position[0]][@position[1]] += 1
+ @field
+ end
+
+ def turn_right
+ case @next_position
+ when [0, 1] then @next_position = [1, 0]
+ when [1, 0] then @next_position = [0, -1]
+ when [0, -1] then @next_position = [-1, 0]
+ when [-1, 0] then @next_position = [0, 1]
+ end
+ end
+
+ def turn_left
+ case @next_position
+ when [0, 1] then @next_position = [-1, 0]
+ when [-1, 0] then @next_position = [0, -1]
+ when [0, -1] then @next_position = [1, 0]
+ when [1, 0] then @next_position = [0, 1]
+ end
+ end
+ end
+end

Имаш сериозни стилови проблеми:

  • В TurtleGraphics има методи, които ползваш като глобални.
  • Има забравени принтирания и коментари.
  • format и work са много лоши имена за методи. Все още не мога да се ориентирам какво трябва да прави work с аргументите си word и args. И някакви gsub!-ове вътре.
  • Дефинирането на нови методи в съществуващи класове (monkey-patching) е лоша практика. Казвали сме го на лекции.

Прегледай нашето решение за идеи и другия път започни по-рано с решението.