Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.35 KB | None | 0 0
  1. input = %w(a b c)
  2. n = 5
  3.  
  4. def gen_seq(prefix, alphabet)
  5.   alphabet.select { |y| y != prefix[-1] }.map { |y| "#{prefix}#{y}" }
  6. end
  7.  
  8. def gen_seq_for_prefixes(prefixes, alphabet)
  9.   prefixes.reduce([]) { |memo, l| memo.push(*gen_seq(l, alphabet)) }
  10. end
  11.  
  12. (1...n)
  13.     .reduce(input) { |memo| gen_seq_for_prefixes(memo, input) }
  14.     .map { |x| puts "#{x}" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement