Combination up to n
arrays, combinations, ruby
Solution
Do as below :
a = %w[a b c]
n = 3
0.upto(n).flat_map { |i| a.combination(i).to_a }
# => [[], ["a"], ["b"], ["c"], ["a", "b"],
# ["a", "c"], ["b", "c"], ["a", "b", "c"]]
Problem
Given an array `a`, what is the best way to achieve its combinations up to the `n`-th? For example: ``` a = %i[a b c] n = 2 # Expected => [[], [:a], [:b], [:c], [:a, b], [:b, :c], [:c, :a]] ```