SHARE
TWEET

Untitled

a guest Sep 12th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # @values = (1..8000).to_a # teste com muitos elementos
  2. @values = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] # teste com elementos nao inteiros
  3. @cached_sequences = {}
  4.  
  5. def get_sequence index
  6.   return @cached_sequences[index] if @cached_sequences[index]
  7.  
  8.   current_value = @values[index]
  9.   is_last = index == @values.count - 1
  10.   return [[current_value]] if is_last
  11.  
  12.   next_sequences = get_sequence(index + 1)
  13.   next_sequences_with_current = [[current_value]] + next_sequences.map { |sequence| sequence.unshift(current_value) }
  14.  
  15.   @cached_sequences[index] = next_sequences_with_current
  16.   return next_sequences_with_current
  17. end
  18.  
  19. @values.count.times.map{ |index| get_sequence(index) }.reduce([]){ |sequences, current| sequences + current }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top