Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9308183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:34:08+00:00 2026-06-19T00:34:08+00:00

Please take a look at the below code def test array = Array.new array2

  • 0

Please take a look at the below code

def test
  array = Array.new
  array2 = Array.new

  groups = [[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]]
  type = ["goa", "mumbai"]
  groups.each_with_index do |item,index|

       if item.include?(type[0]) == true
         array << index  << array2
        elsif item.include?(type[1]) == true
               array2 << index 
       else
         "nothing ;)"
       end

  end
  print array.each_slice(2).map { |a, b| [a, b.first] }
end
combine

#Output - [[0, 1], [2, 1]]

See the problem with the code? That is I am using a bunch of if and else statements. What if type array has more than 2 entries. I cant go on writing the if and elsif statements. And thats where I need your help. What is a better what to structure the code? loops? if so how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:34:09+00:00Added an answer on June 19, 2026 at 12:34 am

    Here is my code.

    def combinations(groups, types)
      array = Array.new(types.size) { Array.new([]) }
      groups.each_with_index do |item, index|
         types.each_with_index { |type, i| array[i] << index if item.include? type }
      end
    
      flat = array.inject { |acc, i| acc.product i }.flatten
      flat.each_slice(types.size).to_a
    end
    

    Sample test cases

    combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]], ["goa", "mumbai"])
    

    output : [[0, 1], [2, 1]]

    combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]], ["goa", "africa"])
    

    output : [[0, 2], [2, 2]]

    combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"], [123, "india"]], ["goa", "mumbai", "india"])
    

    output : [[0, 1, 3], [2, 1, 3]]

    combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "mumbai", "goa"], [123, "india"]], ["goa", "mumbai", "india", "italy"])
    

    output : [[0, 1, 3, 0], [0, 2, 3, 0], [2, 1, 3, 0], [2, 2, 3, 0]]

    If I understood your problem correctly then these should be correct. Though I might have misunderstood you. Please do tell me If i have got your problem wrong, and If you could provide test cases that would be great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this below line of code in JSF <h:inputText id=name
Please take a look at the code snippet below. I want the 2nd MongoDB
Could you please take a look to my code below, I need to increase
Possible Duplicate: Javascript Variable Variables Please take a look at code below var selected_id=1;
Please take a look at the code snippet below: interface IFoo<E>{ void doFoo(E env);
Please take a look at this code: template<class T> class A { class base
Please take a look of my code 1st. I am trying to add some
please take a look at my codes below #include <stdio.h> void printOut() { static
Can anyone take a look at the below code and point out the obvious
Please take a look at the html listed below and let me know why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.