Combination for boolean value array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Combination for boolean value array

Sapna Mishra
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be2b5076-5a38-404a-a630-e7bfa9aeb30f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Combination for boolean value array

Tales Cione
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
--
Tales.


On Fri, Feb 22, 2019 at 9:36 AM Sapna Mishra <[hidden email]> wrote:
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be2b5076-5a38-404a-a630-e7bfa9aeb30f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJ_sHYNPGrkgqpBHG_crjB48DQcNjJ6iL8FSdZj7n7EpiSfOrQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Combination for boolean value array

Hassan Schroeder-2
In reply to this post by Sapna Mishra
On Fri, Feb 22, 2019 at 4:36 AM Sapna Mishra <[hidden email]> wrote:

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

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

What actual problem are you trying to solve?

--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Combination for boolean value array

Marco Antonio Filho
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.
#
# step_conditions_labels - The Array of conditions.
#
# Examples
#
# conditions_combination(%w[true false true])
# # => {1=>true, 2=>true, 3=>false}
#
# conditions_combination(%w[true false true false true])
# # => {1=>true, 2=>true, 3=>true, 4=>false, 5=>false}
#
# Returns a hash of the evaluation of respective combinations.
def conditions_combination(step_conditions_labels)
  # Reduce the Array to the result Hash.
  step_conditions_labels.each_with_index.reduce({}) do |hash, (_, index)|
    # Update each of the Hash keys with the evalutation of the combination.
    hash.update(
      index + 1 =>
        # Eval is the way to go here as Tales mentioned, but it's a very
        # dangerous method. Be careful!
        eval(
          # Array#combination is what does the trick for you. Check it out:
          # The other lines just help on building the condition String.
          step_conditions_labels
            .combination(index + 1)
            .map { |combination| "(#{combination.join(' && ')})" }
            .join(' || ')
        )
    )
  end
end
conditions_combination(%w[true false true])

Best regards,
/ Marco

On Fri, Feb 22, 2019 at 6:56 PM Hassan Schroeder <[hidden email]> wrote:
On Fri, Feb 22, 2019 at 4:36 AM Sapna Mishra <[hidden email]> wrote:

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

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

What actual problem are you trying to solve?

--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACMkcE5HWvKpa8%3D8YWgdnVgwYW03OCbBw3%2BCetMPrH-9Xj31yQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Combination for boolean value array

Sapna Mishra
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.

On Saturday, February 23, 2019 at 2:40:53 AM UTC+5:30, Marco Antonio Almeida wrote:
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.
#
# step_conditions_labels - The Array of conditions.
#
# Examples
#
# conditions_combination(%w[true false true])
# # => {1=>true, 2=>true, 3=>false}
#
# conditions_combination(%w[true false true false true])
# # => {1=>true, 2=>true, 3=>true, 4=>false, 5=>false}
#
# Returns a hash of the evaluation of respective combinations.
def conditions_combination(step_conditions_labels)
  # Reduce the Array to the result Hash.
  step_conditions_labels.each_with_index.reduce({}) do |hash, (_, index)|
    # Update each of the Hash keys with the evalutation of the combination.
    hash.update(
      index + 1 =>
        # Eval is the way to go here as Tales mentioned, but it's a very
        # dangerous method. Be careful!
        eval(
          # Array#combination is what does the trick for you. Check it out:
          # <a href="https://ruby-doc.org/core-2.6.1/Array.html#method-i-combination" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fruby-doc.org%2Fcore-2.6.1%2FArray.html%23method-i-combination\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH0d0QBqyALszdqQ-acwQwyCfCM-w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fruby-doc.org%2Fcore-2.6.1%2FArray.html%23method-i-combination\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH0d0QBqyALszdqQ-acwQwyCfCM-w&#39;;return true;">https://ruby-doc.org/core-2.6.1/Array.html#method-i-combination
          # The other lines just help on building the condition String.
          step_conditions_labels
            .combination(index + 1)
            .map { |combination| "(#{combination.join(' && ')})" }
            .join(' || ')
        )
    )
  end
end
conditions_combination(%w[true false true])

Best regards,
/ Marco

On Fri, Feb 22, 2019 at 6:56 PM Hassan Schroeder <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="dtXNZr_pBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">hassan.s...@...> wrote:
On Fri, Feb 22, 2019 at 4:36 AM Sapna Mishra <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="dtXNZr_pBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">sapna....@...> wrote:

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

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

What actual problem are you trying to solve?

--
Hassan Schroeder ------------------------ <a href="javascript:" target="_blank" gdf-obfuscated-mailto="dtXNZr_pBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">hassan.s...@...
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="dtXNZr_pBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="dtXNZr_pBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCg%2Bm9gv5%3DuESBmdzYOSYCV%3DCspNdbkhB3hucVEXgbFcQ%40mail.gmail.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a6777a65-fb5d-4f65-8b85-c208d4a6ec55%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.