6
\$\begingroup\$

Input:

The input will be a string of any characters.

Output:

the output will be an emojified (discord emojis) version of the input string. The way it will work is as so:

Letters: a-z (case insensitive) will become ':regional_indicator_lowercase letter here:'

e.g 'A' will become ':regional_indicator_a:'

Digits: any digits (0-9) will become the word version of the digit like so ':digit:'

e.g '0' will become ':zero:'

Whitespace: any whitespace character that's matched with the regex: /\s/ will become ':white_large_square:'

e.g ' ' will become ':white_large_square:'

Important notes:

  • The ':' surrounding the emoji's are necessary or else it won't display correctly in discord.

  • Each emoji needs to be separated by 1 space or else it won't display correctly in discord.

  • Trailing whitespace is allowed in the final output.

  • Any other characters that it comes across should not be in the final output and will be ignored.

Examples:

#1: Input:

this is a test string 0123456789

output:

:regional_indicator_t: :regional_indicator_h: :regional_indicator_i: :regional_indicator_s: :white_large_square: :regional_indicator_i: :regional_indicator_s: :white_large_square: :regional_indicator_a: :white_large_square: :regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :white_large_square: :regional_indicator_s: :regional_indicator_t: :regional_indicator_r: :regional_indicator_i: :regional_indicator_n: :regional_indicator_g: :white_large_square: :zero: :one: :two: :three: :four: :five: :six: :seven: :eight: :nine:

#2: input:

!?#$YAY 1234

output:

:regional_indicator_y: :regional_indicator_a: :regional_indicator_y: :white_large_square: :one: :two: :three: :four:

#3: input:

lots         of         spaces

output:

:regional_indicator_l: :regional_indicator_o: :regional_indicator_t: :regional_indicator_s: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :regional_indicator_o: :regional_indicator_f: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :white_large_square: :regional_indicator_s: :regional_indicator_p: :regional_indicator_a: :regional_indicator_c: :regional_indicator_e: :regional_indicator_s:

Examples as shown in discord:

discord examples

This is codegolf so the shortest code wins!

\$\endgroup\$
  • \$\begingroup\$ Yes, it should include all whitespace characters. I've added an example if you need a reference :) \$\endgroup\$ – eniallator May 24 '17 at 13:08
  • \$\begingroup\$ Please include the test cases as rendered by Discord :-) \$\endgroup\$ – John Dvorak May 24 '17 at 13:10
  • \$\begingroup\$ What should tab and newline be replaced with? \$\endgroup\$ – Shaggy May 24 '17 at 13:13
  • \$\begingroup\$ newline should also be the ':white_large_square:', i've editted the post to include all types of whitespace for that ':white_large_square:' :P \$\endgroup\$ – eniallator May 24 '17 at 13:20
  • 3
    \$\begingroup\$ @eniallator You should wait a few days before accepting an answer. Accepting too early discourages new answers. \$\endgroup\$ – mbomb007 May 24 '17 at 13:49

10 Answers 10

2
\$\begingroup\$

05AB1E, 75 bytes

žK… 	
©«Ãlvyaiy’‹¬_Âì_ÿ’}ydiy“¡×€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š“#è}®yåi’„¸_…Æ_ï©’}…:ÿ:ðJ

Try it online!

Explanation

žK…<space><tab><newline>©«Ã   # remove any character that isn't in [A-Za-z0-9<whitespace>]
lv                            # loop over each character converted to lowercase
yai                           # if it is a letter
   y’‹¬_Âì_ÿ’}                # interpolate into string "regional_indicator_<letter>"
ydi                           # if it is a digit
   y“¡×€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š“#è} # extract it from a list of digits as words
®yåi                          # if it is whitespace
    ’„¸_…Æ_ï©’}               # push the string "large_white_square"
…:ÿ:                          # interpolate in string ":<emoji>:" 
    ð                         # push a space
     J                        # join stack to one string
\$\endgroup\$
8
\$\begingroup\$

Python 2, 275 268 267 bytes

