19
\$\begingroup\$

I've been working on this iOS app recently and I encountered a (rather easily solved) problem: How do I determine if my string is comprised only of emojis?

Well, that's your job today!

Input

A string. If the presence of unicode characters is problematic for you then you may use a different reasonable format. Please state what you do if you do something different from a regular string for taking input.

Output

Print or return a truthy value if the input string contains only emojis, and a falsey value if it contains any characters other than emojis.

Wait a sec... what's an emoji? ๐Ÿ˜…

An emoji is a character that falls in these ranges:

0x1F600...0x1F64F  // Emoticons
0x1F300...0x1F5FF  // Misc Symbols and Pictographs
0x1F680...0x1F6FF  // Transport and Map
0x2600...0x26FF    // Misc symbols
0x2700...0x27BF    // Dingbats
0xFE00...0xFE0F    // Variation Selectors

(I think)

This is ๐Ÿ”น๐Ÿ”น, so shortest code in bytes winsโ•

Test cases

"Is it an emoji? ๐Ÿค”" -> False
"๐Ÿ”นcode-golf๐Ÿ”น" -> False
"๐Ÿ˜ฎ ๐Ÿ˜ฅ ๐Ÿ˜จ ๐Ÿ˜ฉ ๐Ÿ˜ช" -> False
"I have no fancy emojis :(" -> False

"๐Ÿ˜Ž" -> True
"๐Ÿ˜Š๐Ÿ˜˜" -> True

For you poor souls who cannot see emojis, here is an image of the test cases.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Perhaps include a version (maybe an image) for those who can't see emojis? ;-) \$\endgroup\$ Commented Dec 7, 2016 at 0:10
  • \$\begingroup\$ @ETHproductions, do you suggest I link to a photo of the test cases or something else? \$\endgroup\$
    – Daniel
    Commented Dec 7, 2016 at 0:12
  • \$\begingroup\$ Well, there are emojis spread throughout the whole question, but I guess the only really important ones are in the test cases, so an image of just the test cases will work. \$\endgroup\$ Commented Dec 7, 2016 at 0:14
  • \$\begingroup\$ @ETHproductions, I added a link to a photo of the test cases \$\endgroup\$
    – Daniel
    Commented Dec 7, 2016 at 0:22
  • \$\begingroup\$ Can input be a numeric array of Unicode code points? Also, what range of characters/numbers can the input contain? \$\endgroup\$
    – Luis Mendo
    Commented Dec 7, 2016 at 0:24

7 Answers 7

13
\$\begingroup\$

Emojicode, 179 bytes

๐Ÿ‡a๐Ÿจ๐Ÿš๐Ÿš‚โžก๐Ÿš‚๐Ÿ”‚n๐Ÿกa๐Ÿ‡๐ŸŠ๐ŸŽ‰๐ŸŽ‰โ—€n 9728๐ŸŽŠโ–ถn 10175โ—€n 65024๐ŸŽ‰๐ŸŽ‰๐ŸŽŠโ–ถn 65039โ—€n 127744๐ŸŽŠโ–ถn 128591โ—€n 128640โ–ถn 128767๐Ÿ‡๐ŸŽ0๐Ÿ‰๐Ÿ‰๐ŸŽ1๐Ÿ‰

A callable that takes a list of integers representing the codepoints and returns 1 or 0.

Try it online!

Emojicode, 187 bytes

๐Ÿ‡a๐Ÿ”กโžก๐Ÿ‘Œ๐Ÿ”‚i๐Ÿกa๐Ÿ‡๐Ÿฆn๐Ÿš‚i๐ŸŠ๐ŸŽ‰๐ŸŽ‰โ—€n 9728๐ŸŽŠโ–ถn 10175โ—€n 65024๐ŸŽ‰๐ŸŽ‰๐ŸŽŠโ–ถn 65039โ—€n 127744๐ŸŽŠโ–ถn 128591โ—€n 128640โ–ถn 128767๐Ÿ‡๐ŸŽ๐Ÿ‘Ž๐Ÿ‰๐Ÿ‰๐ŸŽ๐Ÿ‘๐Ÿ‰

