5
$\begingroup$

Partition can have lots of different arguments so I am curious whether the following results can be achieved using single Partition instead of two.

Example 1:

Partition[#,2,1]&/@Partition[Range[12],3]

(* {{{1,2},{2,3}},{{4,5},{5,6}},{{7,8},{8,9}},{{10,11},{11,12}}} *)

Example 2:

Partition[#,2,1]&/@Partition[Range[20],4]

(* {{{1,2},{2,3},{3,4}},{{5,6},{6,7},{7,8}},{{9,10},{10,11},{11,12}},{{13,14},{14,15},{15,16}},{{17,18},{18,19},{19,20}}} *)
$\endgroup$
0

1 Answer 1

2
$\begingroup$

You need to have nested lists in order to have partitions with different offsets at different levels.

1st example

Starting with:

pts0 = Partition[#, 2, 1] & /@ Partition[Range[12], 3]

pts0sol = 
 First@Partition[{Range[1, 11], Range[2, 12]}, {2, 2}, {2, 3}]

pts0 == pts0sol

True


2nd Example

pts1 = Partition[#, 2, 1] & /@ Partition[Range[20], 4]

pts2sol = 
 First@Partition[{Range[1, 20], Range[2, 21], Range[3, 22]}, {3, 2}, {2, 4}]

pts1 == pts2sol

True

In my opinion, this would become less readable with increased nesting complexity.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.