d=['']*256
d[48:58]='zero one two three four five six seven eight nine'.split()
d[9]=d[10]=d[32]='white_large_square'
d[65:90]=d[97:122]=map(lambda c:'regional_indicator_'+chr(c),range(97,122))
print' '.join((':%s:'%d[ord(c)])*(d[ord(c)]!='')for c in input()).strip()

It creates a large list of strings, iterates through the input string's characters and gets the string from the list using the character's ord value. Works for ascii characters only. Replaces space, tab and newline only; any other whitespace will be discarded.

It did leave whitespaces in front so I had to strip it in the end.

I also tried doing it with regex, turned out to be longer (286 bytes):

import re
print re.sub('([ \t\n])|([a-z])|(\d)',lambda m:':'+['white_large_square','regional_indicator_'+m.group(m.lastindex),'zero one two three four five six seven eight nine'.split()[(ord(m.group(m.lastindex))-68)%10]][m.lastindex-1]+': ',re.sub('[^a-z0-9 \t\n]','',input().lower()))
\$\endgroup\$
  • \$\begingroup\$ @mbomb007 seems to miss the first character \$\endgroup\$ – Skidsdev May 24 '17 at 13:35
  • \$\begingroup\$ How could I replace raw_input to input? The program will fail when you input numbers or anything for that matter. \$\endgroup\$ – Gábor Fekete May 24 '17 at 13:45
  • \$\begingroup\$ Ah alright, I see now. \$\endgroup\$ – Gábor Fekete May 24 '17 at 13:48
  • \$\begingroup\$ Okay, I made the changes, now it works for like in example #2. \$\endgroup\$ – Gábor Fekete May 24 '17 at 13:51
  • 1
    \$\begingroup\$ You can replace ':'+d[ord(c)]+':' with ':%s:'%d[ord(c)] to save 1 byte. \$\endgroup\$ – drobilc May 25 '17 at 10:39
4
\$\begingroup\$

C#, 296 273 bytes


Data

  • Input String i The string to be converted
  • Output String The input converted

Golfed

(string i)=>{var o="";foreach(var c in i){var k=c|32;var t=k>96&&k<123?"regional_indicator_"+(char)k:char.IsWhiteSpace(c)?"white_large_square":c>47&&c<58?new[]{"zero","one","two","three","four","five","six","seven","eight","nine"}[c-48]:"";o+=t!=""?$":{t}: ":t;}return o;};

Ungolfed

( string i ) => {
   var o = "";

   foreach( var c in i ) {
      var k = c | 32;
      var t = k > 96 && k < 123
         ? "regional_indicator_" + (char) k
         : char.IsWhiteSpace( c )
            ? "white_large_square"
            : c > 47 && c < 58
               ? new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }[ c - 48 ]
               : "";

      o += t != ""
         ? $":{ t }: "
         : t;
   }

   return o;
};

Ungolfed readable

// Receives a string to be converted
( string i ) => {

   // Initializes a var to store the output
   var o = "";

   // Cycle through each char in the string
   foreach( var c in i ) {
      // Creates a temp int to check for 'a' - 'z'
      var k = c | 32;

      // Creates a temp string that checks if the char is a lowercase letter
      var t = k > 96 && k < 123
         // If so, "builds" a Discord letter
         ? "regional_indicator_" + (char) k

         // Otherwise, checks if the char is a whitespace
         : char.IsWhiteSpace( c )
            // If so, "builds" a Discord white square
            ? "white_large_square"

            // Otherwise, checks if the char is a digit
            : c > 47 && c < 58
               // If so, gets the digit name
               ? new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }[ c - 48 ]

               // Otherwise, builds nothing
               : "";

      // Checks if the temp string is empty or not
      o += t != ""
         // If so, format it properly -- capsulate with ':' and add a space at the end
         ? $":{ t }: "

         // Otherwise, add the temp string, it's empty, so won't do anything
         : t;
   }

   // Return the converted string
   return o;
};

Full code

using System;
using System.Collections.Generic;

namespace Namespace {
   class Program {
      static void Main( String[] args ) {
         Func<String, String> f = ( string i ) => {
            var o = "";

            foreach( var c in i ) {
               var k = c | 32;
               var t = k > 96 && k < 123
                  ? "regional_indicator_" + (char) k
                  : char.IsWhiteSpace( c )
                     ? "white_large_square"
                     : c > 47 && c < 58
                        ? new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }[ c - 48 ]
                        : "";

               o += t != ""
                  ? $":{ t }: "
                  : t;
            }

            return o;
         };

         List<String>
            testCases = new List<String>() {
               "this is a test string 0123456789",
               "!?#$YAY 1234",
               "lots         of         spaces",
            };

         foreach( String testCase in testCases ) {
            Console.WriteLine( $" Input: {testCase}\nOutput: {f( testCase )}\n" );
         }

         Console.ReadLine();
      }
   }
}

Releases

  • v1.1 - -23 bytes - Implemented pinkfloydx33 suggestions.
  • v1.0 - 296 bytes - Initial solution.

Notes

  • None
\$\endgroup\$
  • \$\begingroup\$ Can you do bitwise fiddling on the sixth bit to convert to/from lowercase? Not sure if it would help in this scenario as I'm being too lazy to check. You shouldn't need new string[] {..} but rather just new[] {..} unless I'm missing something \$\endgroup\$ – pinkfloydx33 May 24 '17 at 20:06
  • \$\begingroup\$ @pinkfloydx33 the bitwise trick is nice to be implemented, but would increase the byte count -- that, or I don't know how to do it. \$\endgroup\$ – auhmaan May 25 '17 at 9:56
  • \$\begingroup\$ c|32 is what I was referring to but like I said I didn't check its implications as far as the problem statement or the overall byte count.. That aside you should still be able to recover some bytes by using implicit array typing and replacing new string[] with just new[] \$\endgroup\$ – pinkfloydx33 May 25 '17 at 10:00
  • \$\begingroup\$ I was trying to do it with the &, never crossed my mind using the |. \$\endgroup\$ – auhmaan May 25 '17 at 10:12
2
\$\begingroup\$

Retina, 142 bytes

T`w	¶p`_dll%%%_
.
:$&: 
[a-z]
regional_indicator_$&
%
white_large_square
0
zero
1
one
2
two
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine

Really wish there was an easier way to replace the digits.

Try it online!

Explanation

T`w ¶p`_dll%%%_

(Note the tab before the ) This is a transliteration stage with changes all letters to lowercase, all whitespace to %, and deletes every other character.

.
:$&: 

Next, every character gets wrapped in colons and followed by a space.

[a-z]
regional_indicator_$&

Next, letters are replaced with regional indicators.

%
white_large_square

And %, which were previously whitespace, are replaced with white_large_square.

0
zero
1
one
.......

And finally, the digits are replaced with their respective emojis.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 184 bytes

->x{[x.chars.map{|c|[?:,': ']*(c==$;?'white_large_square':c=~/\d/?%w[zero one two three four five six seven eight nine][c.to_i]:c=~/[A-Z]/i?'regional_indicator_%s'%c.downcase: 0)}]*''}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Consider making this a full program so you can use the -p flag and gsub instead of x.chars.map. Tips source \$\endgroup\$ – Value Ink May 24 '17 at 23:16
1
\$\begingroup\$

V, 141 bytes

Óá/:regional_indicator_&:
Óä/½':'.split('zero one two three four five six seven eight nine')[submatch(0)].':'
Óó/:white_large_square:
Ó::/: :

Try it online!

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

PHP>=7.0, 202 Bytes

for(;a&$c=$argn[$i++];)echo($e=[0,"",ctype_alpha($c)=>regional_indicator_.lcfirst($c),ctype_space($c)=>white_large_square,ctype_digit($c)=>strtolower(substr(IntlChar::charName("$c"),6))][1])?":$e: ":"";

It exists no Online Interpreter which contains IntlChar::charName

PHP, 211 Bytes

is the nearest way to solve this without IntlChar::charName

