19
\$\begingroup\$

This is similar to Making an acronym, but there are several key differences, including the method of fetching the acronym, and this challenge including flexible output.

Task

Given a string (list of chars/length 1 strings is allowed) containing only printable ASCII, output all capital letters in the input that are either preceded by a space or a dash, or are the first character in the input. Empty string is undefined behavior.

Test Cases:

Output may be in the format of "TEST", ["T","E","S","T"], or whatever else works for you.

Self-contained Underwater Breathing Apparatus
SUBA
a Programming Language
PL
NATO Atlantic TREATY Organization
NATO
DEFCON 2
D
hello, world!
light-Emitting dioDe
E
What Does the Fox Say?
WDFS
3D mov-Ies
I
laugh-Out Lou-D
OLD
Best friends FOREVE-r
BF
--
<space>
-- --a - - --
-- -- - - -- A
A
Step-Hen@Gmail-Mail Mail.CoM m
SHMM

This is , so shortest answer in bytes wins.

asked Jul 21, 2017 at 13:21
\$\endgroup\$
4
  • \$\begingroup\$ Sandbox \$\endgroup\$ Commented Jul 21, 2017 at 13:21
  • \$\begingroup\$ Can we take input as a list of Strings (list of characters)? \$\endgroup\$ Commented Jul 21, 2017 at 13:43
  • \$\begingroup\$ @Mr.Xcoder yes. \$\endgroup\$ Commented Jul 21, 2017 at 13:45
  • \$\begingroup\$ Could you add a test case that includes some letters separated by one or more characters that aren't letters, numbers, spaces or dashes. An e-mail address, for example: [email protected] . \$\endgroup\$ Commented Jul 21, 2017 at 16:12

34 Answers 34

1
2
1
\$\begingroup\$

Ruby, (削除) 55 (削除ここまで) (削除) 30 (削除ここまで) 29 + 1 = (削除) 56 (削除ここまで) (削除) 31 (削除ここまで) 30 bytes

Run with -e flag (+1)

p (?-+$_).scan(/[ -]([A-Z])/)

Explanation: this is much easier as a regex if there's a hyphen in the beginning to match

Note: this returns a 2d list of characters like [["S"], ["U"], ["B"], ["A"]], which should be fine because

Output may be in the format of "TEST", ["T","E","S","T"], or whatever else works for you.

answered Aug 10, 2018 at 21:49
\$\endgroup\$
1
1
\$\begingroup\$

JavaScript, 49 bytes

s=>(s.match(/(^| |-)[A-Z]/g)||[]).map(x=>x[1]||x)

Try it online

answered Jan 4, 2019 at 16:09
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 bytes

Outputs an array of characters, or the empty array.

f"%b%A

Try it

answered Jul 18, 2019 at 16:16
\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 86 bytes

	S =' ' INPUT
S	S ANY(' -') ANY(&UCASE) . X REM . S	:F(O)
	O =O X	:(S)
O	OUTPUT =O
END

Try it online!

	S =' ' INPUT					;* prepend a space
S	S ANY(' -') ANY(&UCASE) . X REM . S	:F(O)	;* find an initial
	O =O X	:(S)					;* add to output string
O	OUTPUT =O					;* print
END
answered Jul 26, 2019 at 15:34
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.