Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by w88dy · Oct 11, 2017 at 11:18 PM · listmathalgorithminteger

Combination of a list - no repeats, specify length

I've been spending quite some time on a problem that I hoped would be fairly straightforward.

I want to populate a list of integers based on the combinations of another list of integers. There can't be any repeats and I want to be able to specify the length of combinations, for example:

List: {0, 1, 2}

Combinations with a length of 1: {0}, {1}, {2}

Combinations with a length of 2: {0, 1}, {0, 2}, {1, 2}

Combinations with a length of 3: {0, 1, 2}

The original list may not always have a length of 3, I just used that as an easy example. Could someone please help me with a way to do this, in C#? I've found numerous examples online, but they either use NET functions which I can't use in Unity, or don't do one of the things I want (no repeats, specify length).

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Oct 12, 2017 at 01:48 AM

That's actually quite simple. All you need to do is using as many nested for loops as you want numbers in your combination and let the inner loops start at the current outer loop +1. The easiest way would probably be a recursive method if you want to have the combination length variable.

I quickly created this helper class:

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 public class Combinations<T>
 {
     private List<T> m_Items;
     private List<List<T>> m_Result;
     private T[] current;
     private int m_Length;
     private Combinations(List<T> aItems, int aLength)
     {
         m_Items = aItems;
         m_Length = aLength;
         m_Result = new List<List<T>>();
         current = new T[aLength];
     }

     public static List<List<T>> GetCombinations(List<T> aItems, int aLength)
     {
         if (aItems == null || aItems.Count < aLength)
             return new List<List<T>>();
         var context = new Combinations<T>(aItems, aLength);
         
         context.GetCombinations(0, 0);
         return context.m_Result;
     }
     private void GetCombinations(int aStart, int aDepth)
     {
         if (aDepth >= m_Length)
             return;
         int c = m_Items.Count + aDepth - m_Length + 1;
         for (int i = aStart; i < c; i++)
         {
             current[aDepth] = m_Items[i];
             if (aDepth == m_Length-1)
                 m_Result.Add(current.ToList());
             else
                 GetCombinations(i+1, aDepth + 1);
         }
     }
 }

You can use it like this:

 List<int> numbers = new List<int> {2,3,5,7,11,13,17,23};
 List<List<int>> combinations = Combinations<T>.GetCombinations(numbers, 2);

here combinations will contain all combinations with 2 elements of the given list. I've actually tested it with the following code. If does actually print all combinations. The two foreach loops are just to convert the lists into a nice printable form.

 private void Start()
 {
     var n = new List<int> { 1,2,3,4,5,6,7,8,9,10};
     var sb = new StringBuilder();
     for(int i = 1; i < 11; i++)
     {
         var c = Combinations<int>.GetCombinations(n, i);

         sb.Length = 0;
         foreach(var k in c)
         {
             sb.Append('[');
             foreach(int v in k)
             {
                 sb.Append(v).Append(", ");
             }
             sb.Remove(sb.Length-2, 2);
             sb.Append("], ");
         }
         sb.Remove(sb.Length - 2, 2);

         Debug.Log("Combinations of length "+ i + ": " + c.Count + "\n"+ sb.ToString());
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image w88dy · Oct 12, 2017 at 02:55 PM 0
Share

This is beautiful! Thanks so much for taking the time to help. Having tested it it looks to be exactly what I want. I've also compared results against an online combination calculator and results are identical.

Thanks again.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Get highest Vector3 from list (using Linq?) 1 Answer

Subdivide Bezier Curves 3 Answers

How to find centers of inner child of a quad (geometry guy needed)? 1 Answer

Really weird behaviour with boolean variables from custom class 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges