Решение на Втора задача от Пламена Петрова

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

Към профила на Пламена Петрова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 13 успешни тест(а)
  • 7 неуспешни тест(а)

Код

def move(snake, direction)
grown_up_snake = snake.dup
grown_up_snake.delete_at(0)
grow(grown_up_snake, direction)
end
def all_positions_available(food, snake, dimensions)
all_positions(dimensions) - food - snake
end
def new_food(food, snake, dimensions)
(all_possitions_available(food, snake, dimensions)).sample
end
def grow(snake, direction)
grown_up_snake = snake.dup
head = grown_up_snake.last
grown_up_snake.push([head[0] + direction[0], head[1] + direction[1]])
end
def all_positions(dimensions)
width_of_field = (dimensions[:width]).pred
height_of_field = (dimensions[:height]).pred
((0..width_of_field).to_a).product((0..height_of_field).to_a)
end
def obstacle_ahead?(snake, direction, dim)
next_move = (grow(snake, direction)).last
(snake.include? next_move) or
(not ((all_positions(dim)).include? next_move))
end
def danger?(snake, direction, dimensions)
next_move = move(snake, direction)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(next_move, direction, dimensions)
end

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

.F....FFFFFF........

Failures:

  1) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:18:in `grow'
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:4:in `move'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:12: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) #new_food generates food on empty position
     Failure/Error: let(:next_food) { new_food(food, snake, dimensions) }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fbc2a78>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:47:in `block (2 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:53: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)>'

  3) #new_food does not generate food outside of borders (width)
     Failure/Error: let(:next_food) { new_food(food, snake, dimensions) }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fbc2758>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:47:in `block (2 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:57: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)>'

  4) #new_food does not generate food outside of borders (height)
     Failure/Error: let(:next_food) { new_food(food, snake, dimensions) }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fbbc9c0>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:47:in `block (2 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:61: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)>'

  5) #new_food does not generate food on position where the snake is
     Failure/Error: let(:next_food) { new_food(food, snake, dimensions) }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fbaa388>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:47:in `block (2 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:65: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)>'

  6) #new_food does not generate food on position where food is already present
     Failure/Error: let(:next_food) { new_food(food, snake, dimensions) }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fba2f20>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:47:in `block (2 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:69: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)>'

  7) #new_food does not mutate the given arguments
     Failure/Error: expect { new_food(food, snake, dimensions) }.not_to change { snake }
     NoMethodError:
       undefined method `all_possitions_available' for #<RSpec::Core::ExampleGroup::Nested_3:0x007f1d9fb9e6a0>
     # /tmp/d20151026-15631-1oz4lxp/solution.rb:12:in `new_food'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:73:in `block (3 levels) in <top (required)>'
     # /tmp/d20151026-15631-1oz4lxp/spec.rb:73: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)>'

Finished in 0.01159 seconds
20 examples, 7 failures

Failed examples:

rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:49 # #new_food generates food on empty position
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:56 # #new_food does not generate food outside of borders (width)
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:60 # #new_food does not generate food outside of borders (height)
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:64 # #new_food does not generate food on position where the snake is
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:68 # #new_food does not generate food on position where food is already present
rspec /tmp/d20151026-15631-1oz4lxp/spec.rb:72 # #new_food does not mutate the given arguments

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

Пламена обнови решението на 16.10.2015 11:42 (преди над 9 години)

