12
\$\begingroup\$

This is my friend Thomas. He is half tree, half emoticon.

|    |
| :D |
|    |

He is lonely. Let's make him some friends!


Given a text-based emoticon as input (e.g. ಠ_ಠ, :P, >_>, not 😀, 🤓, or 🐦), output the corresponding treemote.

A treemote's length is how many characters it is lon (basically most builtin length functions for strings). So ಠ_ಠ has length 3.

The syntax for a treemote of length n is as follows:

|< 2+n spaces>| * ceil(n/2)
| <emote> | (note the spaces)
|< 2+n spaces>| * ceil(n/2)

So any treemote of length 3 would look like:

|     |
|     |
| ಠ_ಠ |
|     |
|     |

He has ceil(n/2) newline separated trunk segments on either side, each with 2 + n spaces inside.

Challenge: Given the text-based emoticon, output the corresponding treemote.


Other rules:

  • This is , which means I want you to write short code.
  • Standard loopholes disallowed.
  • You must support non-ascii characters unless your language can't handle them.

Test Cases:

^_^

|     |
|     |
| ^_^ |
|     |
|     |

\o/

|     |
|     |
| \o/ |
|     |
|     |


(✿◠‿◠)

|        |
|        |
|        |
| (✿◠‿◠) |
|        |
|        |
|        |


D:

|    |
| D: |
|    |


( ͡° ͜ʖ ͡°)


|             |
|             |
|             |
|             |
|             |
|             |
| ( ͡° ͜ʖ ͡°) |
|             |
|             |
|             |
|             |
|             |
|             |
\$\endgroup\$
  • \$\begingroup\$ Are trailing newlines allowed? \$\endgroup\$ – R. Kap May 17 '16 at 21:59
  • \$\begingroup\$ We assume 1-char emoticons do not exist? \$\endgroup\$ – Adám May 17 '16 at 22:01
  • \$\begingroup\$ By "text-based emoticon" do you mean ascii emoticon? \$\endgroup\$ – Downgoat May 17 '16 at 22:21
  • \$\begingroup\$ @Downgoat no. See the ಠ_ಠ test case. \$\endgroup\$ – Rɪᴋᴇʀ May 17 '16 at 23:39
  • 6
    \$\begingroup\$ Maybe add a test case with a length other than 3... \$\endgroup\$ – SuperJedi224 May 18 '16 at 13:08

13 Answers 13

4
\$\begingroup\$

05AB1E, 27 25 bytes

Code:

g©Ìð×"|ÿ|
"®;îש„| ¹s¶®J

Explanation:

g                  # Push the length of the input string.
 ©                 # Copy that to the register.
  Ì                # Increment by 2.
   ð×              # Multiply by spaces.
     "|ÿ|\n"       # ÿ is used for interpolation and push the string "|spaces|\n".
                   #
 ®                 # Retrieve the value from the register.
  ;î               # Divide by 2 and round up.
    ×              # Multiply that by "|spaces|".
     ©             # Copy this into the register.
      „|           # Push the string "| ".
         Â         # Bifurcate, pushing the string and the string reversed.
          ¹s       # Push input and swap.
            ¶      # Push a newline character.
             ®J    # Retrieve the value from the register and join everything in the stack.
                   # Implicitly output this.

Uses CP-1252 encoding. Try it online!.

\$\endgroup\$
  • \$\begingroup\$ Inputting ( ͡° ͜ʖ ͡°) returns funny results. \$\endgroup\$ – Shaun Wild May 18 '16 at 10:14
  • \$\begingroup\$ @ShaunWild Yeah, it's a weird test case, because ( ͡° ͜ʖ ͡°) itself is 11 characters long, but it looks 8 characters long. \$\endgroup\$ – Adnan May 18 '16 at 10:16
4
\$\begingroup\$

Python 3.5, 76 75 73 bytes:

(Thanks to Blue for a tip which saved 2 bytes!)

