Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

Quickly Finding Combinations

###Quickly Finding Combinations TheThe function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == target_sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == target_sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

Quickly Finding Combinations

The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == target_sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]
added 7 characters in body
Source Link
Zack
  • 1.9k
  • 11
  • 13

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == sumtarget_sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == target_sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]
Added additional line of code
Source Link
Zack
  • 1.9k
  • 11
  • 13

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == sum }
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]

This is built assuming you want no duplicates in the results, since the goal is a little unclear.

###Quickly Finding Combinations The function you should be using is Array.combination(n), which generates an enumerator that yields all combinations of size n from the array. Once you have that it is a trivial matter to only select the combinations that sum to the target value.

My function here finds all combinations of size 2 to size data.length and then selects only the combinations with the correct sum.

def find_sum(data, target_sum)
 (2..data.length)
 .flat_map { |n| data.combination(n).to_a }
 .select { |arr| arr.inject(:+) == sum }
 #.uniq # can call this to remove duplicates if necessary, depending on what the input data looks like
end
data = (1..9).to_a
pp find_sum(data, 10)
#[[1, 9],
# [2, 8],
# [3, 7],
# [4, 6],
# [1, 2, 7],
# [1, 3, 6],
# [1, 4, 5],
# [2, 3, 5],
# [1, 2, 3, 4]]
Source Link
Zack
  • 1.9k
  • 11
  • 13
Loading
lang-rb

AltStyle によって変換されたページ (->オリジナル) /