27
\$\begingroup\$

Objective

Your goal is to make a program that converts an input to its acronym. Your input is guaranteed to have only letters and spaces. The input will have exactly one space between words. You must output the acronym of the input.

Rules

  • Your code cannot be case-sensitive(e.g. foo and Foo are the same)
  • Your code must ignore the following words and not put them in the acronym: and or by of
  • You cannot assume that the words are all lowercase.
  • The output must be entirely capitalised, with no separation between characters.
  • A trailing newline is accepted but not necessary.
  • If your language has an acronym function builtin, you may not use it.

Examples

(inputs/outputs grouped)

United States of America
USA

Light Amplification by Stimulation of Emitted Radiation
LASER

united states of america
USA

Jordan Of the World
JTW

Scoring

This is a challenge so the shortest code wins.

Leaderboard

var QUESTION_ID=75448,OVERRIDE_USER=8478;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
19
  • \$\begingroup\$ I have added 2 test cases. \$\endgroup\$ – Aplet123 Mar 13 '16 at 12:05
  • 1
    \$\begingroup\$ @Aplet123 Are you sure that's what you want? Because if that's the case, the challenge boils down to removing spaces and lower case letters. (E.g. 4 bytes in Retina: T` l) \$\endgroup\$ – Martin Ender Mar 13 '16 at 12:17
  • 5
    \$\begingroup\$ Bit too late to change it, but you'd generally also expect words like "a", "an", "the", "for", "to", etc. to be removed. \$\endgroup\$ – Darrel Hoffman Mar 13 '16 at 14:58
  • 3
    \$\begingroup\$ This is setting aside the fact that USA is not an acronym, it's an abbreviation? NASA is an acronym because you say the word "nasa". If you spell out the letters, it isn't an acronym. \$\endgroup\$ – corsiKa Mar 13 '16 at 17:25
  • 1
    \$\begingroup\$ Can we assume the input will always have a non-empty output? \$\endgroup\$ – Downgoat Mar 13 '16 at 17:34

40 Answers 40

1
2
1
\$\begingroup\$

Stax, 16 12 bytes

î♣}ïû┴N=a,╥Ç

Run and debug it

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 149 bytes

#define x*strcmp(c,
F(char*s){char*d,*c=s;while(*s)*s++|=32;for(s=c;c;c=!d?*s=0:d+1)d=index(c,32),d?*d=0:0,1 x"and")x"or")x"by")x"of")?*s++=*c^32:0;}

Try it online!

Very naive approach. Will try to optimize it further. My first priority was to create a working example in C as there was no C submission yet.

Edit: optimized it by replacing initial conversion to upper case with conversion to lower case (easier as space is no longer an exception). I replaced strcmp with a define and replaced a while loop with a for. I did some further small optimizations and am now out of ideas.

Thanks to ceiling cat for bringing it down even more!

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat Thanks! I was able to bring it down to 149 by adding a comma to your define! \$\endgroup\$ – elechris Jan 22 at 16:36
0
\$\begingroup\$

IPOS - non competing, 27 bytes

uS`"AND|OR|BY|OF"ER`%S!h%S-

This works in the current version (v0.2) of the interpreter.

Example run

python IPOS.py -i "United States of America" uS`\"AND|OR|BY|OF\"ER`%S!h%S-
USA

The backslashes there are only for escaping of the quotes and are not needed for the program.

Explanation

u                  # Make the input uppercase
S                  # Push a space to the stack
`"AND|OR|BY|OF"ER` # Push a command to the stack, that replaces matches of the regex
                   # with an empty string
%                  # Split the uppercased input string on spaces, apply the command and
                   # join back on spaces. This removes the required words from the input.
S                  # Push a space to the stack
!h                 # Push a command that only selects the first character of a string
%                  # Split on spaces, apply the command and join back on spaces
S-                 # Remove the remaining spaces (forgot to make this usuable
                   # with regexes, would have saved me some bytes here :( )
\$\endgroup\$
0
\$\begingroup\$

bash / GNU coreutils, 60 bytes

sed -e's/\b'{and,o[rf],by}'\W*//Ig' -re's/(\w)\S* ?/\u\1/g'

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

Pylongolf2, 14 bytes (UTF-8)

c| l1╨3♀~

Pylongolf2 has many non-ASCII characters that count as 2 bytes.

c| l1╨3♀~
c         read input
 |        split by space (note the space after |)
   l1     substring(0, 1)
     ╨3   convert to uppercase
       ♀~ pop the array into the stack and print it.
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 19 bytes (non-competing...?)

lð¡“€ƒ€—€‹€‚“#K€нJu

Try it online!

A surely competing version (21 bytes):

lð¡“€ƒ€—€‹€‚“#Kvy0èuJ

Try it online!

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

Pip, 43 bytes

a:UCq^sFi,#a{`(OR|OF|AND|BY)`~=a@i?xOa@i@0}

definitely golfable, but I'm still learning regex

explanation:

a:UCq^s                                     input toUpper, split on " "
       Fi,#a{                             } iterate over a
             `(OR|OF|AND|BY)`~=a@i?xOa@i@0  if not exact match, output first char of this word

Try it online!

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

C# .NET, 144 bytes

class P{static void Main(string[]a){foreach(var c in a[0].ToUpper().Split(' '))if(c!="OR"&c!="AND"&c!="BY"&c!="OF")System.Console.Write(c[0]);}}

Try Online

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

05AB1E, 18 bytes

lð¡“€ƒ€—€‹€‚“#K€нu

Try it online!

lð¡“€ƒ€—€‹€‚“#K€нu  # full program
                 u  # convert...
                н   # first character of...
               €    # each element of...
                    # implicit input...
l                   # in lowercase...
  ¡                 # split by...
 ð                  # spaces...
              K     # without any of the elements of...
   “€ƒ€—€‹€‚“       # "and or by of"...
             #      # split by spaces...
                 u  # to uppercase
                    # implicit output
\$\endgroup\$
0
\$\begingroup\$

Scala, 68 bytes

_.toUpperCase.replaceAll("\\b(AND|OR|BY|OF) ?|(?<=\\b\\w)\\w+ ?","")

Try it online!

Not the most creative answer ever.

Without regex, 75 bytes

_.toUpperCase split " "filterNot Set("AND","OR","OF","BY")map(_(0))mkString

Scastie

\$\endgroup\$
1
2

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.