in reply to Re^2: printing every 2nd entry in a list backwards
in thread printing every 2nd entry in a list backwards
Doing this on my machine, the despicable snake language comes out ahead
Even a snake can do what other beasts can't. No need to worry. Perl doesn't have a stepping range operator, it doesn't even have a descending one. So the indices must be built every line through. OTOH, knowing that the input lines always contain 10 items, it is trivial to beat the snake:
qwurx [shmem] ~> time perl -lne 'BEGIN{$,=" "}print+(split)[9,7,5,3,1] +' in.txt >/dev/null real 0m1.589s user 0m1.588s sys 0m0.004s qwurx [shmem] ~> time python list.py in.txt >/dev/null real 0m2.204s user 0m2.188s sys 0m0.016s
And your snake code does - according to the spec Given a line of numbers from a file, print every 2nd number starting from the back - get it right only for an even number of integers:
qwurx [shmem] ~> python Python 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> line = "1 2 3 4" >>> line.split()[::-2] ['4', '2'] >>> line = "1 2 3 4 5" >>> line.split()[::-2] ['5', '3', '1'] >>> ^D
So, what does every 2nd number starting from the back mean? If we start from the back and take every 2nd, the output for the even example should be ['3','1'], and for the odd example ['4','2']. If we count every 2nd from the beginning, the output ought to be ['4','2'] for both cases.
Because you presented
Example:
1 22 3 -4 ==> -4 22
almost all code examples in this thread assumed that every 2nd meant counting from the beginning, but outputting in reverse order. Tell the snake to do that, and compare again.
|
---|