for(;a&$c=$argn[$i++];)echo($e=[0,"",ctype_alpha($c)=>regional_indicator_.lcfirst($c),ctype_space($c)=>white_large_square,ctype_digit($c)=>[zero,one,two,three,four,five,six,seven,eight,nine][$c]][1])?":$e: ":"";

Try it online!

PHP>=7.0, 220 Bytes

Regex Based

<?=preg_replace_callback("#(\pL)|(\d)|(\s)|.#",function($t){$r=!$t[1]?!$t[2]?!$t[3]?"":white_large_square:strtolower(substr(IntlChar::charName("$t[2]"),6)):regional_indicator_.lcfirst($t[1]);return$r?":$r: ":"";},$argn);

It exists no Online Interpreter which contains IntlChar::charName

PHP, 229 Bytes

is the nearest way to solve this without IntlChar::charName

<?=preg_replace_callback("#(\pL)|(\d)|(\s)|.#",function($t){$r=!$t[1]?!$t[2]?!$t[3]?"":white_large_square:[zero,one,two,three,four,five,six,seven,eight,nine][$t[2]]:regional_indicator_.lcfirst($t[1]);return$r?":$r: ":"";},$argn);

Try it online!

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

Python, 212 205 bytes

lambda s:''.join('/'<c<':'and':'+'zero one two three four five six seven eight nine'.split()[int(c)]+': 'or'`'<c<'{'and':regional_indicator_'+c+': 'or':white_large_square: '*(c in' 	\n')for c in s.lower())

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice, you beat me! This also works in Python 2! \$\endgroup\$ – Gábor Fekete May 25 '17 at 8:40
0
\$\begingroup\$

PowerShell, 200 197 Bytes

-3 because the [A-Za-z] regex isn't needed in the default case-insensitive regex matches.

$a=switch -R ([char[]]"$args"){
"[0-9]"{(-split'zero one two three four five six seven eight nine')["$_"]}
"[a-z]"{"regional_indicator_"+"$_"|% *l*r}
"\s"{"white_large_square"}};":$($a-join': :'):"

Would like to find:

a way around using $a - wrapping a switch statement in brackets causes it to not work at all for some reason, and any other method is longer than just using a variable.

Try it online! - input whatever you want and get the result easily.

Several People Are Typing

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

JavaScript (ES6), 197 199 bytes

Fixed underscores not being handled properly thanks to @Neil.

s=>s.toLowerCase().replace(/_|(\d)|(\w)|(\s)|./g,(c,d,a,w)=>a||d||w?`:${a?"regional_indicator_"+c:d?"zero one two three four five six seven eight nine".split` `[c]:"white_large_square"}: `:"").trim()

Cleaned up

s => s.toLowerCase().replace( /_|(\d)|(\w)|(\s)|./g, (c, d, a, w) => {
    if(a || d || w) {
        let out;
        if (a) out = "regional_indicator_" + c;
        else if (d) out = "zero one two three four five six seven eight nine".split(" ")[c];
        else out = "white_large_square";
        return `:${out}: `;
    }
    else {
        return "";
    }
}).trim()

Test Snippet

f=
s=>s.toLowerCase().replace(/_|(\d)|(\w)|(\s)|./g,(c,d,a,w)=>a||d||w?`:${a?"regional_indicator_"+c:d?"zero one two three four five six seven eight nine".split` `[c]:"white_large_square"}: `:"").trim()
<input id="I" type="text" size="50" oninput="O.innerHTML=f(I.value)">
<pre id="O" style="width:90vw;white-space:pre-wrap">

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice but doesn't work for _. Hopefully the fix won't cost more than a couple of bytes. \$\endgroup\$ – Neil May 26 '17 at 10:03
  • \$\begingroup\$ I'm pretty sure you can do /_|([\d\w\s])|./g as your regex. \$\endgroup\$ – Kritixi Lithos May 27 '17 at 4:39
  • \$\begingroup\$ I split it into three separate groups so that in the replace function, I have d, a, and w for the three groups. Then I could just check which of those parameters existed to determine digit vs letter vs whitespace. \$\endgroup\$ – Justin Mariner May 27 '17 at 4:43

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.