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 ...
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}}
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
...
21
votes
Accepted
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 <...
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]]
...

Kuba♦
- 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,
...
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.
...
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$ ...
16
votes
Accepted
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 ...
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
...
14
votes
Accepted
Partitioning an image based on features
You can use ImageTrim to extract the bounding boxes from the image.
...
13
votes
Is there concise code for the list operation I want to perform?
This is literally the canonical example from the ReplaceList documentation:
...
12
votes
Accepted
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 ...
12
votes
Accepted
Goldbach Partition
Take a look at IntegerPartitions, although it relies on brute-force enumeration that is unlikely to scale well.
...
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 ...
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):
...
12
votes
12
votes
Accepted
Merge list repeating elements
lst = {{{a, b}, {c, d}}, {{e, f}, {h, i}}};
You can use Tuples or Outer or ...
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, ...
11
votes
How can I extract parts from a ragged nested list?
Using Part you could do something like the following:
...
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 ...
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.
...
10
votes
How to split a number
DateList[{IntegerString @ #, {"Year", "", "Month", "", "Day"}}][[;; 3]] & @ 19001231
...
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 ...
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}}
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", "...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
partitions × 146list-manipulation × 71
combinatorics × 40
number-theory × 10
performance-tuning × 8
summation × 6
matrix × 5
graphs-and-networks × 5
string-manipulation × 5
permutation × 5
plotting × 4
assignment × 4
functions × 3
table × 3
random × 3
code-request × 3
map × 3
algorithm × 3
sets × 3
subset × 3
combinatorica × 3
equation-solving × 2
graphics × 2
function-construction × 2
probability-or-statistics × 2