| Bytes | Lang | Time | Link |
|---|---|---|---|
| 018 | Japt P | 250720T133830Z | Shaggy |
| 031 | Uiua | 250302T234037Z | ErikDaPa |
| 099 | Swift 6 | 250301T182706Z | macOSist |
| 048 | AWK | 250228T215748Z | xrs |
| 026 | Pip | 240318T000610Z | DLosc |
| 016 | Thunno 2 | 230729T174232Z | The Thon |
| 019 | 05AB1E | 170719T143041Z | Erik the |
| 016 | Vyxal sr | 210524T173938Z | a stone |
| 047 | Factor | 210524T180945Z | chunes |
| 044 | Zsh | 210120T174309Z | pxeger |
| 068 | Scala | 210122T174105Z | user |
| 149 | C gcc | 210122T151558Z | elechris |
| 012 | Stax | 210121T215622Z | recursiv |
| 034 | K ngn/k | 210121T214901Z | coltim |
| 018 | Husk | 210120T115849Z | Razetime |
| 075 | R | 170719T143751Z | Giuseppe |
| 018 | 05AB1E | 210120T204912Z | Makonede |
| 144 | C# .NET | 190820T154342Z | canttalk |
| 043 | Pip | 190801T144338Z | Kenzie |
| 058 | PHP | 160315T140034Z | aross |
| 053 | JavaScript | 160402T200354Z | ericw314 |
| 046 | vim | 160313T164610Z | Doorknob |
| 148 | Pylongolf2 | 160402T181312Z | user4701 |
| 060 | bash / GNU coreutils | 160401T215119Z | meereeum |
| 027 | IPOS non competing | 160401T150619Z | Denker |
| 096 | Python | 160313T172427Z | mriklojn |
| 134 | C# | 160316T141706Z | GER |
| 027 | MATL | 160313T123021Z | Luis Men |
| 093 | Lua | 160316T041612Z | Trebuche |
| 175 | Factor | 160316T000916Z | cat |
| 020 | Jelly | 160313T125200Z | Sp3000 |
| 085 | JavaScript | 160313T143715Z | clamchow |
| 076 | Bash + GNU coreutils | 160315T111538Z | rexkogit |
| 075 | Haskell | 160313T125359Z | flawr |
| 122 | Lua | 160315T093315Z | Katenkyo |
| 056 | JavaScript ES6 | 160313T172926Z | Downgoat |
| 092 | PHP | 160314T014254Z | Justin |
| 081 | Python | 160314T152858Z | shooqie |
| 029 | Retina | 160313T121821Z | Andreas |
| 063 | JavaScript | 160313T164810Z | jdidion |
| 081 | PowerShell | 160314T013849Z | Matt |
| 043 | Ruby | 160313T162248Z | Alex A. |
| 032 | Perl | 160313T163704Z | Doorknob |
| 117 | Mathematica | 160313T155134Z | martin |
| 055 | Julia | 160313T155459Z | Alex A. |
| 022 | CJam | 160313T143948Z | NinjaBea |
| 028 | 05AB1E | 160313T121205Z | Adnan |
| 020 | Pyth | 160313T120846Z | Denker |
Uiua, 31 bytes
∵◇⊢▽¬⊸∊∩(⊜□⊸≠@ )"AND OR BY OF"⌵
Explanation:
∵◇⊢▽¬⊸∊∩(⊜□⊸≠@ )"AND OR BY OF"⌵
∩(⊜□⊸≠@ )"AND OR BY OF"⌵ => split both the input uppercased and list of ignored words
▽¬⊸∊ => filter words not in the list of ignored words
∵◇⊢ => take first letter of each word
Swift 6, 123 99 bytes
{String(($0+"").uppercased().split{$0<"!"}.filter{!$0.contains(/^(AND|O[RF]|BY)$/)}.map(\.first!))}
Ungolfed version:
func acronym(of string: String) -> String {
// uppercase the string and split it by spaces
// " " compares less than "!" -- we can use this to save a byte
let words = string.uppercased().split { $0 < "!" }
// remove extra words
let filteredWords = words.filter { !$0.contains(/^(AND|O[RF]|BY)$/) }
// get initial letters and join them
return String(filteredWords.map(\.first!))
}
Pip, 26 bytes
@_M_N"OROFBYAND"CH4FNUCa^s
Takes the string as a command-line argument. Attempt This Online!
A version that takes each word as a separate command-line argument would be 24 bytes (replace a^s at the end with g). This might meet the spec, since the invocation would look something like:
pip.py acronym.pip United states of America
Explanation
@_M_N"OROFBYAND"CH4FNUCa^s
a ; Command-line argument
UC ; Uppercase
^s ; Split on spaces
FN ; Filter, keeping elements for which this function is falsey:
"OROFBYAND" ; Take that string
CH4 ; Chop it into 4 pieces: ["OR";"OF";"BY";"AND"]
_N ; Is the argument in that list?
M ; Map this function to each remaining string:
@_ ; Get the first character
; Join and autoprint (implicit)
Thunno 2, 16 bytes
RO“œẆḶuıdƓ“ROṗ€h
Explanation
RO“œẆḶuıdƓ“ROṗ€h # Implicit input
RO # Uppercase and split on spaces
“œẆḶuıdƓ“ # Compressed string "and or by of"
RO # Uppercase and split on spaces
ṗ # Remove these words
€h # First character of each
# Implicit output
Vyxal sr, 20 16 bytes
-4 thanks to @Underslash.
⇧⌈«X⟩qḊ8ɖ⇩«⇧⌈Fvh
⇧⌈«X⟩qḊ8ɖ⇩«⇧⌈Fvh # Full program
⇧ # Convert (implicit) input converted to uppercase...
⌈ # Split on spaces
«X⟩qḊ8ɖ⇩«⇧⌈ # Create the list ['OR', 'BY', 'OF', 'AND']
F # and remove those from the input.
vh # Replace each word with its first letter
# `s` flag: Concatenate the letters and print
Zsh, 46 45 44 bytes
for i;printf ${${${i:u}:#(AND|OR|BY|OF)}[1]}
-1 thanks to @roblogic
Explanation:
for i;printf ${${${i:u}:#(AND|OR|BY|OF)}[1]}
for i; # for each word in the input
${i } # take that word
:u # capitalised
(AND|OR|BY|OF) # if it matches one of these words
${ :# } # replace it with an empty string
${ [1]} # take the first character
printf # and print with no newline
Scala, 68 bytes
_.toUpperCase.replaceAll("\\b(AND|OR|BY|OF) ?|(?<=\\b\\w)\\w+ ?","")
Not the most creative answer ever.
Without regex, 75 bytes
_.toUpperCase split " "filterNot Set("AND","OR","OF","BY")map(_(0))mkString
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;}
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!
K (ngn/k), 34 bytes
{`c$-32+*'(" "\_x)^$`and`or`by`of}
_xlowercase the input(" "\...)split input on spaces(...)^$`and`or`by`ofremove and/or/by/of*'take the first character of each remaining word`c$-32+convert to uppercase
R, 77 75 bytes
cat(substr(w<-toupper(scan(,'')),1,1)[!grepl('^(AND|OR|OF|BY)$',w)],sep='')
-1 byte thanks to Dominic van Essen changing the indexing.
Reads from stdin. Takes the words, converts them to uppercase, removes those that are excluded from acronyms, extracts the first character from each, and prints them to the stdout.
05AB1E, 18 bytes
lð¡“€ƒ€—€‹€‚“#K€нu
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
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]);}}
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
PHP, 68 61 58 bytes
Uses ISO-8859-1 encoding.
for(;$w=$argv[++$x];)stripos(_AND_OR_BY_OF,$w)||print$w&ß;
Run like this (-d added for aesthetics only):
php -d error_reporting=30709 -r 'for(;$w=$argv[++$x];)stripos(_AND_OR_BY_OF,$w)||print$w&ß; echo"\n";' united states oF america
Ungolfed:
// Iterate over the CLI arguments (words).
for(;$w = $argv[++$x];)
// Check if the word is one of the excluded words by substring index.
// The check is case insensitive.
stripos("_AND_OR_BY_OF", $w) ||
// Print the word, converting to uppercase and getting only the
// first char by using bitwise AND.
print $w & "ß";
- Saved 7 bytes by using bitwise AND instead of using
ucwords. - Saved 3 bytes by using ISO-8859-1 encoding and using
ß(binary11011111) for binaryANDinstead of a negated space (binary00100000).
JavaScript, 80 72 55 53 bytes
Code
function a(t){t=t.toUpperCase();t=t.replace(/AND|OR|BY|OF|\B.| |/g,"");return t}
function a(t){return t.toUpperCase().replace(/AND|OR|BY|OF|\B.| |/g,"")}
I just read about arrow functions and realized I could shorten this up even more.
a=t=>t.toUpperCase().replace(/AND|OR|BY|OF|\B.| |/g,"")
According to this, you don't count the assignment in the length, so -2 bytes.
t=>t.toUpperCase().replace(/AND|OR|BY|OF|\B.| |/g,"")
This is my first golf, so it's not very good.
vim, 46
gUU:s/ /\r/g<cr>:g/\vAND|OR|OF|BY/d<cr>:%s/.\zs.*\n<cr>
gUU make line uppercase
:s/ /\r/g<cr> replace all spaces with newlines
:g/\vAND|OR|OF|BY/d<cr> remove unwanted words
:%s/.\zs.*\n<cr> remove all non-initial characters and newlines
I particularly like that last bit. The first . in the regex matches the first character of the line. Then we use \zs to start the "actually-being-replaced" part, effectively not replacing the initial character. .* matches the rest of the line, and \n matches the trailing newline. Since we don't specify a replace string, vim simply removes everything in the match, leaving only initials.
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.
bash / GNU coreutils, 60 bytes
sed -e's/\b'{and,o[rf],by}'\W*//Ig' -re's/(\w)\S* ?/\u\1/g'
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 :( )
Python, 103 96 bytes
This is my first attempt at code golf, and this could probably be golfed a lot more. Thanks to DenkerAffe for saving seven characters.
lambda x:"".join([x[0]for y in x.split(" ") if y.lower() not in ['and','or','of','by']]).upper()
It takes the input, turns it into a list of words and takes their first letter if it's not one of the forbidden words, then turns everything to uppercase.
C#, 134 bytes
Golfed
class P{static void Main(string[] a){foreach (var s in a){if(!"AND OR BY OF".Contains(s.ToUpper())){Console.Write(s.ToUpper()[0]);}}}}
Readable
class P
{
static void Main(string[] a)
{
foreach (var s in a)
{
if (!"AND OR BY OF".Contains(s.ToUpper()))
{
Console.Write(s.ToUpper()[0]);
}
}
}
}
Execute from command line
75448.exe Light Amplification by Stimulation of Emitted Radiation
LASER
75448.exe united states of america
USA
MATL, 34 27 bytes
1 byte fewer thanks to @AandN
KkYb'OF AND OR BY'YbX-c1Z)!
Xk % convert to uppercase
Yb % split by spaces. Gives a cell array of input words
'AND OR BY OF' % ignored words separated by spaces
Yb % split by spaces. Gives a cell array of ignored words
X- % setdiff: remove ignored words (result is stable)
c % convert to 2D char array, padding words with spaces
1Z) % take first column
! % transpose into a row
Lua, 113 112 93 bytes
arg[1]:upper():gsub("%w+",function(w)io.write(("AND OR BY OF"):find(w)and""or w:sub(0,1))end)
Factor, 175 bytes
I learned a lot by writing this.
USING: strings ascii sets splitting kernel sequences math.ranges ;
>lower " " split [ { "and" "or" "by" "of" } in? not ] filter [ first dup [a,b] >string ] map "" join >upper
As a word:
USING: strings ascii sets splitting kernel sequences math.ranges ;
: >initialism ( str -- str )
>lower " " split ! string.lower.split(" ")
[ { "and" "or" "by" "of" } in? not ] filter ! word in { } ?
[ first dup [a,b] >string ] map ! word[0]
"" join >upper ; ! "".join.upper
Unit tests:
USING: tools.test mk-initialism ;
IN: mk-initialism.tests
{ "LASER" } [ "Light Amplification by Stimulation of Emitted Radiation" >initialism ] unit-test
{ "USA" } [ "United States OF Americaof" >initialism ] unit-test
{ "USA" } [ "united states and america" >initialism ] unit-test
{ "JTW" } [ "Jordan Of the World" >initialism ] unit-test
Pass!
Jelly, 21 20 bytes
,“°ɲịĊs°gɗ»ṣ€⁶Œuḟ/Ḣ€
(-1 thanks to @Dennis.)
,“°ɲịĊs°gɗ» Pair input with the string "OR OF by AND"
ṣ€⁶ Split both by spaces
Œu Uppercase
ḟ/ Reduce filter (removing ignored words from input)
Ḣ€ Keep first letters of remaining words
Jelly's dictionary is a bit weird in that it has AND in uppercase yet by in lowercase...
JavaScript, 104 85 bytes
Saved 19 bytes thanks to @Aplet123.
Splits the string by spaces then checks if it is the words of, or, and, or by. If it is, it ignores it, otherwise it takes the first letter of it. It then joins the array and makes the string uppercase.
a=_=>_.split` `.map(v=>/\b(o(f|r)|and|by)\b/i.test(v)?"":v[0]).join("").toUpperCase()
Ungolfed:
function a(_) {
_ = _.split` `; //Split on spaces
_ = _.map(function(v){return new RegExp("\b(o(f|r)|and|by)\b","i").test(v)}); //Check if the banned words are in the result
_ = _.join(""); //Join it into a string
_ = _.toUpperCase(); //Convert it to uppercase
};
Bash + GNU coreutils, 103 76 bytes
for i in ${@^^};do grep -qE '\b(AND|OR|BY|OF)\b'<<<$i||echo -n ${i:0:1};done
Run with
./codegolf.sh Light Amplification BY Stimulation of Emitted Radiationofo
either with single argument quoted or with multiple arguments.
(I distorted the last word to contain of).
60 bytes
Thanks to @manatwork.
for i in ${@^^};{ [[ $i = @(AND|OR|BY|OF) ]]||printf %c $i;}
Haskell, 100 99 98 82 75 bytes
I am quite sure it can be shortened a lot more as I still suck at using $,. etc. so I keep using () insted=)
Thanks @nimi for your help magic!
import Data.Char
w=words
x=[h!!0|h<-w$toUpper<$>x,notElem h$w"OF BY OR AND"]
Example:
*Main> a "united states by america"
"USA"
Lua, 122 Bytes
I would have love to use a pattern to get rid of the banned words, but sadly, lua isn't made to match groups of characters... So I had to use a for loop instead, which is much more expensive.
s=arg[1]for k,v in next,{"of","and","by","or"}do
s=s:gsub(v,"")end
print(s:gsub("(%a)%a+",string.upper):gsub("%s","").."")
Ungolfed
s=arg[1] -- initialise s with the argument
for k,v in next,{"of","and","by","or"} -- iterate over the array of banned words
do
s=s:gsub(v,"") -- replace the occurences of v by
end -- an empty string
print(s:gsub("(%a)%a+", -- replace words (separated by spaces)
string.upper) -- by their first letter capitalised
:gsub("%s","") -- replace spaces with empty strings
.."") -- concatenate to prevent the number of
-- substitutions to be shown
JavaScript (ES6), 56 bytes
Saved a byte thanks to @edc65.
s=>s.toUpperCase().replace(/\B.| |(AND|O[RF]|BY)\b/g,"")
Explanation
The code is self explanatory, I'll just explain the regex:
\B. // Matches any character (`.`), that's not the start of a word
| // Matches spaces
|(...)\b // Matches all the words that should be ignored
It removed all of these matched characers and uppercases the word
PHP, 92 bytes
First attempt at code golf.
foreach(explode(" ",str_replace(["AND","OR","BY","OF"],"",strtoupper($s)))as$x){echo$x[0];}
The variable $s is the phrase to be converted: $s = "United States of America".
Requires PHP 5.4 or above for short array syntax to work.
Python, 81 bytes
lambda s:''.join(c[0]for c in s.upper().split()if c not in'AND OF OR BY'.split())
Retina, 29 31 36 bytes
T`l`L
|(AND|OR|BY|OF)\b|\B.
Intended newline at the end.
Thanks to Martin Büttner for saving 5 bytes
T`l`L # Replace lowercase with uppercase
|(AND|OR|BY|OF)\b|\B. # Regex match, it doesn't matter if we match 'AND' in SHAND
# since the 'SH' will still become 'S' or am I missing something?
# Replace with nothing
JavaScript, 61 64 66 63 bytes
a=>a.toUpperCase().replace(/(AND|O[FR]|BY|(\w)\w+)( |$)/g,"$2")
It uses a Regular Expression to find get words that aren't from the list: and, or, of, by, and captures the first letter. It then capitalizes the resulting string of letters.
EDIT: 64 Bytes - Fixed for words start with of,or,by,and
EDIT: 66 Bytes - Fixed to check all words including last word.
PowerShell, 81 Bytes
(-join($args[0].Split(" ")|?{$_-notmatch"^(and|or|by|of)$"}|%{$_[0]})).ToUpper()
Explanation
Split on the spaces creating an array. Drop the offending members. Pull the first character and join together. Use ToUpper() on the resulting string.
Ruby, 45 43 bytes
->s{s.upcase.scan(/\b(?!AND|OR|OF|BY)\S/)*''}
This is a lambda function that accepts a string and returns a string. To call it, assign it to a variable and do f.call(input).
It uses the same approach as my Julia answer, namely convert to uppercase, get matches of the regular expression \b(?!AND|OR|OF|BY)\S, and join into a string.
Saved 2 bytes thanks to manatwork!
Perl, 32 bytes
say uc=~/\b(?!AND|OR|OF|BY)\S/g
+1 byte for the -n flag.
Algorithm stolen from @AlexA's Julia answer.
Mathematica, 132 117 bytes
ToUpperCase[First/@Characters@DeleteCases[StringDelete[StringSplit@#,"of"|"and"|"or"|"by",IgnoreCase->True],""]<>""]&
15 bytes saved thanks to @CatsAreFluffy.
Julia, 72 63 61 55 bytes
s->join(matchall(r"\b(?!AND|OR|OF|BY)\S",uppercase(s)))
This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.
We convert the string to uppercase, select each match of the regular expression \b(?!AND|OR|OF|BY)\S as an array, and join it into a string.
Saved 8 bytes thanks to Dennis!
CJam, 28 24 22 bytes
qeuS/"AOBONRYFD"4/z-:c
Try it online. Thanks to Sp3000 for pointing out a bug and suggesting a fix, and to Dennis for saving 4 6(!) bytes.
Explanation
qeuS/ e# Convert the input to uppercase and split on spaces
"AOBONRYFD"4/z e# Push the array of short words. See more below
- e# Remove each short word from the input words
:c e# Cast the remaining words to characters, which is a
e# shorter way of taking the first letter
Dennis suggested this trick for shortening the word list: Splitting AOBONRYFD into chunks of four, we get
AOBO
NRYF
D
Transposing columns into rows with the z operator, we get the proper words!
Pyth, 25 21 20 bytes
shM-crz1dc4."@YK½¼
Thanks to @Jakube for saving one byte!
Explanation
shM-crz1dc4."@YK½¼ # z = input
rz1 # convert input to uppercase
c d # split input on spaces
c4."@YK½¼ # create a list of the words from a packed string which shall be ignored
- # filter those words out
hM # only take the first letter of all words
s # join them into one string
The packed string becomes ANDBYOROF