-6
\$\begingroup\$

The challenge is simple: Print the first and last element of a list and print them

Rules:

  • You will take only 1 line of input, and that is the list (e.g. [1, 5, 6, 4, 2])
  • You are allowed to print the 2 numbers in any order with or without spaces between them
  • Output the 2 numbers on 1 line and the remaining list on the second line

A simple example is print(list.pop()+list.pop(0)) in Python3, since pop removes the element and returns it at the same time

This is code-golf, so shortest code wins!

\$\endgroup\$
6
  • 4
    \$\begingroup\$ Your example would not be valid as it would output the sum of the numbers and not the two numbers separately. \$\endgroup\$
    – hyper-neutrino
    Dec 29, 2021 at 0:21
  • 4
    \$\begingroup\$ Also, I would advise against the strict output requirement. \$\endgroup\$
    – hyper-neutrino
    Dec 29, 2021 at 0:22
  • 5
    \$\begingroup\$ Since the output is just the first and last elements, there is no observable way to check that they have been removed from the list. You could require the remaining list be output, though this might not be the original list, and in some languages this distinction isn't meaningful. \$\endgroup\$
    – xnor
    Dec 29, 2021 at 0:24
  • 6
    \$\begingroup\$ The requirement to print to STDOUT (which disallows function submissions) with no spaces separating (which adds nothing to the challenge). I would recommend allowing any reasonable output of the first and last values in either order. \$\endgroup\$
    – hyper-neutrino
    Dec 29, 2021 at 0:25
  • 7
    \$\begingroup\$ For future reference, I highly recommend using the Sandbox before posting so you can get feedback, suggestions, and clarifications first. \$\endgroup\$
    – hyper-neutrino
    Dec 29, 2021 at 0:26

4 Answers 4

5
\$\begingroup\$

Vyxal aṠj, 8 bytes

₌₍htḢṪWṠ

Try it Online!

\$\endgroup\$
0
3
\$\begingroup\$

Jelly, 5 bytes

Ḣ³ṪṄṛ

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 25 bytes

a=>alert(a[0]+''+a.pop())

f=
a=>alert(a[0]+''+a.pop())

console.log(f([1,2,3,4,5]))

\$\endgroup\$
0
2
\$\begingroup\$

Retina 0.8.2, 9 bytes

(?=,).*,

Try it online! Explanation: Just removes the commas and anything in between. The lookahead deals with the case when there are only two elements in the list.

\$\endgroup\$

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