4
$\begingroup$

Suppose the triangular array is given by

 T[n_, k_] := T[n, k] = If[k < 0 || n < k, 0, If[n == 0 && k == 0, 1, 
    If[k == 0, T[n - 1, n - 1], (n+1)*T[n - 1, k - 1] + (n-1)*T[n - 1, k - 2]]]];
 Table[T[n, k], {n, 0, 5}, {k, 0, n}]

which produces

    {{1}, {1, 2}, {2, 3, 7}, {7, 8, 16, 34}, {34, 35, 61, 104, 218}, 
{218, 204, 346, 506, 868, 1724}}

What Drop, Select, or other, method can be used to only keep the first and last row to establish the set

{1}, {1,2}, {2,7}, {7,34}, {34, 218}, {218, 1724}...
$\endgroup$
0
3
$\begingroup$

Try:

mylist = {{1}, {1, 2}, {2, 3, 7}, {7, 8, 16, 34}, {34, 35, 61, 104, 218}, {218, 204, 346, 506, 868, 1724}};

Join[Take[#, 1], Take[#, -1]] & /@ mylist

works for this special case.

But you want

If[Length[#] > 1, Join[Take[#, 1], Take[#, -1]], #] & /@ mylist

in a more general case.

$\endgroup$
1
  • $\begingroup$ Carl... yep... thanks! $\endgroup$ Dec 15 '17 at 20:17
7
$\begingroup$

Another possibiity is to use Drop:

list = {{1}, {1, 2}, {2, 3, 7}, {7, 8, 16, 34}, {34, 35, 61, 104, 218}, {218, 204, 346, 506, 868, 1724}};

Drop[
    list,
    None,
    UpTo[2];;-2
]

{{1}, {1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}

$\endgroup$
5
$\begingroup$

yet another. this takes first and last of each row regardless of value.

Prepend[#[[2 ;;, {1, -1}]], #[[1]]] &@mylist

{{1}, {1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}

or

MapAt[#[[{1, -1}]] &, mylist, 2 ;;]

same

$\endgroup$
5
$\begingroup$
lst = {{1}, {1, 2}, {2, 3, 7}, {7, 8, 16, 34}, {34, 35, 61, 104, 218},
       {218, 204, 346, 506, 868, 1724}};

Extract:

Extract[lst, {{1}, {2 ;;, {1, -1}}}] (* for version 9 *)

{{1}, {{1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}}

This form of Extract does not work in versions 10+. Lengthier version below does work:

Extract[lst, Prepend[Thread[{Range[2,Length@lst], {1,-1}}, List, 1], {{1}}]]

Part:

lst[[##]] & @@@ {{1}, {2 ;;, {1, -1}}}

Replace:

Replace[lst, {a_, ___, b_} :> {a, b}, {1}]

Partition:

The tables produced using the function T have a special structure, namely, the last element of entry i is the first element of entry i+1. So we can take the last elements and partition them to get the desired output:

Partition[lst[[;;, -1]], 2, 1, -1, {}] &

{{1}, {{1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}}

Fun:

☺ = {#, ## & @@ {##2}[[;;, {1, -1}]]} & @@ # &;
☺ @ lst

{{1}, {{1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}}

$\endgroup$
2
  • $\begingroup$ any thoughts on whether the changes made to Extract in V10 that stop the above from working are an oversight or deliberate? $\endgroup$ Dec 15 '17 at 21:54
  • $\begingroup$ @Mike, your guess is as good as mine:) $\endgroup$
    – kglr
    Dec 15 '17 at 22:26
3
$\begingroup$

First and Last work pretty well, though you might need to deal with singletons separately:

list = {{1}, {1, 2}, {2, 3, 7}, {7, 8, 16, 34}, {34, 35, 61, 104, 
    218}, {218, 204, 346, 506, 868, 1724}};

{First[#], Last[#]} & /@ list
{{1, 1}, {1, 2}, {2, 7}, {7, 34}, {34, 218}, {218, 1724}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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