+def move(snake, direction)
+ snake_result = snake.dup
+ snake_result.delete_at(0)
+ grow(snake_result, direction)
+end
+
+def all_positions_available(food, snake, dimensions)
+ all = all_positions(dimensions)
+ (all - food) - snake
+end
+
+def new_food(food, snake, dimensions)
+ (all_positions_available(food, snake, dimensions)).sample
+end
+
+def grow(snake, direction)
+ snake_result = snake.dup
+ head = snake_result.delete_at(-1)
+ snake_result.insert -1, head
+ snake_result.insert -1, [head[0] + direction[0], head[1] + direction[1]]
+end
+
+def all_positions(dimensions)
+ size_width = (dimensions[:width]) - 1
+ size_height = (dimensions[:height]) - 1
+ all = ((0..size_width).to_a).product((0..size_height).to_a)
+end
+
+def obstacle_ahead?(snake, direction, dim)
+ next_move = (grow(snake, direction)).delete_at(-1)
+ (snake.include? next_move) | (not ((all_positions(dim)).include? next_move))
+end
+
+def danger?(snake, direct, dim)
+ next_move = (grow(snake, direction)).delete_at(-1)
+ obstacle_ahead?(snake, direct, dim) | obstacle_ahead?(next_move, direct, dim)
+end

Като цяло добре. Няколко идеи:

  • Ако поразровиш още малко документацията, ще намериш няколко метода, които са ти липсвали. Например в move - Array#drop, в grow - Array#last и Array#push.
  • Не използвай | за обикновено или. Ние предпочитаме or, || също е приемливо.
  • Слагаш малко повече скоби, отколкото е нужно. При някои по-сложни изрази е добре да се прави за яснота. В общия случай е безсмислено.
  • Защо не използва вече наличния move в danger??
  • Присвояването all = в all_positions е излишно.

Откъм имена:

  • dim изглежда е съкратено заради ограничението символи на ред. Това не е добър аргумент. :)
  • Избягвай result в имената си. Всичко е някакъв вид резултат. Вместо това се запитай какво всъщност е въпросната стойност. Например резултатната змия в grow всъщност е пораснала змия.
  • size_width/size_height звучат като "размер на дължината/ширината". Можеше да използваш Integer#pred или range-a с три точки: (0...dimensions[:width]).

Пламена обнови решението на 19.10.2015 15:04 (преди над 9 години)

def move(snake, direction)
- snake_result = snake.dup
- snake_result.delete_at(0)
- grow(snake_result, direction)
+ grown_up_snake = snake.dup
+ grown_up_snake.delete_at(0)
+ grow(grown_up_snake, direction)
end
def all_positions_available(food, snake, dimensions)
- all = all_positions(dimensions)
- (all - food) - snake
+ all_positions(dimensions) - food - snake
end
def new_food(food, snake, dimensions)
- (all_positions_available(food, snake, dimensions)).sample
+ (all_possitions_available(food, snake, dimensions)).sample
end
def grow(snake, direction)
- snake_result = snake.dup
- head = snake_result.delete_at(-1)
- snake_result.insert -1, head
- snake_result.insert -1, [head[0] + direction[0], head[1] + direction[1]]
+ grown_up_snake = snake.dup
+ head = grown_up_snake.last
+ grown_up_snake.push([head[0] + direction[0], head[1] + direction[1]])
end
def all_positions(dimensions)
- size_width = (dimensions[:width]) - 1
- size_height = (dimensions[:height]) - 1
- all = ((0..size_width).to_a).product((0..size_height).to_a)
+ width_of_field = (dimensions[:width]).pred
+ height_of_field = (dimensions[:height]).pred
+ ((0..width_of_field).to_a).product((0..height_of_field).to_a)
end
def obstacle_ahead?(snake, direction, dim)
- next_move = (grow(snake, direction)).delete_at(-1)
- (snake.include? next_move) | (not ((all_positions(dim)).include? next_move))
+ next_move = (grow(snake, direction)).last
+ (snake.include? next_move) or
+ (not ((all_positions(dim)).include? next_move))
end
-def danger?(snake, direct, dim)
- next_move = (grow(snake, direction)).delete_at(-1)
- obstacle_ahead?(snake, direct, dim) | obstacle_ahead?(next_move, direct, dim)
+def danger?(snake, direction, dimensions)
+ next_move = move(snake, direction)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(next_move, direction, dimensions)
end