21
\$\begingroup\$

Input a String and surround it with a rounded rectangle of alternating "friendliness pellets".(0)

Sandbox

enter image description here

Idea originally from Lyxal.

Challenge

Given a single line non-empty String of length \$<100\$, print the string with a rounded rectangle of alternating pellets(0) around it.

The rectangle must be 11 lines in height.

The first line of the rectangle must have at least 1 pellet.

The middle line of the rectangle must be at least 13 characters long. There must be a padding of one space on each side of the text.

Strings smaller than 9 characters must be padded with spaces equally on both sides to fit the above specification. The right side is allowed to have one extra space if the string is of even length.

The template for the output is as follows:

      00000..... 6 spaces
    0 4 spaces 
  0   2 spaces
 0    1 space
0     no spaces

If the string's length is greater than or equal to 9; the first line must have \$\text{length}-8\$ pellets.

Example Input and Output

Input:

Hello World!

Output:

       0000  
     0      0  
   0          0
  0            0
 0              0
 0 Hello World! 0
 0              0
  0            0
   0          0
     0      0
       0000

Input

0

Output

       0
     0   0  
   0       0
  0         0
 0           0
 0     0     0
 0           0
  0         0
   0       0
     0   0
       0

Input

The quick brown fox

Output

       00000000000 
     0             0  
   0                 0
  0                   0
 0                     0
 0 the quick brown fox 0
 0                     0
  0                   0
   0                 0
     0             0
       00000000000

Input

In this world, it's KILL or BE killed

Output

      00000000000000000000000000000
    0                               0
  0                                   0
 0                                     0
0                                       0
0 In this world, it's KILL or BE killed 0
0                                       0
 0                                     0
  0                                   0
    0                               0
      00000000000000000000000000000

Input

hi there

Output

      0
    0   0
  0       0
 0         0
0           0
0 hi there  0
0           0
 0         0
  0       0
    0   0
      0

Example code in Ruby

Try it online!

a=[4,2,1,0] # space length to create circle

str=gets.chomp;
l = str.length;
# print first line of pellets
print " "*6
if l<=9 then puts "0" else puts "0"*(l-8) end
    
# initial pattern
for n in a do 
    print " "*n +"0"+" "*(5-n)
    print " " if l<=9
    print " "*(l-8) if l>9
    puts " "*(5-n) +"0"

end
    
# display string with spaces
if l<9 then puts "0"+" "*((11-l)/2).floor + str + " "*((12-l)/2).ceil + "0" else puts "0 #{str} 0" end

#initial pattern but reversed
for n in a.reverse do 
    print " "*n +"0"+" "*(5-n)
    print " " if l<=9
    print " "*(l-8) if l>9
    puts " "*(5-n) +"0"
end
    
# last line(same as first line)
print " "*6
if l<=9 then puts "0" else puts "0"*(l-8) end


Scoring

This is . Shortest answer in each language wins.

\$\endgroup\$
0

7 Answers 7

8
\$\begingroup\$

Charcoal, 40 bytes

≔⌈⟦¹⁻Lθ⁸⟧η×η0↘↘0→↘00↓00‖OO←η‖O↓J⊘⁻⊖ηLθ⁵θ

Try it online! Link is to verbose version of code. Explanation:

≔⌈⟦¹⁻Lθ⁸⟧η

Calculate the number of 0s in the top row.

×η0

Print them.

↘↘0→↘00↓00

Print the remaining 0s on the right.

‖OO←η

Reflect the additional rows of 0s to the left, by overlapping the top row of 0s onto itself.

‖O↓

Reflect the 0s downwards overlapping the middle row.

J⊘⁻⊖ηLθ⁵θ

Calculate the position of the text and print it there. This is necessary for short strings but also avoids dealing with a Charcoal bug whereby an overlapped reflection moves the cursor to the wrong place.

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6),  152 139 134  133 bytes

s=>'64210~01246'.replace(/./g,k=>(g=i=>--i?(d=i+~k,d*=L-i+~k,s[k>g&&(L+n>>1)-i]||d&&k^6|d<0&&' ')+g(i):`
`)(L=(n=s.length)>9?n+5:14))

Try it online!

How?

Given an input string of length \$n\$, we define \$L=\min(14,n+5)\$. This is the width of the rows of the ASCII art, including the linefeed.

For each line, we store the number of leading spaces in \$k\$ and iterate from \$i=L-1\$ to \$i=1\$. We test the sign of \$d=(i-k-1)\times(L-i-k-1)\$ to figure out which character must be drawn.

We always draw a space if \$d\$ is negative and a zero if \$d=0\$. If \$d\$ is positive, we draw a zero if \$k=6\$ (top and bottom borders) or a space otherwise (inner cells). The middle line is a special case where we attempt to get the character from the input string, at the position \$\lfloor (L+n)/2\rfloor-i\$.

