Abbreviate everything
+3
−0
Challenge
Make a program that takes input of a string and abbreviate it.
- All letters of an abbreviation are capitalized, so keep that in mind.
- Whitespace, numbers and non-English characters are ignored.
- If the inputted string is a paragraph (2+ sentences), the abbreviation will be split by spaces (
.
,?
or!
are, while commas (,
) are ignored in this splitting scenario. - Words that are connected by hyphens (
-
) will result to only one letter. - The only non-alphabetical characters to remain in the result are colons (
:
) and semicolons (;
). - This is code-golf, so shortest program in each language wins.
Test Cases
// Input
# Output
Hello, World!
HW
Can you make me a sandwich?
CYMMAS
hyper-neutrino
H
Too long; didn't read
TL;DR
I studied, don't worry. I will pass.
ISDW IWP
What???!?!
W
Get me 99 bottles of beer, bro!
GMBOBB
+3
−0
[Haskell], 139 138 bytes …
3y ago
+3
−0
[Python 3], 142 bytes d …
3y ago
+2
−0
[JavaScript (Node.js)], 91 68 …
3y ago
3 answers
+3
−0
Haskell, 139 138 bytes
import Data.Char
f(a:b)|d a=toUpper a:f(dropWhile(\a->e a"-'"||d a)b)|e a":;"=a:c|e a".?!"=' ':c|0<1=c where c=f b;d=isLetter;e=elem
f a=a
0 comment threads
+2
−0
JavaScript (Node.js), 91 68 65 bytes
s=>s.toUpperCase().match(/(?<=^| )[A-Z]|:|;|(?<=[.?!]) /g).join``
Assumes that the original text is properly formatted (has a space after punctuation). It converts the string to uppercase, then matches the following:
- A letter at the beginning of the string or preceded by a space
-
:
or;
- A space preceded by
.
,?
, or!
0 comment threads
+3
−0
Python 3, 142 bytes
def f(s):
m=1;r=""
for c in s.upper():
if'@'<c<'['or'-'==c:r+=c*m;m=0
if c in":; ":r+=c*(c!=' ');m=1
if c in".?!":r+=' ';m=1
return r
0 comment threads