30 votes
Accepted

Efficient lazy weak compositions

Chunks of weak compositions Here is slightly modified version of algorithm used in Combinatorica`NextComposition converted to a ...
user avatar
  • 14.7k
25 votes

Partitioning with varying partition size

New in 11.2 is TakeList: TakeList[Range[10], {2, 3, 5}] {{1, 2}, {3, 4, 5}, {6, 7, 8, 9, 10}}
user avatar
  • 122k
23 votes

Partitioning a number into consecutive integers

The sum of consecutive numbers from $a$ to $b$ is $$\frac{(a+b)(b-a+1)}{2}$$ hence simply ...
user avatar
  • 23.2k
21 votes
Accepted

How to split a number

You can also use NumberDecompose with the basis {10000, 100, 1}: ...
user avatar
  • 348k
18 votes

Improving speed of code computing number of nonrepeating partitions

Here is a summary of comments (before @ciao's best answer above), with a change in notation. These functions calculate the number of partitions of n into exactly <...
user avatar
  • 14.8k
18 votes
Accepted

Improving speed of code computing number of nonrepeating partitions

This seems pretty quick, particularly on larger cases / larger k, e.g. 451, 29, 101 finishes in a few seconds on the loungebook. N.B. - I have not tested this ...
18 votes

Partition a list by count of a number

It is always good to start with System` functions: Flatten /@ Partition[Split[list, #1 =!= 2 &], UpTo[3]] ...
user avatar
  • 132k
18 votes

Is there concise code for the list operation I want to perform?

Li = Range[5]; TakeDrop[Li, #] & /@ Range[Length[Li]-1] // Column[Row/@#]& or, slightly shorter, ...
user avatar
  • 348k
17 votes
Accepted

How to generate all possible orderless partitions of a list according to another list?

A solution using Repeated, ReplaceList, and the Orderless attribute. ...
user avatar
  • 264k
16 votes

Improving speed of code computing number of nonrepeating partitions

Here is a totally different approach based on the fact that successive products forming the generating function are due to multiplication by a binomial $1+t*z^j$. Form a matrix $v$ of zeros with $n+1$ ...
user avatar
  • 14.8k
16 votes
Accepted

Random Partitions

...
user avatar
15 votes

Partitioning with varying partition size

This can be implemented elegantly with FoldPairList and TakeDrop (both new in v10.2), in fact it's one of the examples in the ...
user avatar
  • 5,559
15 votes

How to generate all possible orderless partitions of a list according to another list?

Permutations treats repeated elements as identical, so you can get a flattened version of the desired result with something like ...
user avatar
  • 83.2k
14 votes
Accepted

Partitioning an image based on features

You can use ImageTrim to extract the bounding boxes from the image. ...
user avatar
  • 83.2k
13 votes

Is there concise code for the list operation I want to perform?

This is literally the canonical example from the ReplaceList documentation: ...
user avatar
  • 264k
12 votes
Accepted

Index-based Array splitting

For the simple case of even and odd, you can do either: ...
user avatar
  • 86.8k
12 votes

While partitioning the elements in a list using GatherBy, can I correspondingly partition the elements of an unrelated list?

Here is another approach. The basic idea is that GatherBy creates a list of representatives corresponding to the input, then partitions the input based on those ...
user avatar
  • 122k
12 votes
Accepted

Goldbach Partition

Take a look at IntegerPartitions, although it relies on brute-force enumeration that is unlikely to scale well. ...
user avatar
  • 264k
12 votes

Any alternative way to compute IntegerPartitions?

There are 190,569,292 unrestricted integer partitions of 100 (PartitionsP@100). This will need >1gb of RAM just to keep the final result. You can generate them in ...
user avatar
  • 25k
12 votes

How to generate all possible orderless partitions of a list according to another list?

It's far from pretty, using pattern matching (OrderlessPatternSequence): ...
user avatar
  • 14.6k
12 votes

How to generate all possible orderless partitions of a list according to another list?

...
user avatar
  • 5,709
12 votes
Accepted

Merge list repeating elements

lst = {{{a, b}, {c, d}}, {{e, f}, {h, i}}}; You can use Tuples or Outer or ...
user avatar
  • 348k
11 votes
Accepted

Partitioning a number into consecutive integers

I took it as a challenge to avoid using Solve, which can be slower than a direct assault. If $a$ is the first number in the sum of consecutive positive integers, ...
user avatar
  • 14.8k
11 votes

How can I extract parts from a ragged nested list?

Using Part you could do something like the following: ...
user avatar
  • 7,299
10 votes

generating integer partitions

I needed to do this sometime ago while investigating Bell polynomial analogs. Normally, you'd do FrobeniusSolve[Range[n], n] but the fastest variation (and quite ...
user avatar
10 votes
Accepted

GatherBy/SplitBy and Sort a list

As noted by @SimonWoods in the comments, using #.#& instead of Norm gives a huge speed up. ...
user avatar
  • 348k
10 votes

How to split a number

DateList[{IntegerString @ #, {"Year", "", "Month", "", "Day"}}][[;; 3]] & @ 19001231 ...
user avatar
  • 348k
9 votes

Any alternative way to compute IntegerPartitions?

You may use PartitionsP to skip calculating the partitions. This will improve performance "infinity-fold" (in practical terms) for the integer frequency counts on ...
user avatar
  • 39.3k
9 votes

Partitioning a set of integers

Split[{1, 2, 3, 6, 7, 9, 10}, #2 - #1 == 1 &] {{1, 2, 3}, {6, 7}, {9, 10}}
user avatar
  • 6,425
9 votes
Accepted

How to Partition rest rows by the first row?

lst2 = lst; lst2[[2 ;;]] = TakeList[Flatten @ #, Length /@ lst[[1]]] & /@ lst[[2 ;;]]; lst2 {{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {{"A", "...
user avatar
  • 348k

Only top scored, non community-wiki answers of a minimum length are eligible