For "Hello World!", we have \$n=12\$ and \$L=17\$. This gives:

    1      0  
i = 6543210987654321
    ------0++0------  k = 6
    ----0++++++0----  k = 4
    --0++++++++++0--  k = 2
    -0++++++++++++0-  k = 1
    0++++++++++++++0  k = 0
    0+Hello World!+0  k = 0
    0++++++++++++++0  k = 0
    -0++++++++++++0-  k = 1
    --0++++++++++0--  k = 2
    ----0++++++0----  k = 4
    ------0++0------  k = 6

Commented

s =>                         // s = input string
'64210~01246'.replace(       // replace in the string '64210~01246'
  /./g, k => (               // each character k:
    g = i =>                 //   g is a recursive function taking a counter i:
      --i ?                  //     decrement i; if it's not 0:
        ( d = i + ~k,        //       d = i - k - 1
          d *= L - i + ~k,   //       d = (i - k - 1) * (L - i - k - 1)
          s[                 //       extract from s:
            k > g &&         //         if k is '~':
            (L + n >> 1) - i //           use floor((L + n) / 2) - i
                             //         otherwise, use false
                             //         (leading to s[false] == undefined)
          ]                  //       end of lookup in s
          ||                 //       if it's undefined:
            d &&             //         use '0' if d = 0
            k ^ 6 | d < 0    //         or k is equal to 6 and d is non-negative
            && ' '           //         otherwise, use a space
        ) + g(i)             //       append the result of a recursive call
      :                      //     else:
        `\n`                 //       append a linefeed and stop the recursion
  )(                         //   initial call to g:
    L = (n = s.length) > 9 ? //     if the length n of s is greater than 9:
      n + 5                  //       define L = n + 5
    :                        //     else:
      14                     //       define L = 14
  )                          //
)                            // end of replace()
\$\endgroup\$
2
  • \$\begingroup\$ What's the purpose of the tilde in '64210~01246'? \$\endgroup\$
    – Razetime
    Commented Sep 7, 2020 at 11:41
  • 2
    \$\begingroup\$ @Razetime It behaves like 0 for bitwise operations but triggers the test k > g. (Because the function g is coerced to "i=>...", any character greater than "i" would work.) \$\endgroup\$
    – Arnauld
    Commented Sep 7, 2020 at 11:58
5
\$\begingroup\$

05AB1E, 64 54 bytes

4Å0Îg9‚à8-©;îךŽüÎSú.B®Éi.ºëº}I11Ig-;1‚à©ú®îð׫0.øª».∊

Try it online or verify all test cases.

Explanation:

4Å0                   # Push a list of 4 0s: [0,0,0,0]
   Î                  # Push 0 and the input-string
    g                 # Pop the input, and push its length
     9‚à              # Pair with 9, and pop and push the maximum
        8-            # Subtract 8
          ©           # Store this in variable `®` (without popping)
           ;          # Halve it
            î         # Ceil it
             ×        # Create a string of that many 0s
              š       # Prepend it at the front of the [0,0,0,0]-list
ŽüÎ                   # Push compressed integer 64210
   S                  # Convert it to a list of digits: [6,4,2,1,0]
    ú                 # Pad that many leading spaces to the list of 0s
.B                    # Box it, which adds trailing spaces to each string to equal
                      # their lengths
®Éi                   # If the value `®` is odd:
   .º                 #  Mirror each line with the last character overlapping
  ë                   # Else (it's even instead):
   º                  #  Mirror each line without overlapping
  }                   # Close the if-else statement
I                     # Push the input-string again
 11                   # Push 11
   Ig-                # Subtract the input-length from it
      ;               # Halve it
       1‚à            # Pair with 1, and pop and push the maximum
          ©           # Store it in variable `®` (without popping)
           ú          # Pad that many leading spaces to the input
            ®         # Push `®` again
             î        # Ceil it
              ð׫     # Pad that many trailing spaces
                 0.ø  # Then surround it with leading/trailing 0
                    ª # And append it to the list of strings
»                     # Join the lines by newlines
 .∊                   # And mirror the entire string vertically with overlap
                      # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽüÎ is 64210.

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

Jelly, 52 bytes

³L>9ȯ
L11_»2⁶ẋŒHṚjµL_⁽|hḤD©Ḥ¤⁶ẋ;W¬1¦Ø0j0ç?Ɱ⁶ẋ®¤;"ŒḄY

A full program which prints the result.

Try it online!

How?

