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

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

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

Резултати

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

Код

module GameOfLife
class Board
include Enumerable
def initialize(*cells)
@cells = cells
end
def [](x, y)
@cells.include? [x, y]
end
def each
for pair in @cells
yield pair
end
end
def count
@cells.size
end
def next_generation
ng = @cells.map {|pt| self.nearby pt}.flatten(1).uniq
ng.select! {|pt| self.should_live pt}
Board.new *ng
end
def should_live(pt)
cnt = count_near pt
(self[*pt] and (cnt == 2 or cnt == 3)) or (cnt == 3)
end
def count_near(pt)
nearby(pt).select {|pt| self[*pt] }.size
end
def nearby(pt)
[[pt[0], pt[1]+1], [pt[0], pt[1]-1], [pt[0]+1, pt[1]], [pt[0]-1, pt[1]],
[pt[0]+1, pt[1]+1], [pt[0]+1, pt[1]-1], [pt[0]-1, pt[1]+1], [pt[0]-1, pt[1]-1]]
end
end
end

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

......F...........

Failures:

  1) GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
     Failure/Error: new_board([0, 0], [1, 1], [0, 0]).count.should eq 2
       
       expected: 2
            got: 3
       
       (compared using ==)
     # /tmp/d20111221-3114-tza0yy/spec.rb:46: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.39009 seconds
18 examples, 1 failure

Failed examples:

rspec /tmp/d20111221-3114-tza0yy/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board

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

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

+module GameOfLife
+ class Board
+ include Enumerable
+
+ def initialize(*cells)
+ @cells = cells
+ end
+
+ def [](x, y)
+ @cells.include? [x, y]
+ end
+
+ def each
+ for pair in @cells
+ yield pair
+ end
+ end
+
+ def count
+ @cells.size
+ end
+
+ def next_generation
+ ng = @cells.map {|pt| self.nearby pt}.flatten(1).uniq
+ ng.select! {|pt| self.should_live pt}
+ Board.new *ng
+ end
+
+ def should_live(pt)
+ cnt = count_near pt
+ (self[*pt] and (cnt == 2 or cnt == 3)) or (cnt == 3)
+ end
+
+ def count_near(pt)
+ nearby(pt).select {|pt| self[*pt] }.size
+ end
+
+ def nearby(pt)
+ [[pt[0], pt[1]+1], [pt[0], pt[1]-1], [pt[0]+1, pt[1]], [pt[0]-1, pt[1]],
+ [pt[0]+1, pt[1]+1], [pt[0]+1, pt[1]-1], [pt[0]-1, pt[1]+1], [pt[0]-1, pt[1]-1]]
+ end
+ end
+end
  • count ти идва наготово от Enumerable; ако искаш, дефинирай си size (виж документацията)
  • Трябва да поработиш малко върху умението си да именоваш променливи и методи :)

УЖАСТ! for! ЩЕ ТИ ВЗЕМЕМ ТОЧКИ!!!!!!1

Трябва да направиш жертвоприношение, за да смекчиш гнева и да омилостивиш боговете. Бррр.

(Другия път просто ползвай each :)

  • count-a го дефинирах, защото ми се стори, че така е по условие. Но явно не съм го направила според пълните спецификации, мерси! :)
  • Ограничението за дължина на реда убива поетическите ми наклонности при именуване на променливи..
  • Мога да напиша 100 пъти "Никога няма да пиша for!": for i in (1..100) p "Никога няма да пиша for!" end =D (вярно, забравих, с each става мъничко по-красиво :)