Friday, March 2, 2012

Juggling Notation Kata

I performed my first kata for 8th Light University today. It's a method that determines whether or not a pattern is composed entirely of repeating sub-patterns.

That is:
repeat?([4,4,1,4,4,1]) # => true
repeat?([3,3,3]) # => true
repeat?([4,4,1]) # => false


It was good to practice a kata over and over again until it felt like second nature. On the other hand, performing it in front of 60 people was a lot more difficult than I thought it would be. Under pressure, I somehow lost my ability to see detail, and I stumbled...and stumbled some more. Then I decided to erase my previous step and start over. I regained my composure and finished the kata.

Thinking, coding, narrating, and performing at the same time was quite challenging, but it's good to be challenged. If things are always easy and error-free, it's a good sign that you're stagnating in your progress.

Here's the code for my repeat Kata:

def repeat?(pattern)
  chunks(pattern).each do |chunk|
    if divides_evenly?(pattern, chunk)
      slices = []
      pattern.each_slice(chunk) {|sub| slices << sub}
      return true if slices.uniq.length == 1
    end
  end
  return false
end

def chunks(pattern)
  (1..pattern.length/2)
end

def divides_evenly?(pattern, chunk)
  pattern.length % chunk == 0
end

No comments:

Post a Comment