Skip to main content
edited tags
Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Tweeted twitter.com/#!/StackMma/status/281258644606173184
added tag which qualifies by definition
Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198
remove uneeded extra line
Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396

Given

lst = {a, b, c, d} 

I'd like to generate

{{a}, {a, b}, {a, b, c}, {a, b, c, d}} 

but using built-in functions only, such as Subsets, Partitions, Tuples, Permutations or any other such command you can choose. But it has to be done only using built-in commands. You can use a pure function, if inside a built-in command, i.e. part of the command arguments. That is OK.

It is of course trivial to do this by direct coding. One way can be

lst[[1 ;; #]] & /@ Range[Length[lst]] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

or even

lst = {a, b, c, d} LowerTriangularize[Table[lst, {i, Length[lst]}]] /. 0 -> Sequence @@ {} (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

But I have the feeling there is a command to do this more directly as it looks like a common operation, but my limited search could not find one so far.

Sorry in advance if this was asked before. Searching is hard for such general questions.

Given

lst = {a, b, c, d} 

I'd like to generate

{{a}, {a, b}, {a, b, c}, {a, b, c, d}} 

but using built-in functions only, such as Subsets, Partitions, Tuples, Permutations or any other such command you can choose. But it has to be done only using built-in commands. You can use a pure function, if inside a built-in command, i.e. part of the command arguments. That is OK.

It is of course trivial to do this by direct coding. One way can be

lst[[1 ;; #]] & /@ Range[Length[lst]] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

or even

lst = {a, b, c, d} LowerTriangularize[Table[lst, {i, Length[lst]}]] /. 0 -> Sequence @@ {} (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

But I have the feeling there is a command to do this more directly as it looks like a common operation, but my limited search could not find one so far.

Sorry in advance if this was asked before. Searching is hard for such general questions.

Given

lst = {a, b, c, d} 

I'd like to generate

{{a}, {a, b}, {a, b, c}, {a, b, c, d}} 

but using built-in functions only, such as Subsets, Partitions, Tuples, Permutations or any other such command you can choose. But it has to be done only using built-in commands. You can use a pure function, if inside a built-in command, i.e. part of the command arguments. That is OK.

It is of course trivial to do this by direct coding. One way can be

lst[[1 ;; #]] & /@ Range[Length[lst]] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

or even

LowerTriangularize[Table[lst, {i, Length[lst]}]] /. 0 -> Sequence @@ {} (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

But I have the feeling there is a command to do this more directly as it looks like a common operation, but my limited search could not find one so far.

Sorry in advance if this was asked before. Searching is hard for such general questions.

deleted 11 characters in body; edited title
Source Link
Szabolcs
  • 238.9k
  • 32
  • 653
  • 1.3k
Loading
Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396
Loading