31
2
Given a non-empty list/array containing only non-negative integers like this:
[0, 0, 0, 8, 1, 4, 3, 5, 6, 4, 1, 2, 0, 0, 0, 0]
Output the list with trailing and leading zeroes removed.
The output for this would be:
[8, 1, 4, 3, 5, 6, 4, 1, 2]
Some other test cases:
[0, 4, 1, 2, 0, 1, 2, 4, 0] > [4, 1, 2, 0, 1, 2, 4]
[0, 0, 0, 0, 0, 0] > nothing
[3, 4, 5, 0, 0] > [3, 4, 5]
[6] > [6]
Shortest code wins
Are the numbers non-negative integers only? I suggest you clarify that or add test cases with other numbers – Luis Mendo – 2016-02-12T20:08:31.643
1Can we assume that there will be at least one leading and one trailing 0? – James – 2016-02-12T20:24:12.000
@LuisMendo Yes, only non-negative integers. – Lamaro – 2016-02-12T22:56:50.657
@DJMcGoathem No, it is possible that there is no leading or trailing 0 – Lamaro – 2016-02-12T22:57:28.820
4What constitutes nothing? I can think of several different things that are variations on nothing in Perl 6.
Nil
()
/[]
slip()
/Empty
Any
{}
some of them are undefined, some defined but singular, some that slip into other lists such that they don't increase the number of elements. ( There are as many different variations onAny
as there are classes/types and roles ) – Brad Gilbert b2gills – 2016-02-13T00:41:42.1307Is it a coincidence that there are no integers over 10 or can we assume that all the numbers are going to be single-digit? – A Simmons – 2016-02-13T17:57:06.580
Huh. I was thinking about posting this the other day, actually. +1 – Addison Crump – 2016-02-21T16:08:07.707
1Can we input/output the list as a delimited string? For example:
"0,4,1,2,0,1,2,4,0" => "4,1,2,0,1,2,4"
EDIT: Just noticed many languages do this already. – Mwr247 – 2016-02-24T18:18:07.910