32
\$\begingroup\$

Note: The winning answer will be selected on 4/12/17 the current winner is Jolf, 1 byte.

I'm surprised that we haven't had a what's my middle name challenge on this site yet. I did alot of searching but found nothing. If this is a dup, please flag it as such.

Your challenge

Parse a string that looks like Jo Jean Smith and return Jean.

Test cases

Input: Samantha Vee Hills
Output: Vee

Input: Bob Dillinger
Output: (empty string or newline)

Input: John Jacob Jingleheimer Schmidt
Output: Jacob Jingleheimer

Input: Jose Mario Carasco-Williams
Output: Mario

Input: James Alfred Van Allen
Output: Alfred Van 

(That last one is incorrect technically, but fixing that would be too hard.)

Notes:

  • Names will always have at least 2 space-separated parts, with unlimited middle names between them or can be a list/array of strings.
  • Names may contain the alphabet (case-insensitive) and - (0x2d)
  • You may output a trailing newline.
  • You may require input to have a trailing newline.
  • Input from STDIN, a function parameter, or command-line argument is allowed, but hard-coding it in is not allowed.
  • Standard loopholes forbidden.
  • Output may be function return value, STDOUT, STDERR, etc.
  • Trailing spaces/newlines/tabs in the output are allowed.
  • Any questions? Comment below!

This is , so the shortest anwser in bytes wins!

\$\endgroup\$
  • 2
    \$\begingroup\$ Can the output be a list of strings? \$\endgroup\$ – Anthony Pham Apr 2 '17 at 14:34
  • 5
    \$\begingroup\$ If other formats than a space-separated string are allowed, please edit that into the specification. \$\endgroup\$ – Martin Ender Apr 2 '17 at 14:46
  • 5
    \$\begingroup\$ @programmer5000: if the input can be a list of strings, how about the output? Is ["John", "Jacob", "Jingleheimer", "Schmidt"] -> ["Jacob", "Jingleheimer"] a valid solution? \$\endgroup\$ – nimi Apr 2 '17 at 15:17
  • 3
    \$\begingroup\$ Are leading spaces allowed? \$\endgroup\$ – betseg Apr 2 '17 at 15:32
  • 3
    \$\begingroup\$ @DJ Because "Van" isn't his middle name, it's part of his last name. A particularly vexing case is David Lloyd George, whose first name is David and last name is Lloyd George. Any attempt to parse real people's names like this is doomed. In fact, you can't even tell what the first and last names are (think Li Shi). \$\endgroup\$ – David Conrad Apr 3 '17 at 2:16

63 Answers 63

1 2
3
0
\$\begingroup\$

Clojure, 43 bytes:

#(drop 1(butlast(read-string(str"("%")"))))

Here we read string as an edn data, then choosing its center.

Or for stdout, 57 bytes:

#(apply pr-str(drop 1(butlast(read-string(str"("%")")))))



You can also check it here.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Is the space in str " necessary? \$\endgroup\$ – Jonathan Frech Dec 14 '18 at 16:50
  • \$\begingroup\$ @JonathanFrech it leads to "RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)" without space \$\endgroup\$ – bbb1 Dec 14 '18 at 16:55
  • \$\begingroup\$ @JonathanFrech I add link to online Clojure REPL, so you can check it. UPD: where it works... It's strange. \$\endgroup\$ – bbb1 Dec 14 '18 at 16:57
  • \$\begingroup\$ To me it looks like you changed it in your online REPL, did not change your post and said it would not work ... (TIO). \$\endgroup\$ – Jonathan Frech Dec 14 '18 at 17:27
0
\$\begingroup\$

Pip, 9 bytes

@<@>(a^s)

Slices the first and last names from the string(split on spaces) and prints it.

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl 5 + -p040, 13 bytes

$_ x=$.>1&/ /

Try it online!

Explanation

With -p, input is implicitly stored in $_, the 040 flag makes space the line terminator which means the code is called for each word/name in the input. x= is the in-place string repetition operator which modifies the string in place (e.g. $_="abc";$_ x=3;# "abcabcabc") and the condition is $.>1 ($. is set to the current 'line' number, although since space is our line end character this is word/name number) &ed with / / (if $_ m//atches , which it will for all but the last word/name). Because of -p the result (the string repeated zero or one times depending on the conditional) is implicitly printed.

| improve this answer | |
\$\endgroup\$
1 2
3

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.