# Combination for boolean value array

def conditions_combination step_conditions_labels

conditions_combination_hash = Hash.new

1.upto(step_conditions_labels.size).flat_map do |n|

conditions_combination = “”

step_conditions_labels.permutation(n).map { |condition|

if condition.length == n

conditions_combination += "( "

condition.each do |condition_label|

conditions_combination += condition_label+" && "

end

conditions_combination = conditions_combination.chomp(’ && ')

conditions_combination += " ) || "

end

conditions_combination_hash[n.to_s] = conditions_combination.chomp(’ || ‘).chomp(’ && ')

}

end

cond = false

condition_check = conditions_combination_hash[step_conditions_labels.length.to_s]

if condition_check

cond = true

end

return cond

end

I have created this function

conditions_combination_hash = conditions_combination([‘true’,‘false’,‘true’])

which gives me different combination like this

{“1”=>“( true ) || ( false ) || ( true )”, “2”=>“( true && false ) || ( true && true ) || ( false && true ) || ( false && true ) || ( true && true ) || ( true && false )”, “3”=>“( true && false && true ) || ( true && true && false ) || ( false && true && true ) || ( false && true && true ) || ( true && true && false ) || ( true && false && true )”}

Issue is now I am not able use this combination to check whether it satisfies the condition as it is consider as string class.

You probably could use something like eval. That being said, if you refactor in order to avoid this, it should lead to clearer, safer code

If I saw something like that in a PR I would reject it in ~2 seconds.

What actual problem are you trying to solve?

Hi Sapna,

This is a fun exercise to do and also to do some refactoring after I got to understand what you are aiming to do.

First of all, your code is not optimal yet. Let’s look at the output of the example you gave:

{“1”=>“( true ) || ( false ) || ( true )”, “2”=>“( true && false ) || ( true && true ) || ( false && true ) || ( false && true ) || ( true && true ) || ( true && false )”, “3”=>“( true && false && true ) || ( true && true && false ) || ( false && true && true ) || ( false && true && true ) || ( true && true && false ) || ( true && false && true )”}

The combination of 2 and 3 have duplicated information. For example: (true && false && true) are the same as (true && true && false). I have provided a solution for what I understood from your problem below with some comments.

I am considering that you need to solve this in one method, otherwise, I would split in many different methods to make the code more clear. I am also being very cheeky here to introduce many features of the language that would make your life easier and your code much more understandable.

I hope this helps.

# Evaluate the combinations of conditions.

Hello Marco,

Thank you for replying to the query with detailed explanation. This is what I wanted to do in my code but the code was not clear but you tried to understand my issue and provided the solution so thanks for that. I was not aware with the combination method before.