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

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

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

Резултати

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

Код

module GameOfLife
require 'enumerator'
class Board
include Enumerable
def initialize(*args)
@cells = args.uniq
@next_cells = []
@to_go_alive = []
inspect
end
def each
@cells.each { |cell| yield cell }
end
def next_generation
evolve
resurrect
Board.new(@next_cells.uniq)
end
def [](a, b)
(@cells & [[a, b]]) != []
end
def inspect
"Board: #{@cells.inspect}"
end
private
def find_neighbours(cell)
auxiliary = [-1, -1, 0, 0, 1, 1]
offsets = auxiliary.permutation(2).to_a.uniq - [[0, 0]]
neighbours = offsets.map { |offset| [offset[0] + cell[0], offset[1] + cell[1]] }
end
def count_alive_neighbours(cell)
neighbours = find_neighbours(cell)
(@cells & neighbours).size
end
def evolve
@cells.each do |cell|
if count_alive_neighbours(cell) == 2 or count_alive_neighbours(cell) == 3
@next_cells += [cell]
end
@to_go_alive += find_neighbours(cell)
end
end
def resurrect
@to_go_alive.uniq.each do |cell|
if count_alive_neighbours(cell) == 3
@next_cells += [cell]
end
end
end
end
end

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

...........FF.FFFF

Failures:

  1) GameOfLife::Board GameOfLife::Board evolution rules kills cells in underpopulated areas
     Failure/Error: next_gen.count.should eq 0
       
       expected: 0
            got: 1
       
       (compared using ==)
     # /tmp/d20111221-3114-bwzxai/spec.rb:91:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) GameOfLife::Board GameOfLife::Board evolution rules preserves stable cells
     Failure/Error: next_gen[1, 1].should be_true
       expected false to be true
     # /tmp/d20111221-3114-bwzxai/spec.rb:103:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) GameOfLife::Board GameOfLife::Board evolution rules sprouts new life when appropriate
     Failure/Error: next_gen[1, 0].should be_true
       expected false to be true
     # /tmp/d20111221-3114-bwzxai/spec.rb:125:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
     Failure/Error: board.count.should eq cells.count
       
       expected: 7
            got: 1
       
       (compared using ==)
     # /tmp/d20111221-3114-bwzxai/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-bwzxai/spec.rb:136:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
     Failure/Error: board.count.should eq cells.count
       
       expected: 3
            got: 1
       
       (compared using ==)
     # /tmp/d20111221-3114-bwzxai/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-bwzxai/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-bwzxai/spec.rb:142:in `times'
     # /tmp/d20111221-3114-bwzxai/spec.rb:142:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable
     Failure/Error: board.count.should eq cells.count
       
       expected: 4
            got: 1
       
       (compared using ==)
     # /tmp/d20111221-3114-bwzxai/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-bwzxai/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-bwzxai/spec.rb:158:in `times'
     # /tmp/d20111221-3114-bwzxai/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-bwzxai/spec.rb:155:in `each'
     # /tmp/d20111221-3114-bwzxai/spec.rb:155:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.36166 seconds
18 examples, 6 failures

Failed examples:

rspec /tmp/d20111221-3114-bwzxai/spec.rb:81 # GameOfLife::Board GameOfLife::Board evolution rules kills cells in underpopulated areas
rspec /tmp/d20111221-3114-bwzxai/spec.rb:95 # GameOfLife::Board GameOfLife::Board evolution rules preserves stable cells
rspec /tmp/d20111221-3114-bwzxai/spec.rb:117 # GameOfLife::Board GameOfLife::Board evolution rules sprouts new life when appropriate
rspec /tmp/d20111221-3114-bwzxai/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-bwzxai/spec.rb:139 # GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
rspec /tmp/d20111221-3114-bwzxai/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Мария обнови решението на 20.12.2011 23:53 (преди над 8 години)

+module GameOfLife
+ require 'enumerator'
+
+ class Board
+ include Enumerable
+
+ def initialize(*args)
+ @cells = args.uniq
+ @next_cells = []
+ @to_go_alive = []
+ inspect
+ end
+
+ def each
+ @cells.each { |cell| yield cell }
+ end
+
+ def next_generation
+ evolve
+ resurrect
+ Board.new(@next_cells.uniq)
+ end
+
+ def [](a, b)
+ (@cells & [[a, b]]) != []
+ end
+
+ def inspect
+ "Board: #{@cells.inspect}"
+ end
+
+ private
+ def find_neighbours(cell)
+ auxiliary = [-1, -1, 0, 0, 1, 1]
+ offsets = auxiliary.permutation(2).to_a.uniq - [[0, 0]]
+ neighbours = offsets.map { |offset| [offset[0] + cell[0], offset[1] + cell[1]] }
+ end
+
+ def count_alive_neighbours(cell)
+ neighbours = find_neighbours(cell)
+ (@cells & neighbours).size
+ end
+
+ def evolve
+ @cells.each do |cell|
+ if count_alive_neighbours(cell) == 2 or count_alive_neighbours(cell) == 3
+ @next_cells += [cell]
+ end
+ @to_go_alive += find_neighbours(cell)
+ end
+ end
+
+ def resurrect
+ @to_go_alive.uniq.each do |cell|
+ if count_alive_neighbours(cell) == 3
+ @next_cells += [cell]
+ end
+ end
+ end
+ end
+end

Винаги в последния момент, without fail :)

  • За какво ти е този require 'enumerator'? По-скоро не ти трябва :)
  • Като имаш require, го слагай извън модула, в началото на файла; така или иначе, няма смисъл да е в модул
  • #inspect — gut! :) Може би не трябва да го викаш в initialize, обаче; а и там няма никакъв смисъл, върнатата стойност от initialize се изхвърля така или иначе
  • Може би щеше да е по-добре evolve и resurrect просто да връщаха резултат, вместо да тъпчат в инстанционна променлива; и после, в next_generation, нещо като Board.new *(evolve + resurrect) (или нещо в този дух)
  • Един празен ред след private и преди следващата дефиниция на метод

Хаха, този път имам fail :D Ами, който си доспива, не си дописва...

Мерси за коментарите :)

Всъщност inspect беше за дебъг, не бих го сложила в конструктура при нормални обстоятелства :D Пардон, трябваше да го махна.

А това с променливите на инстанцията е голяма идиотщина, признавам си. Хаха, сега искам да си го променя :)