38
\$\begingroup\$

Given an input of an ASCII art picture, output the ASCII art italicized.

To italicize the ASCII art:

  1. Insert zero spaces before the last line, one space before the second-last line, two spaces before the third-last line, etc.

  2. Remove any extra leading whitespace that may have been created. That is, if each line has at least n spaces preceding it, remove n spaces from the beginning of each line.

  3. Replace all \s with |, all |s with /, and all /s with _. Keep existing _s the same.

    The input is guaranteed to contain only the characters \|/_ and space.

For example, the input

/\/\/\    /\/\/\      /\/\/\    /\/\/\
 /\    /\  /\    /\  /\        /\
  /\/\/\    /\/\/\    /\        /\  /\/\
   /\        /\        /\        /\    /\
    /\        /\          /\/\/\    /\/\/\

must output

_|_|_|    _|_|_|      _|_|_|    _|_|_|
_|    _|  _|    _|  _|        _|
_|_|_|    _|_|_|    _|        _|  _|_|
_|        _|        _|        _|    _|
_|        _|          _|_|_|    _|_|_|

which in turn, provided as input, outputs

    _/_/_/    _/_/_/      _/_/_/    _/_/_/
   _/    _/  _/    _/  _/        _/
  _/_/_/    _/_/_/    _/        _/  _/_/
 _/        _/        _/        _/    _/
_/        _/          _/_/_/    _/_/_/

which would output

        ______    ______      ______    ______
      __    __  __    __  __        __
    ______    ______    __        __  ____
  __        __        __        __    __
__        __          ______    ______

Input and output may be either a single string (with optional trailing newline) or an array of lines.

Since this is , the shortest code in bytes will win.

Extra test cases:

|||||
  /////
 \\\\\
   _____
 /////
  _____
|||||
 _____


________
________


(that is, input of empty string results in output of empty string)

\$\endgroup\$
9
  • \$\begingroup\$ Can we assume that the input is padded to a rectangle? \$\endgroup\$ Mar 1, 2016 at 17:32
  • 2
    \$\begingroup\$ Are trailing spaces acceptable in each output line? \$\endgroup\$
    – Luis Mendo
    Mar 1, 2016 at 17:54
  • \$\begingroup\$ @MartinBüttner Yes, you can. \$\endgroup\$
    – Doorknob
    Mar 1, 2016 at 20:14
  • \$\begingroup\$ @DonMuesli Trailing spaces are okay, just no leading spaces. \$\endgroup\$
    – Doorknob
    Mar 1, 2016 at 20:14
  • \$\begingroup\$ May we take input as a rectangular array (matrix)? \$\endgroup\$
    – Adám
    Mar 1, 2016 at 23:16

10 Answers 10

6
\$\begingroup\$

CJam, 43 38 bytes

qN/W%eeSf.*:sW%z_{S-}#>zN*"\|/__"_(+er

Requires the input to be padded to a rectangle.

Try it online!

Explanation

qN/     e# Read input, split into lines.
W%      e# Reverse lines.
ee      e# Enumerate them (pairing each line with its index starting from 0).
Sf.*    e# Turn each index i into i spaces.
:s      e# Flatten each pair into a single string, prepending the spaces.
W%      e# Reverse the lines again.
z_      e# Transpose the character grid, duplicate it.
{S-}#   e# Find the first row index that contains non-spaces.
>       e# Discard that many rows.
zN*     e# Transpose back and join with linefeeds.
"\|/__" e# Push this string.
_(+     e# Make a copy that's rotated one character to the left, i.e. "|/__\".
er      e# Perform character transliteration mapping from the first string to the second.
\$\endgroup\$
2
  • \$\begingroup\$ Anecdotal, but I tried the PCCG example from the question, feeding each output back into the program, but without padding to a rectangle. Worked fine. \$\endgroup\$
    – alexwlchan
    Mar 2, 2016 at 10:52
  • \$\begingroup\$ @alexwlchan Well yeah, trailing spaces beyond the bounding rectangle don't matter (but will not necessarily be preserved). But if yout take the original PPCG example itself for instance, and remove the trailing spaces inside the G then the transformation will collapse it to the top. \$\endgroup\$ Mar 2, 2016 at 12:33
4
\$\begingroup\$

Pyth, 32

jCf|Z=Z-Td.t_.e+*kd.rb"\|/__"_Qd

Try it here or run the Test Suite

Each line has several trailing spaces. This works by applying .r which is the rotation built-in to each line of the string. The rotation takes each character in the string that matches one in the other argument "\|/__" and replaces it with the next character. The double underscore nicely prevents underscores from becoming backslashes. The strings are also padded based on their index.

Once that is done, the lines are zipped together, then we filter out each column that only contains spaces, until one of them doesn't. Then we unzip and join on newlines.

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

MATL, 38 33 29 bytes

P"NZ"@Y:'\|/' '|/_'XEh]XhPcYv

Output lines have trailing spaces to match the longest line (this is allowed by the challenge).

