| Bytes | Lang | Time | Link |
|---|---|---|---|
| 027 | R | 240622T190626Z | int 21h |
| 005 | Thunno 2 b | 230427T094133Z | The Thon |
| 018 | J | 230426T205522Z | south |
| 008 | Vyxal | 230426T125024Z | emirps |
| 029 | Lua | 230425T233951Z | bluswimm |
| 028 | Arturo | 230425T224710Z | chunes |
| 012 | TeaScript | 151224T000932Z | Downgoat |
| 006 | 05AB1E | 180601T120407Z | Kevin Cr |
| 041 | Python | 151224T163159Z | Zenadix |
| 043 | Python 2 | 190528T000129Z | Chas Bro |
| 048 | VB6 | 151224T001434Z | Toothbru |
| 051 | PHP | 190523T192052Z | 640KB |
| 017 | Perl 6 | 190521T054942Z | bb94 |
| 098 | Kotlin | 190522T214641Z | Xvolks |
| 009 | Japt | 190522T181208Z | Shaggy |
| 009 | Stax | 190522T172013Z | recursiv |
| 083 | JavaScriptES6 | 190522T171140Z | Naruyoko |
| 073 | C++ | 190522T160627Z | HatsuPoi |
| 028 | Java | 160212T183904Z | hyper-ne |
| 029 | PowerShell | 190517T143543Z | Veskah |
| 018 | Perl 5 p | 180601T162239Z | Xcali |
| 1537 | AWK | 180601T050009Z | Jonah |
| 089 | C# 4 | 151226T135907Z | Farhan A |
| 188 | C# | 151228T175007Z | Goose |
| 025 | bash/zsh/ksh | 151224T001051Z | Aaron Da |
| 046 | Mathematica 10.1 | 151223T233452Z | LegionMa |
| 016 | grep | 151225T033900Z | mikeserv |
| 111 | C | 151224T063724Z | dogAwake |
| 116 | Swift 2 | 151224T084247Z | Jojodmo |
| 058 | Python | 151224T160951Z | Wayne We |
| 019 | IA32 machine code | 151224T131017Z | anatolyg |
| 028 | Ruby | 151224T101859Z | CocoaBea |
| 017 | Gema | 151224T095149Z | manatwor |
| 039 | SpecBAS | 151224T091548Z | Brian |
| 166 | Ocaml | 151224T044902Z | Moshe Ka |
| 016 | Seriously | 151224T023812Z | quintopi |
| 061 | Haskell | 151224T022536Z | lynn |
| 018 | MATL | 151224T013137Z | Luis Men |
| nan | BotEngine | 151224T005932Z | SuperJed |
| 053 | Java | 151224T001150Z | SuperJed |
| 039 | k4 | 151224T001034Z | Aaron Da |
| 012 | Pyth | 151223T230725Z | Maltysen |
| 013 | Retina | 151223T230713Z | NinjaBea |
| 026 | JavaScript ES6 | 151223T231022Z | edc65 |
R, 27 bytes
\(S)grep("^[A-Z][a-z]+$",S)
The shortest solution contains the same regex pattern as in other answers.
A longer but more interesting solution:
R, 51 bytes
\(S,V=utf8ToInt(S))
all((V+32*!seq(V)-1)%in%97:122)
To the vector of the character codes is added another vector of the same length with 32 being first element: [32, 0, 0,...]. This is made with help of 32*!seq(V)-1 (see also Tips for golfing in R). This way every charcode must be in range of a-z codes.
Thunno 2 b, 5 bytes
Ịḟ=×ḣ
Port of Kevin Cruijssen's 05AB1E answer.
Explanation
Ịḟ=×ḣ # Implicit input
Ị # Remove non-alphabets from the input
ḟ # Convert this string to title case
= # Check whether it's still equal to the input
× # Repeat the input this many times
# (i.e. input if true, empty string otherwise)
ḣ # Remove the first character from this string
# The b flag boolifies the resulting string,
# checking that it is not empty
# Implicit output
Screenshots
(click to enlarge)
J, 18 bytes
'[A-Z][a-z]+'&rxeq
'[A-Z][a-z]+'&rxeq
rxeq NB. does the regex fully describe the string
& NB. bind the left argument to
'[A-Z][a-z]+' NB. the Official™ regex
Vyxal, 8 bytes
Lċ?₍ǐǍ≈∧
Try it Online (with test cases)!
Potentially golfable. I swear there was a builtin for len(a)>1...
Assumes input is nonempty.
L # Length of input
ċ # is not 1
? # Push the input,
₍ # parallel apply...
ǐ # title case and
Ǎ # keep only alphabetic chars
≈ # is equal?
∧ # bitwise AND the length and ^
TeaScript, 12 bytes
xO`A-Z][a-z`
Abuses the O function.
Explanation
The O function makes this:
x O `A-Z][a-z`
x.O(/^[A-Z][a-z]+$/)
Then, the O function checks if the regex matches x.
Alternatively, a TeaScript 3 answer at 7 bytes:
xO/\A\a
05AB1E, 11 7 6 bytes
á™QצĀ
-4 bytes thanks to @Grimy.
Try it online or verify all test cases.
Explanation:
á # Only leave the letters of the (implicit) input
# i.e. "Adnan" → "Adnan"
# i.e. "A" → "A"
# i.e. "aDNAN" → "aDNAN"
# i.e. "Adnan123" → "Adnan"
™ # Titlecase it
# i.e. "Adnan" → "Adnan"
# i.e. "Adnan" → "A"
# i.e. "aDNAN" → "Adnan"
Q # Check if it's still equal to the (implicit) input
# i.e. "Adnan" and "Adnan" → 1 (truthy)
# i.e. "A" and "A" → 1 (truthy)
# i.e. "aDNAN" and "Adnan" → 0 (falsey)
# i.e. "Adnan123" and "Adnan" → 0 (falsey)
× # Repeat the (implicit) input that many times
# i.e. "Adnan" and 1 → "Adnan"
# i.e. "A" and 1 → "A"
# i.e. "aDNAN" and 0 → ""
# i.e. "Adnan123" and 0 → ""
¦Ā # Remove the first character, and check that it's non-empty
# i.e. "Adnan" → "dnan" → 1 (truthy)
# i.e. "A" → "" → 0 (falsey)
# i.e. "" → "" → 0 (falsey)
# (which is output implicitly as result)
Python, 50 45 43 41 bytes
lambda s:s.isalpha()*s.istitle()*len(s)>1
Returns True if it's an official name or False if it's not.
VB6, 48 bytes
Function f(i):f=i Like"[A-Z][a-z]+":End Function
PHP, 51 bytes
<?=ucfirst($s=$argn)==$s&ctype_lower(substr($s,1));
No regex. Input is STDIN, output to STDOUT is '1' for truthy and '0' for falsy.
$ echo Adnan|php -F p.php
1
$ echo adnan|php -F p.php
0
$ echo AdnaN|php -F p.php
0
$ echo Adnan123|php -F p.php
0
$ echo Adnan Adnan|php -F p.php
0
$ echo A|php -F p.php
0
$ echo Mary-Ann|php -F p.php
0
$ echo Mary-ann|php -F p.php
0
Perl 6, 17 bytes
{/^<:Lu><:Ll>+$/}
Returns a Match object if this is an official name and Nil otherwise.
Kotlin 98 bytes
x.length>1 && x.mapIndexed{i,c->if(i>0)c in 'a'..'z' else c in 'A'..'Z'}.sumBy{if(it) 0 else 1}==0
Way less boring than shorter:
x.matches(Regex("[A-Z][a-z]+"))
JavaScript(ES6), 83 bytes
s=>s[0].match(/[A-Z]/)&&s.substr(1).match(/[a-z]/)&&!s.match(/[^A-Z]/i)&&s.length>1
C++, 73 bytes
Good old #define
#include<regex>
#define F(s)std::regex_match(s,std::regex("[A-Z][a-z]+"))
Code to test :
std::cout << F("Adnan") << '\n';
std::cout << F("adnan") << '\n';
std::cout << F("AdnaN") << '\n';
std::cout << F("Adnan123") << '\n';
std::cout << F("Adnan Adnan") << '\n';
std::cout << F("A") << '\n';
std::cout << F("Mary-Ann") << '\n';
Java, 28 bytes
n->n.matches("[A-Z][a-z]+")
Uses regex to make sure the string consists of an uppercase character followed by at least one lowercase character.
-1 bytes thanks to Benjamin Urquhart
PowerShell, 29 bytes
"$args"-cmatch'^[A-Z][a-z]+$'
Does the same regex trick everyone else is using. Has to use case-sensitive match to properly do it at the cost of a byte.
AWK, 15 bytes or 37 bytes (rule dependent)
Flexibly truthy
If you're allowed to define "truthy" as "non-empty string", then:
/^[A-Z][a-z]+$/
Strict truthy
If it needs to be something more consistent like 1 and 0, then:
{print match($0,/^[A-Z][a-z]+$/)?1:0}
C# 4, 89 bytes
My first attempt at Code Golf. Here it comes:
bool o(string i){return System.Text.RegularExpressions.Regex.IsMatch(i,"^[A-Z][a-z]+$");}
See it in action at Dot Net Fiddle.
C#, 188 bytes
Regular expressions would have been the right way to tackle this, but here's an attempt without it.
bool O(string s){for(int i=1;i<s.Length;i++){if(char.IsUpper(s[i])){return false;}}if(char.IsUpper(s[0])&&s.All(Char.IsLetter)&&!s.Contains(" ")&& s.Length > 1){return true;}return false;}
Longhand
static bool O(string s)
{
for (int i = 1; i < s.Length; i++)
{
if (char.IsUpper(s[i]) )
{
return false;
}
}
if (char.IsUpper(s[0]) && s.All(Char.IsLetter) && !s.Contains(" ") && s.Length > 1)
{
return true;
}
return false;
}
Would love advice on how to make the lowercase check shorter, perhaps without the loop. I just started learning the language, and used this as practice, figured I'd share my result anyway.
bash/zsh/ksh, 25 bytes
[[ $1 =~ ^[A-Z][a-z]+$ ]]
To actually use this, make a file with it as the only line and make the file executable; executable files not recognized as a known binary type are treated as shell scripts (for /bin/sh specifically).
$ printf '[[ $1 =~ ^[A-Z][a-z]+$ ]]' >f
$ chmod +x f
$ wc -c f
25 f
$ for x in 'Adnan' 'adnan' 'AdnaN' 'Adnan123' 'Adnan Adnan' 'A' 'Mary-Ann'; do f "$x" && echo 1 || echo 0; done
1
0
0
0
0
0
0
$
Mathematica 10.1, 46 bytes
LetterQ@#&&#==ToCamelCase@#&&StringLength@#>1&
Uses one less byte than the standard regex solution. It does three checks. LetterQ@# ensures that the string is entirely composed of letters, and StringLength@#>1 invalidates single-letter strings. #==ToCamelCase@# makes less sense, however. ToCamelCase is an undocumented function I found that takes an input string AndOutputsItLikeThis. Since there is only one word, it will capitalize the first letter, so we check if the string is equal to that.
grep, 16 bytes
This is the pattern:
[A-Z][a-z]+
If you use the -E and -x and -c switches grep will print a count of matching input lines. So if you give it one line you get a 1 or a 0. I think that's how this place works.
The pattern is 11 chars, the whole command line is 23. I've seen people use sed scripts without the command so I don't know what is what. But, it reads stdin, and so you can just type at it. Here's echo:
for a in Adnan adnan Ad\ nan
do echo "$a" | grep -cxE \[A-Z]\[a-z]+
done
1
0
0
C, 129 122 121 111 bytes
main(c,b,d){b=d=0;while((c=getchar())>13)b|=b|=!b&&c>90|c<65?1:2&&d++&&c<97|c>122?4:2;printf("%d\n",b<3&&d>1);}
main(c,b,d)
{
b=d=0;
while((c=getchar())>13)
{
// Twiddle bits, 1<<0 for first character and 1<<3 for subsequent
b|=!b&&c>90|c<65?1:2; // check first character is valid
b|=d++&&c<97|c>122?4:2; // check later characters are valid
}
// If all OK b == 2, if either of above are wrong, b >= 3 due to
// extra bits. Also, d should be > 1 for name length to be valid.
printf("%d\n",b<3&&d>1);
}
Swift 2, 116 bytes
Regex is so verbose in Swift that doing this is much shorter
func e(s:String)->Int{var c=0;for k in s.utf8{if(c==0 ?k<65||k>90:k<97||k>122){return 0};c++};return s.utf8.count-1}
This will return 0 or -1 (in the case of no input) for non-official names, and a number > 0 (which is equal to the length of the string - 1) if the name is official
Ungolfed
func e(s: String) -> Int{
var c = 0
for k in s.utf8{
if(c == 0 ? k < 65 || k > 90 : k < 97 || k > 122){
return 0
}
c++
}
return s.utf8.count - 1
}
Python, 59 58 bytes
I'm sure there's no real way to beat the Retina version, since this is basically just that within Python. But I think this is my first submission ;)
import re,sys;print(re.match('[A-Z][a-z]+$',sys.argv[1]))
It's a very odd truthy value:
(test2)wayne@arglefraster ~/programming/inactive/golf/67554
⚘ python golf.py AdNan $? 148 %# 3 10:06:36
None
(test2)wayne@arglefraster ~/programming/inactive/golf/67554
⚘ python golf.py Adnan %# 3 10:06:40
<_sre.SRE_Match object at 0x7feefea7f440>
(test2)wayne@arglefraster ~/programming/inactive/golf/67554
⚘ python golf.py "Adnan Banana" %# 3 10:06:47
None
(And it does require "" around strings with spaces in it, if passed via the shell)
IA-32 machine code, 19 bytes
A function that receives the pointer to a null-terminating string in ecx and returns 0 or 1 in eax (according to the fastcall convention).
Hexdump of the code:
6a 20 58 32 01 74 0a 41 2c 61 3c 1a b0 00 72 f3 c3 40 c3
In assembly language:
push 32;
pop eax;
myloop:
xor al, [ecx];
jz yes;
inc ecx;
sub al, 'a';
cmp al, 26;
mov al, 0;
jb myloop;
ret;
yes:
inc eax;
ret;
The first byte of the input name has its 5th bit flipped (xor with 32) to convert it from capital case to small case. This loads 32 into eax, using 3 bytes of code:
push 32;
pop eax;
To check whether the byte is a small letter:
sub al, 'a';
cmp al, 26;
jb myloop;
If not, this code falls through. To return 0 in this case, it puts 0 in al before doing the conditional jump:
sub al, 'a';
cmp al, 26;
mov al, 0;
jb myloop;
The 0 in al also serves as a xor-mask (or absence of it) for the following bytes of the input name.
A successful exit is when it encounters a zero byte, which stays zero after the xor:
xor al, [ecx];
jz yes;
It assumes that the input name is not empty. I guess it's a reasonable assumption about a name (not an arbitrary string)!
Ruby, 28 bytes
p (gets=~/^[A-Z][a-z]+$/)!=p
-2 bytes( thanks to manatwork )
Gema, 17 characters
\B<K1><J>\E=1
*=0
Sample run:
bash-4.3$ echo -n 'Adnan' | gema '\B<K1><J>\E=1;*=0'
1
bash-4.3$ echo -n 'adnan' | gema '\B<K1><J>\E=1;*=0'
0
bash-4.3$ echo -n 'Adnan123' | gema '\B<K1><J>\E=1;*=0'
0
SpecBAS - 39 bytes
SpecBAS handles regular expressions through the MATCH command. Output is 0 for false and 1 if true.
1 input n$: ?MATCH("^[A-Z][a-z]+$",n$)
Ocaml, 231 216 197 166 bytes
let f n=let l=String.length n in if l=1 then 0 else let rec e=function 0->1|i->match n.[i] with('a'..'z')->e(i - 1)|_->0 in match n.[0]with('A'..'Z')->e(l - 1)|_->0;;
Example usage:
# f "Adnan";;
- : int = 1
# f "adnan";;
- : int = 0
# f "AdnaN";;
- : int = 0
# f "Adnan123";;
- : int = 0
# f "Adnan Adnan";;
- : int = 0
# f "A";;
- : int = 0
# f "Mary-Ann";;
- : int = 0
Ungolfed (with real function names):
let is_name name =
let len = String.length name
in if len = 1 then 0 else
let rec explode_lower = function
| 0 -> 1
| i ->
match name.[i] with
| ('a'..'z') -> explode_lower (i - 1)
| _ -> 0
in match name.[0] with
| ('A'..'Z') -> explode_lower (len - 1)
| _ -> 0;;
Seriously, 16 bytes
ú4,nÿ=)l1<)ù-Y&&
Hex Dump:
a3342c6e983d296c313c29972d592626
Seriously does not have regex support yet, so the best we can do is:
4,n Push 4 copies of input
ÿ= Check that it's equal to itself converted to titlecase
) Put the boolean on the bottom
l1< Check that it's longer than 1 character
) Put the boolean on the bottom
ù Convert it to lowercase.
ú -Y Check that removing the lowercase alphabet empties it
&& And all the booleans together
Haskell, 61 bytes
f(h:t@(_:_))=elem h['A'..'Z']&&all(`elem`['a'..'z'])t
f _=1<0
MATL, 18 bytes
The current version (4.0.0) of the language is used.
This applies the same regular expression as NinjaBearMonkey's answer:
j'^[A-Z][a-z]+$'XX
The output is the string (which is truthy) if it's an official name, and nothing (which is falsy) if it's not.
Examples
>> matl
> j'^[A-Z][a-z]+$'XX
>
> December
December
>>
>> matl
> j'^[A-Z][a-z]+$'XX
>
> ASCII
>>
BotEngine, 203 180 29x6=174
v ABCDEFGHIJKLMNOPQRSTUVWXYZ
>ISSSSSSSSSSSSSSSSSSSSSSSSSSF
v <<<<<<<<<<<<<<<<<<<<<<<<<<
Tabcdefghijklmnopqrstuvwxyz
> SSSSSSSSSSSSSSSSSSSSSSSSSSF
^E<<<<<<<<<<<<<<<<<<<<<<<<<<
I should really add builtins for identifying uppercase and lowercase letters. That would be much more concise than checking each letter individually.
Rough translation:
for a of input enqueue a
if ABCDEFGHIJKLMNOPQRSTUVWXYZ contains first
remove first
while abcdefghijklmnopqrstuvwxyz contains first
remove first
if empty
yield TRUE exit
else
yield FALSE exit
else
yield FALSE exit
Java, 53 bytes
boolean b(String a){return a.matches("[A-Z][a-z]+");}
k4, 39 bytes
{((*x)in .Q.A)&(&/(1_,/x)in .Q.a)&1<#x}
First char is upper, all others are lower, count greater than one.
E.g.:
{((*x)in .Q.A)&(&/(1_,/x)in .Q.a)&1<#x}'("Adnan";"adnan";"AdnaN";"Adnan123";"Adnan Adnan";"A";"Mary-Ann")
1000000b
Pyth, 16 13 12 bytes
Thanks to @Thomas Kwa for reminding me about titlecase.
&qzr@GrzZ3tz
& Boolean and operator
qz Equality test on input
r 3 Titlecase operator
@G Setwise intersection with the alphabet
rzZ Input to lowercase
tz All but the first character of the input
Retina, 13 bytes
^[A-Z][a-z]+$
Try it online | Test suite (The output 0 means none of the strings matched, which is expected.)
When Retina is only provided with a single line of code, it outputs the number of times the expression matched the input string, so it will output 1 (truthy) if it matches and therefore is an official name and 0 (falsy) if it's not.
Breakdown
^ The beginning of the string
[A-Z] One uppercase letter
[a-z]+ One or more lowercase letters
$ The end of the string
JavaScript (ES6), 26
n=>/^[A-Z][a-z]+$/.test(n)
By: Edcsixtyfive
f=n=>/^[A-Z][a-z]+$/.test(n)
console.log=x=>O.textContent+=x+'\n'
;['Adnan','adnan','AdnaN','Adnan123','Adnan Adnan','A','Mary-Ann']
.forEach(t=>console.log(t+' '+f(t)))
<pre id=O></pre>