A callable that takes a string and returns a boolean.

Try it online!

Ungolfed and explained

๐Ÿ‘ด A callable that takes a ๐Ÿ”ก called input and returns a ๐Ÿ‘Œ
๐Ÿ‡ input ๐Ÿ”ก โžก ๐Ÿ‘Œ
 ๐Ÿ‘ด For each character in input
 ๐Ÿ”‚ character ๐Ÿก input ๐Ÿ‡
  ๐Ÿ‘ด Create a frozen called n containing the unicode codepoint of character
  ๐Ÿฆ n ๐Ÿš‚ character
  ๐Ÿ‘ด If the value is in one of specific ranges, return ๐Ÿ‘Ž (if the current character is not an emoji)
  ๐ŸŠ๐ŸŽ‰๐ŸŽ‰โ—€n 9728๐ŸŽŠโ–ถn 10175โ—€n 65024๐ŸŽ‰๐ŸŽ‰๐ŸŽŠโ–ถn 65039โ—€n 127744๐ŸŽŠโ–ถn 128591โ—€n 128640โ–ถn 128767๐Ÿ‡
   ๐ŸŽ ๐Ÿ‘Ž
  ๐Ÿ‰
 ๐Ÿ‰
 ๐Ÿ‘ด Since we're here, all characters are an emoji, so return ๐Ÿ‘
 ๐ŸŽ๐Ÿ‘
๐Ÿ‰

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ruby, 61 56+1 = 62 57 47 bytes

Uses the -n flag and takes input from STDIN. Prints 0 for truthy and nil for falsy.

-5 bytes because for some reason I initially thought the regex solution would not work???

-10 bytes from NieDzejkob for noticing a code point overlap that I missed :o

p$_=~/^[๐ŸŒ€-๐Ÿ™๐Ÿš€-๐Ÿ›ฟโ˜€-โžฟ๏ธ€-๏ธ€๏ธ]*$/

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 47 bytes if you merge two ranges in the regex. \$\endgroup\$
    – Maya
    Commented Dec 11, 2017 at 19:40
  • 1
    \$\begingroup\$ That's 47 isn't it? \$\endgroup\$ Commented Jan 22, 2018 at 9:24
  • \$\begingroup\$ Yeah I forgot to update the score after getting the suggestion from @NieDzejkob \$\endgroup\$
    – Value Ink
    Commented Jan 25, 2018 at 9:25
3
\$\begingroup\$

JavaScript (V8), 39 bytes

Since the proposal for Unicode property escapes is now part of V8, this can also be addressed without manually specifying ranges. Granted, this comes years after the original question was asked, but I think it is an interesting addition none the less.

a=>/^\p{Emoji_Presentation}*$/u.test(a)

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Amazing, TIL about Unicode property escapes. Thanks! \$\endgroup\$
    – blitzmann
    Commented Aug 24, 2022 at 20:44
1
\$\begingroup\$

Python, 87 bytes

lambda x:re.match('^[๐Ÿ˜€-๐Ÿ™๐ŸŒ€-๐Ÿ—ฟ๐Ÿš€-๐Ÿ›ฟโ˜€-โžฟ๏ธ€-๏ธ€๏ธ]*$',x)and 1
import re

Try it online!

Returns 1 as truthy value and None as falsy value.

\$\endgroup\$
3
  • \$\begingroup\$ I "borrowed" your regex, hope you don't mind ;) \$\endgroup\$
    – hyper-neutrino
    Commented Aug 15, 2017 at 18:29
  • \$\begingroup\$ Since I told Value Ink about it, I'll tell you too - the first two groups in the regex can be merged because of adjacent codepoints: ๐ŸŒ€-๐Ÿ™ \$\endgroup\$
    – Maya
    Commented Dec 11, 2017 at 19:42
  • \$\begingroup\$ -12 bytes by using a better regex and using re.sub instead of re.match Try it online! \$\endgroup\$
    – Jakque
    Commented Jun 28, 2021 at 13:02
