Remove leading and trailing zeros from a list
From the docs of Longest
:
The default is to have earlier patterns match shortest sequences
Therefore, use Longest
for the zeros:
{0, 0, 1, 0, 2} /. {Longest[0 ...], x___} :> {x}
{1, 0, 2}
From the docs of Longest
:
The default is to have earlier patterns match shortest sequences
Therefore, use Longest
for the zeros:
{0, 0, 1, 0, 2} /. {Longest[0 ...], x___} :> {x}
{1, 0, 2}