Skip to main content
added 554 characters in body
Source Link
Bob Hanlon
  • 162.6k
  • 7
  • 81
  • 205
$Version

"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

You need two items in the first list so the first element should appear at position -2 rather than -1.

Partition[Range[50], 8, 8, -2, {}]

{{1, 2}, {3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33, 34}, {35, 36, 37, 38, 39, 40, 41, 42}, {43, 44, 45, 46, 47, 48, 49, 50}}

list = Range[RandomInteger[{40, 100}]];

len = Length[list]

75

n = RandomInteger[{5, 9}]

7

If[Mod[len, n] == 0, Partition[list, n], 
 Partition[list, n, n, -Mod[len, n], {}]]

{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33}, {34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47}, {48, 49, 50, 51, 52, 53, 54}, {55, 56, 57, 58, 59, 60, 61}, {62, 63, 64, 65, 66, 67, 68}, {69, 70, 71, 72, 73, 74, 75}}

$Version

"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

You need two items in the first list so the first element should appear at position -2 rather than -1.

Partition[Range[50], 8, 8, -2, {}]

{{1, 2}, {3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33, 34}, {35, 36, 37, 38, 39, 40, 41, 42}, {43, 44, 45, 46, 47, 48, 49, 50}}

$Version

"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

You need two items in the first list so the first element should appear at position -2 rather than -1.

Partition[Range[50], 8, 8, -2, {}]

{{1, 2}, {3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33, 34}, {35, 36, 37, 38, 39, 40, 41, 42}, {43, 44, 45, 46, 47, 48, 49, 50}}

list = Range[RandomInteger[{40, 100}]];

len = Length[list]

75

n = RandomInteger[{5, 9}]

7

If[Mod[len, n] == 0, Partition[list, n], 
 Partition[list, n, n, -Mod[len, n], {}]]

{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33}, {34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47}, {48, 49, 50, 51, 52, 53, 54}, {55, 56, 57, 58, 59, 60, 61}, {62, 63, 64, 65, 66, 67, 68}, {69, 70, 71, 72, 73, 74, 75}}

Source Link
Bob Hanlon
  • 162.6k
  • 7
  • 81
  • 205

$Version

"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

You need two items in the first list so the first element should appear at position -2 rather than -1.

Partition[Range[50], 8, 8, -2, {}]

{{1, 2}, {3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33, 34}, {35, 36, 37, 38, 39, 40, 41, 42}, {43, 44, 45, 46, 47, 48, 49, 50}}