Class: Bocadillo::Cover
- Inherits:
-
Object
- Object
- Bocadillo::Cover
- Defined in:
- lib/bocadillo/cover.rb
Class Method Summary collapse
Class Method Details
.cover(arr = [], x0 = 0, y = 0, l = nil) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bocadillo/cover.rb', line 2 def self.cover(arr = [], x0 = 0, y = 0, l = nil) l = arr.length if l.nil? # Check bad input if x0 >= l # Error: past assigned slice return false end if x0 > arr.length - 1 # Error: past array length return false; end if y > arr[x0].length - 1 if x0 === l - 1 # Termination of rightmost string return [nil] end # The string at x0 was only as long as y0, so call on the next item return [nil].concat(self.cover(arr, x0 + 1, y, l)) end # Initialize non-trivial return structure u = { 'l' => arr[x0][y], 'c' => [] } r = [u] # Spread in width until we find either the end, or a different char x = x0 loop do m = false if x < l && arr[x0][y] === arr[x][y] m = true x += 1 end break unless m end # If we haven't covered the full length of the assigned slice, launch # on the pair (x + 1, y0) with the original length l. if x < l r.concat(self.cover(arr, x, y, l)) end # Launch on the tail of the string at s[x0] from the position y u['c'].concat(self.cover(arr, x0, y + 1, x)) r end |