def i(n):q=len(n);z=('|'+' '*(2+q)+'|\n')*-(-q//2);print(z+'| '+n+' |\n'+z)

Try It Online! (Ideone)

Also, here is a noncompeting Python 2.7.5 version since it is much longer at 87 bytes.

def i(n):q=len(n.decode('utf-8'));z=('|'+' '*(2+q)+'|\n')*-(-q/2);print z+'| '+n+' |\n'+z

This is because Python 2's default encoding is ascii, and therefore, characters such as outside the 128 unicode point range count as more than 1 byte (list('ಠ') yields ['\xe0', '\xb2', '\xa0']). The only workaround I could think of for this was to first decode the input using utf-8, and then move on with this utf-8 decoded string.

Try This Python 2 Version Online! (Ideone)

\$\endgroup\$
  • \$\begingroup\$ You can get rid of the parens around (-(-q//2)) for 2 bytes. I think you can save 2 more bytes by switching to Python 2.7. It doesn't need the second / in //, and only a space in the print statement. \$\endgroup\$ – Blue May 17 '16 at 22:59
  • \$\begingroup\$ @Blue Yeah, you're right, I can remove those second pair of parenthesis. However, I do need the second / in // to do the ceil division. \$\endgroup\$ – R. Kap May 17 '16 at 23:27
  • \$\begingroup\$ like I was saying, in Python 2.7 integer division is done with 1 / \$\endgroup\$ – Blue May 17 '16 at 23:28
  • \$\begingroup\$ @Blue Oh, I assumed you were saying I don't need that in Python 3. Well, in that case, then I will switch over to Python 2. Thanks for the tips! :) \$\endgroup\$ – R. Kap May 17 '16 at 23:29
  • \$\begingroup\$ @Blue If that's the case, then how do you do float division in Python 2? \$\endgroup\$ – R. Kap May 17 '16 at 23:31
3
\$\begingroup\$

Dyalog APL, 37 34 33 bytes

{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×≢⍵}

Chrome users: See footnote*

Test cases

      f←{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×≢⍵}

      f,'☺' ⍝ the , is necessary to create a 1 char string instead of a character scalar
|   |
| ☺ |
|   |
      f':D'
|    |
| :D |
|    |
      f'^_^'
|     |
|     |
| ^_^ |
|     |
|     |

*Chrome misdisplays the two characters ≢⍵ (U+2262,U+2375) as ≢⍵ (U+2261,U+0338,U+2375) instead of as ̸≡⍵ (U+0338,U+2262,U+2375), so here is a display version for Chrome: {↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×̸̸≡⍵}

\$\endgroup\$
  • 1
    \$\begingroup\$ +1 for having emoticons within your code itself {⍺⍵⍺}¨b \$\endgroup\$ – Value Ink May 18 '16 at 3:25
  • 3
    \$\begingroup\$ Ah, {⍺⍵⍺}¨b is the “APL programmer on his fifth cup of coffee giving a thumbs up” emoticon. \$\endgroup\$ – Lynn May 18 '16 at 12:36
2
\$\begingroup\$

V, 60 57 bytes

I| A |ByWo=ceil(len(""")/2.0)
 dF.d0kwviWr Yu@-pH@-P

Unfortunately, V has next to nothing in terms of mathematical operations. The divide and ceil functions drastically inflated the byte count.

Since this contains a bunch of nasty unprintables, here is a reversible hexdump:

00000000: 497c 201b 4120 7c1b 4279 576f 123d 6365  I| .A |.ByWo.=ce
00000010: 696c 286c 656e 2822 1222 2229 2f32 2e30  il(len("."")/2.0
00000020: 290d 201b 6446 2e64 306b 7776 6957 7220  ). .dF.d0kwviWr 
00000030: 5975 402d 7048 402d 50                   Yu@-pH@-P

Explanation:

I| A |                                #Add surrounding bars
      B                               #Move back
       yW                             #Yank a word
         o                            #Open a new line
          <C-r>=                      #Evaluate

          <C-r>"                      #Insert the yanked text into our evaluation
ceil(len("      ")/2.0)<cr>           #Evaluate ceil(len(text)/2) and insert it

 dF.                                  #Append a space and delete backward to a (.)
                                      #By default, this will be in register "-
    d0                                #Delete this number into register a
      kw                              #Move up, and forward a word
        viWr                          #Replace the emoticon with spaces
             Yu                       #Yank this line, and undo 
                                      #(so we can get the emoticon back)
               @-p                    #Paste this text "- times.
                  H                   #Move to the beginning
                   @-P                #Paste this text "- times behind the cursor.
\$\endgroup\$
  • \$\begingroup\$ How many bytes would it have been if there were mathematical operators? \$\endgroup\$ – Adnan May 17 '16 at 22:28
  • \$\begingroup\$ @Adnan It's hard to say, since I don't know exactly what they will look like. Optimistically I'd say around 30 bytes? \$\endgroup\$ – James May 17 '16 at 22:33
2
\$\begingroup\$

Vitsy, 43 bytes

IV2m3mz4m2m
3mV\D4m
V1+2/\[1m]
' || '
}}ZaO

Explanation:

IV2m3mz4m2m

I            Grab the length of the input string.
 V           Save that value to a global final variable.
  2m         Call the method at line index 2.
    3m       Call the method at line index 3.
      z      Push the entire input to the stack.
       4m    Call the method at line index 4.
         2m  Call the method at line index 2.

3mV\D4m

3m           Call the method at line index 3.
  V          Push the global variable to the stack.
   \D        Duplicate the top item on the stack that many times.
     4m      Call the method at line index 4.

V1+2/\[1m]

V            Push the global variable to the stack.
 1+          Add one to the top value.
             REASONING: We want ceil(V/2), and since repeat commands follow the floor value of repeats, we want ceil(V/2)+.5, so we add one to make this work right.
   2/        Divide by two.
     \[1m]   Call method 1 that top value of the stack times.

' || '

' || '       Push ' || ', the string, to the stack.

}}ZaO

}}           Push the bottom item of the stack to the top twice.
  Z          Output everything in the stack.
   aO        Output a newline.

Try it online!

Note that, due to a bug in TIO, input with unicode characters will not work. You'll have to use the local version instead for those. Thanks, @Dennis!

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

Pyke, 31 bytes

"||
"1dQlO*:Ql_2f_*iO"|  |"2Q:i

Try it here!

Thanks @R.Kap for saving a byte with the floor divide trick

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

Ruby, 57 bytes

Uses integer division tricks and takes advantages of the quirks in Ruby's puts function.

->e{s=e.size+1;puts k=[?|+' '*-~s+?|]*(s/2),"| #{e} |",k}
\$\endgroup\$
1
\$\begingroup\$

JavaScript ES6, 83 78 bytes

e=>(a=`| ${" ".repeat(n=e.length)} |
`.repeat(Math.ceil(n/2)))+`| ${e} |
${a}`
\$\endgroup\$
  • \$\begingroup\$ You don't need the f=, that saves you 2 bytes. Save another 2 bytes by moving e inside the template and a outside. Save another 2 bytes by inserting spaces instead of adding 2 to the repeat. Save another bunch of bytes by using bit shifting to divide by 2. \$\endgroup\$ – Neil May 17 '16 at 23:07
  • \$\begingroup\$ I don't think this works, it just tries to print the source code of the repeat \$\endgroup\$ – Bálint May 18 '16 at 11:13
  • \$\begingroup\$ Try again, I fixed it @Bálint \$\endgroup\$ – Conor O'Brien May 18 '16 at 11:17
1
\$\begingroup\$

><>, 103 bytes

i:0(?\
}$&1[\~rl:::2%+*:2,
1-:?!\" "$
]{\  \~
?!\$}1-:
~&\
?!\l$:@-[l2)
~]\
}}\" || "
?!\ol
8.>]l?!;ao2

Try it online!

This solution is based on the observation that each line consists of | <x> |, where <x> is the pattern in the middle line, and the same number of spaces in the other lines.

After reading the input (length n) from STDIN, the program pushes n*(n+(n%2)) spaces. The stack is then rolled half as many times. Next, all but n characters are pulled to a new stack, leaving a stack of stacks consisting of either n spaces or the pattern itself (in the middle stack only). In the output step, the contents of the current stack are printed, surrounded by | and |.

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

C, 89 bytes

f;main(int c,char**a){for(c=strlen(*++a)+1;f<(c|1);)printf("|%*s |\n",c,f++==c/2?*a:"");}

Not sure if it will handle non-ascii emoticon though....

\$\endgroup\$
  • \$\begingroup\$ Can you test it with the non-ascii emoticons? \$\endgroup\$ – Rɪᴋᴇʀ May 19 '16 at 15:17
  • \$\begingroup\$ Tried and the result wasn't good - strlen counts bytes until first zero byte and as result non-ascii emoticons are considered much wider than they are. \$\endgroup\$ – aragaer May 20 '16 at 8:16
1
\$\begingroup\$

PowerShell v3+, 72 bytes

param($a)$b=("| "+(" "*($l=$a.length))+" |`n")*($l+1-shr1);"$b| $a |";$b

Takes input string $a. Constructs $b as (the empty pipe-ended string (with $a.length spaces in the middle) and a trailing newline) repeated (length+1 shifted right one bit, i.e., divided by two and ceiling'd) times. Then outputs the copies of $b, the input string with its own pipes, and finally the copies of $b again.

Requires v3+ for the bit-shift -shr operator.

Examples

PS C:\Tools\Scripts\golfing> .\grow-a-treemote.ps1 '>:-|'
|      |
|      |
| >:-| |
|      |
|      |


PS C:\Tools\Scripts\golfing> .\grow-a-treemote.ps1 '>:`-('
|       |
|       |
|       |
| >:`-( |
|       |
|       |
|       |
\$\endgroup\$
1
\$\begingroup\$

Pyth, 30 bytes

I'm surprised that such an easy task isn't implemented in Pyth.

Ls[K\|dbdK)j++J*/hlQ2]sym;QyQJ

Try it online!

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

TSQL, 96 88 bytes

DECLARE @ varchar(100)='^_^'

PRINT STUFF(REPLICATE('|'+SPACE(2+LEN(@))+'|
',LEN(@)*2-1),LEN(@)*(LEN(@)+5)-3,LEN(@),@)

Try online!

\$\endgroup\$

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.