³L>9ȯ - Link 1: unused argument; list of characters, Y
³     - program argument, the given text (same as T in the main Link)
 L    - length
  >9  - greater than nine?
    ȯ - logical OR with Y
 
L11_»2⁶ẋŒHṚjµL_⁽|hḤD©Ḥ¤⁶ẋ;W¬1¦Ø0j0ç?Ɱ⁶ẋ®¤;"ŒḄY - Main Link: list of characters, T
L                                              - length
 11_                                           - 11 - that
    »2                                         - max of that and two
      ⁶ẋ                                       - that many spaces
        ŒH                                     - split in two
          Ṛ                                    - reverse (get longer part on the right)
           j                                   - join with T
            µ                                  - start a new monadic chain - f(X=that)
                      ¤                        - nilad followed by link(s) as a nilad:
               ⁽|h                             -   32105
                  Ḥ                            -   double -> 64210
                   D                           -   digits -> [6,4,2,1,0]
                    ©                          -   store in register
                     Ḥ                         -   double -> [12,8,4,2,0]
             L_                                - length (X) minus that (vectorises)
                       ⁶ẋ                      - that many spaces (vectorises)
                          W                    - wrap X in a list
                         ;                     - concatenate
                           ¬1¦                 - logical NOT first line
                                                   (changing spaces to 0s)
                              Ø0               - [0,0]
                                    Ɱ          - map across the lines with:
                                   ?           -   if...
                                  ç            -   ...condition: call Link 1 f([0,0], line)
                                j              -   ...then: ([0,0]) join with (line)
                                 0             -   ...else a single zero
                                        ¤      - nilad followed by link(s) as a nilad:
                                     ⁶         -   space
                                       ®       -   recall from register -> [6,4,2,1,0]
                                      ẋ        -   repeat (vectorises)
                                         ;"    - zip with concatenation
                                                   (prefix lines with spaces)
                                           ŒḄ  - bounce (reflect top lines to the bottom)
                                             Y - join with newlines
                                               - implicit print
\$\endgroup\$
3
\$\begingroup\$

Python 3, 134 bytes

lambda s:'\n'.join(f'{0:{i}}{0**i*s:{"0 "[i<6]}^{max(i*2,len(s)+4,13)-(i*2or 2)}}'+'0'[i+i>10>len(s):]for i in(7,5,3,2,1,0,1,2,3,5,7))

Try it online!

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

Perl 5, 155 bytes

sub f{$_=pop;substr$r=64210001246=~s,.,$"x$&.($&-6?0 .$"x(11-$&*2).0:0).$/,ger=~s,^.{6}(.),($l=y|||c-9)>0?$&.$1x$l:$&,mger,$l>0?59+5*$l:59-$l/2,$l+9,$_;$r}

Try it online!

Slightly ungolfed:

sub f{
  $_=pop;                           # $_ = input text
  substr                            # args: string, offset, length, replacement
    $r=                             # $r = result string init to empty rectangle
      64210001246                   # digits are spaces before first 0 each line
        =~s,.,$"x$&.($&-6?0 .$"x(11-$&*2).0:0).$/,ger
        =~s,^.{6}(.),($l=y|||c-9)>0?$&.$1x$l:$&,mger,
    $l>0 ? 59+5*$l : 59-$l/2,       # insert input text at this position
    $l+9,                           # ...this length ($l=input length minus 9)
    $_;                             # replacement = input
  $r                                # return result
}
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 158 bytes

lambda s:'\n'.join((e:=[" "*6+"0"*(l:=len(b:=(c:=9-len(s))//2*' '+s+(c//2+c%2)*' ')-8),*[i*" "+"0"+" "*(l+10-2*i)+"0"for i in[4,2,1,0]],f'0 {b} 0'])+e[4::-1])

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! For future reference, TIO's Python 2 is on Python 2.7.16, Python 3 is on Python 3.7.4, and Python 3.8 is on Python 3.8.0b4. \$\endgroup\$
    – The Thonnu
    Commented Jun 10, 2023 at 14:05
  • \$\begingroup\$ Here is a screenshot of what I see on TIO Python 3: imgur link \$\endgroup\$ Commented Jun 10, 2023 at 14:27
  • \$\begingroup\$ My first answer was wrong by the way, that's why I put strikethrough on it \$\endgroup\$ Commented Jun 10, 2023 at 14:29
  • \$\begingroup\$ That's because you're checking the Python 2 version. Use python3 --version or python3.8 --version instead. Sorry about the edit, I thought you just needed help with the TIO link. \$\endgroup\$
    – The Thonnu
    Commented Jun 10, 2023 at 14:30
  • 1
    \$\begingroup\$ Oh, that makes sense, thanks! \$\endgroup\$ Commented Jun 10, 2023 at 14:42

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