Input is a cell array (list) of strings. The array uses curly braces, and strings use single quotes, as follows (click each link to Try it online!).

  • First example:

    {'/\/\/\    /\/\/\      /\/\/\    /\/\/\'  ' /\    /\  /\    /\  /\        /\'  '  /\/\/\    /\/\/\    /\        /\  /\/\' '   /\        /\        /\        /\    /\' '    /\        /\          /\/\/\    /\/\/\'}
    
  • Second example:

    {'_|_|_|    _|_|_|      _|_|_|    _|_|_|' '_|    _|  _|    _|  _|        _|      ' '_|_|_|    _|_|_|    _|        _|  _|_|' '_|        _|        _|        _|    _|' '_|        _|          _|_|_|    _|_|_|'}
    
  • Third example:

    {'    _/_/_/    _/_/_/      _/_/_/    _/_/_/' '   _/    _/  _/    _/  _/        _/       ' '  _/_/_/    _/_/_/    _/        _/  _/_/  ' ' _/        _/        _/        _/    _/   ' '_/        _/          _/_/_/    _/_/_/    '}
    

Explanation

The array is initially flipped. Each string is processed in a loop and the modified string is pushed onto the stack. Processing consists in replacing the characters and appending a number of spaces. The number of spaces equals the current number of elements in the stack (thanks to the fact that the array has been flipped).

After the loop the strings are collected in an array, which is flipped back, converted to a 2D char array, and disposed of unwanted leading spaces: leading spaces that are present in all rows of the 2D char array.

P             % implicitly input cell array of strings. Flip it (reverse order)
"             % for each
  NZ"         %   string with as many spaces as elements there are on the stack
  @Y:         %   push current input string
  '\|/'       %   push string: origin for replacement
  '|/_'       %   push string: target for replacement
  XE          %   replace
  h           %   concatenate string with spaces and modified-character string
]             % end for each
Xh            % collect all processed strings in a cell array
P             % flip back to restore original order
c             % convert to 2D char array
Yv            % remove trailing spaces common to all rows. Implicitly display
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 171 149 148 bytes

a=>a.map(s=>(t=' '.repeat(--i)+s,l=t.match` *`[0].length,n=l<n?l:n,t),n=i=a.length).map(s=>s.slice(n).replace(/./g,c=>"|/__ "["\\|/_ ".indexOf(c)]))

Accepts and returns lines as an array of strings.

Edit: Saved 22 bytes thanks to @user81655. Saved 1 byte by realising that the input is limited to the 5 characters \|/_ and space.

\$\endgroup\$
2
  • \$\begingroup\$ replaces can be combined into .replace(/[\\|/]/g,m=>"|/_"["\\|/".indexOf(m)]), match(/ */) can be match` *`, first two maps can be combined into a.map(s=>(t=' '.repeat(--i)+s,(l=t.match` *`[0].length)<n?n=l:l,t),..., outer parentheses ((a=...)) can now be removed. 149 bytes: a=>a.map(s=>(t=' '.repeat(--i)+s,(l=t.match` *`[0].length)<n?n=l:l,t),n=i=a.length).map(s=>s.slice(n).replace(/[\\|/]/g,m=>"|/_"["\\|/".indexOf(m)])) \$\endgroup\$
    – user81655
    Mar 2, 2016 at 1:56
  • \$\begingroup\$ @user81655 Wow, thanks! (Then I somewhat spoiled it by golfing a byte off your replace.) \$\endgroup\$
    – Neil
    Mar 2, 2016 at 9:13
2
\$\begingroup\$

Dyalog APL (older versions), 23 48 bytes

Adjusting whitespace is expensive:

{{⍵↓⍨0,⌊/+/∧\' '=⍵}(⍳≢⍵)⌽'|/ _'['\| '⍳(∊,⊢,∊)⍵]}

This requires ⎕ML←0, which was default until recently.

\$\endgroup\$
2
+100
\$\begingroup\$

Vyxal, a 39 37 bytes

-2 thanks to @Aaron Miller

ṘėṘƛt`\|/``|/_`Ŀnh$꘍;:ƛ`^ *`$et;g£ƛ¥ȯ

Really bad answer, id like to fix it; the program takes in the string, breaks it into chunks, transliterates to the correct characters, and then spends most of the chars trying to fix the spacing.

Try it Online!

\$\endgroup\$
2
  • \$\begingroup\$ -2 bytes \$\endgroup\$ Jun 2, 2021 at 14:03
  • \$\begingroup\$ thanks for fixing that, my bad \$\endgroup\$
    – Underslash
    Jun 2, 2021 at 16:21
1
\$\begingroup\$

Pip -rl, 39 38 37 36 bytes

How do you golf an elephant? One byte at a time...

YsXRV,#g.:gWw~=@ZyY@>*yyTR"\|/""|/_"

Try it online!

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

Jelly, 32 bytes

JU⁶ẋ$;¥"µEƤS$€Ṃ$‘ṫ€@µ“\|/“|/_”yⱮ

Try it online!

The number in STDIN just indicates how many times italicization will be run (for easier testing). Set it to 1 for normal execution.

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

Stax, 24 bytes

╦'Fi╘◘¥╘τ9jq'∟w7#ù╢→↨↨─╟

Run and debug it

Takes a list of lines as input. They are formatted as ascii codepoints for convenience, but you can still pass a list of strings directly.

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

Python 3.8 (pre-release), 84 bytes

def f(s):
 i=len(s)
 for x in s:print(' '*i+x.translate({92:124,124:47,47:95}));i-=1

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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