1
\$\begingroup\$

Proton, 54 bytes

map(/[๐ŸŒ€-๐Ÿ™๐Ÿš€-๐Ÿ›ฟโ˜€-โžฟ๏ธ€-๏ธ€๏ธ]/.match)+all

Try it online!

-9 bytes (3 chars) thanks to NieDzejkob

"Borrowing" Uriel's regex :P This is shorter thanks to Proton's obscure features and regex literals

\$\endgroup\$
3
  • \$\begingroup\$ As I told Value Ink, the first two groups in the regex can be merged because of adjacent codepoints: ๐ŸŒ€-๐Ÿ™ \$\endgroup\$
    – Maya
    Commented Dec 11, 2017 at 19:42
  • \$\begingroup\$ @NieDzejkob How exactly? It gives me errors: tio.run/##KyjKL8nP@59m@z83sUBDP/rD/BkNuh/mT9//Yf4sEGP2/… \$\endgroup\$
    – hyper-neutrino
    Commented Dec 11, 2017 at 20:13
  • \$\begingroup\$ like this \$\endgroup\$
    – Maya
    Commented Dec 12, 2017 at 6:27
1
\$\begingroup\$

QuadR, 47 bytes

''โ‰กโต
[๐ŸŒ€-๐Ÿ™๐Ÿš€-๐Ÿ›ฟโ˜€-โžฟ๏ธ€-๏ธ€๏ธ]

Try it online!

Isโ€ฆ

โ€ƒ''โ‰กโตโ€ƒan empty string identical to the result whenโ€ฆ

โ€ƒ[๐ŸŒ€-๐Ÿ™๐Ÿš€-๐Ÿ›ฟโ˜€-โžฟ๏ธ€-๏ธ€๏ธ]โ€ƒall emojis areโ€ฆ

โ€ƒโ€ƒreplaced with nothing

?

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

JavaScript, 83 61 bytes

-22 bytes thanks to @Shaggy

s=>(s.match(/\ud83d[\ude00-\ude4f]/g)||"").length==s.length/2

Demo

f=s=>(s.match(/\ud83d[\ude00-\ude4f]/g)||"").length==s.length/2

document.writeln(f("๐Ÿ”นcode-golf๐Ÿ”น")) //false
document.writeln(f("๐Ÿ˜ฎ ๐Ÿ˜ฅ ๐Ÿ˜จ ๐Ÿ˜ฉ ๐Ÿ˜ช")) //false
document.writeln(f("I have no fancy emojis :(")) //false
document.writeln(f("๐Ÿ˜Ž")) //true
document.writeln(f("๐Ÿ˜Š๐Ÿ˜˜")) //true

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 61 bytes: s=>(s.match(/\ud83d[\ude00-\ude4f]/g)||"").length==s.length/2 \$\endgroup\$
    – Shaggy
    Commented Aug 15, 2017 at 15:46
  • \$\begingroup\$ Uh, how does this work? The regex doesn't specify the codepoints of any emoji... \$\endgroup\$
    – Maya
    Commented Dec 11, 2017 at 19:40
  • \$\begingroup\$ @NieDzejkob I believe JS uses UTF-16 for Unicode, so I guess these are the UTF-16 byte representations of those codepoints. The answer is incomplete though, it fails for ๐Ÿ›‚๐Ÿš€, โ™”โ˜บโ˜ , ๐Ÿ๐ŸŽ…๐ŸŒ etc., all of which should be truthy. It seems to check only for one range out of the six that OP mentions. \$\endgroup\$
    – Sundar R
    Commented Jul 10, 2018 at 20:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.