Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

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

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.
share|improve this answer
    
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. – alexwlchan Mar 2 at 10:52
    
@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. – Martin Büttner Mar 2 at 12:33

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.

share|improve this answer

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
share|improve this answer

Dyalog APL (older versions), 23 48 bytes

Adjusting whitespace is expensive:

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

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

share|improve this answer

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.

share|improve this answer
    
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)])) – user81655 Mar 2 at 1:56
    
@user81655 Wow, thanks! (Then I somewhat spoiled it by golfing a byte off your replace.) – Neil Mar 2 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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