0

I was looking for a function that returns the partitions of an integer. I found almost what I wanted in this question:

Print all unique integer partitions given an integer as input

This function prints the partitions of an integer. But how can I change it in order to actually store and return all the partitions (maybe in a List)?

I'm aware that Java has the combinatoricslib. Is there something similar in C#? If not, how could I make that function return all possible partitions?

2
  • 3
    Do you have any code you are getting stuck on? Commented Aug 16, 2015 at 2:29
  • Simply replace System.out.println(suffix); in the linked code by partitionList.Add(suffix) using a List<String> partitionList Commented Aug 16, 2015 at 14:55

1 Answer 1

0

The link you gave are simply printing the result, but you want the result returned. The best way is likely to add an extra "result collector" parameter the "print" method, and have that method add to the collector instead of printing each partition.

Depending on what you want, your collector would be one of the following:

// Each partition is a string of the numbers, like when printed
List<String> partitions;
// Each partition is a list of the numbers
List<List<Integer>> partitions;
answered Aug 16, 2015 at 3:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.