41
\$\begingroup\$

Introduction:

Inspired by this comment of @MagicOctopusUrn on @Emigna's 05AB1E answer for my "It was just a bug" challenge:

8F9ÝÀNð×ý}».∊ I done did made a spaceship maw! And I was all excited about suggesting a 12-byte edit. – Magic Octopus Urn Jul 17 '17 at 20:10

Which is a 05AB1E (legacy) program resulting in this:

1234567890
1 2 3 4 5 6 7 8 9 0
1  2  3  4  5  6  7  8  9  0
1   2   3   4   5   6   7   8   9   0
1    2    3    4    5    6    7    8    9    0
1     2     3     4     5     6     7     8     9     0
1      2      3      4      5      6      7      8      9      0
1       2       3       4       5       6       7       8       9       0
1      2      3      4      5      6      7      8      9      0
1     2     3     4     5     6     7     8     9     0
1    2    3    4    5    6    7    8    9    0
1   2   3   4   5   6   7   8   9   0
1  2  3  4  5  6  7  8  9  0
1 2 3 4 5 6 7 8 9 0
1234567890

Try it online.

Challenge:

Input: A non-empty string

Output: From outwards going inwards, add one more space between each character every line, similar as done in the output above, equal to the length - 1. So for an input 1234567890 the output would actually be this instead:

1234567890
1 2 3 4 5 6 7 8 9 0
1  2  3  4  5  6  7  8  9  0
1   2   3   4   5   6   7   8   9   0
1    2    3    4    5    6    7    8    9    0
1     2     3     4     5     6     7     8     9     0
1      2      3      4      5      6      7      8      9      0
1       2       3       4       5       6       7       8       9       0
1        2        3        4        5        6        7        8        9        0
1         2         3         4         5         6         7         8         9         0
1        2        3        4        5        6        7        8        9        0 
1       2       3       4       5       6       7       8       9       0
1      2      3      4      5      6      7      8      9      0
1     2     3     4     5     6     7     8     9     0
1    2    3    4    5    6    7    8    9    0
1   2   3   4   5   6   7   8   9   0
1  2  3  4  5  6  7  8  9  0
1 2 3 4 5 6 7 8 9 0
1234567890

Why? The length of 1234567890 is 10. So we start by outputting 10 lines: the first line without spaces; second with one space delimiter; third with two; etc. And then (without have the middle line with length - 1 spaces duplicated), we go back to the initial input while going down.

Challenge rules:

  • Input is guaranteed to be non-empty (a length >= 1). (For single char inputs we simply output that character.)
  • Any amount of trailing/leading spaces/newlines are allowed, as long as the output itself (wherever on the screen) is correct. (Empty line(s) in between output lines also isn't allowed.)
  • Input will only contain printable ASCII characters excluding whitespaces (code-point range [33, 126])
  • I/O is flexible. Input may be taken as STDIN, argument, or function parameter. May be a list/array/stream of characters instead of string. Output may also be a list/array/stream of characters instead of strings; may be printed to STDOUT; returned as newline-delimited string; etc.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, adding an explanation for your answer is highly recommended.

Test cases:

Input: @
Output:
@

Input: test
Output:
test
t e s t
t  e  s  t
t   e   s   t
t  e  s  t
t e s t
test

Input: ?!
Output:
?!
? !
?!

Input: Spaceship
Output:
Spaceship
S p a c e s h i p
S  p  a  c  e  s  h  i  p
S   p   a   c   e   s   h   i   p
S    p    a    c    e    s    h    i    p
S     p     a     c     e     s     h     i     p
S      p      a      c      e      s      h      i      p
S       p       a       c       e       s       h       i       p
S        p        a        c        e        s        h        i        p
S       p       a       c       e       s       h       i       p
S      p      a      c      e      s      h      i      p
S     p     a     c     e     s     h     i     p
S    p    a    c    e    s    h    i    p
S   p   a   c   e   s   h   i   p
S  p  a  c  e  s  h  i  p
S p a c e s h i p
Spaceship

Input: 05AB1E
Output:
05AB1E
0 5 A B 1 E
0  5  A  B  1  E
0   5   A   B   1   E
0    5    A    B    1    E
0     5     A     B     1     E
0    5    A    B    1    E
0   5   A   B   1   E
0  5  A  B  1  E
0 5 A B 1 E
05AB1E

Input: )}/\
Output:
)}/\
) } / \
)  }  /  \
)   }   /   \
)  }  /  \
) } / \
)}/\
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Gets all just jittery !!!SPACESHIP!!! \$\endgroup\$ Sep 5, 2018 at 22:03
  • 1
    \$\begingroup\$ I knew I recognized that output. I love that this idea is still going. \$\endgroup\$ Sep 5, 2018 at 23:01
  • 3
    \$\begingroup\$ TFW you vaguely recognize a pattern in a question ಠ_ಠ then realize it's because you accidentally made it a year ago ಠ⌣ಠ. \$\endgroup\$ Sep 6, 2018 at 17:25
  • 1
    \$\begingroup\$ @MagicOctopusUrn Thanks for the inspiration. ;D \$\endgroup\$ Sep 6, 2018 at 17:26
  • 3
    \$\begingroup\$ @KevinCruijssen thanks for keeping the goofy quote haha! \$\endgroup\$ Sep 6, 2018 at 17:26

45 Answers 45

12
\$\begingroup\$

Japt, 8 6 bytes

Takes input as an array of characters, outputs an array of strings.

£qYçÃê

Try it


Explanation

£          :Map each element at (0-based) index Y
 q         :  Join input with
  Yç       :   Space repeated Y times
    Ã      :End Map
     ê     :Palindromise

Original, 8 bytes

I/O is a string. Uses the -R flag. Includes trailing spaces on each line.

¬£múYÄÃê

Try it

Explanation

             :Implicit input of string U
¬            :Split
 £           :Map each character at 0-based index Y
  m          :  Map original U
   ú         :    Right pad with spaces to length ...
    YÄ       :     Y+1
      Ã      :End map
       ê     :Palindromise
             :Implicitly join with newlines
\$\endgroup\$
7
  • \$\begingroup\$ S.ç() FTW once again :-) \$\endgroup\$ Sep 4, 2018 at 16:00
  • 1
    \$\begingroup\$ Serious question: would it be feasible to search iteratively through all 1 to 6 bytes long solutions for a puzzle like this? \$\endgroup\$
    – filip
    Sep 5, 2018 at 7:22
  • 2
    \$\begingroup\$ @filip No: there are more than 256**6=281474976710656 (at least naive) combinations. It's like guessing a password. \$\endgroup\$ Sep 5, 2018 at 14:44
  • 3
    \$\begingroup\$ @KirillBulygin, there are over 37 trillion (37,764,717,485,592) possible ways of combining the characters available in Japt into a string between 1 & 6 characters long. If you include all the other 1 byte characters which can be used in string literals or compressed strings, that number grows to over 276 trillion (276,024,445,697,280). So, no, writing a bot to generate all those then filter out the valid Japt programmes then find the one (if any exists) that works for the challenge at hand probably wouldn't be feasible. Besides where's the fun in letting a bot do your golfing for you?! \$\endgroup\$
    – Shaggy
    Sep 5, 2018 at 15:46
  • 7
    \$\begingroup\$ @Shaggy: "where's the fun in letting a bot do your golfing for you?!" What if you made the bot really, really short? \$\endgroup\$ Sep 7, 2018 at 2:17
11
\$\begingroup\$

R, 105 99 85 84 79 bytes

-6 thanks to @Kevin Cruissen and @Giuseppe

-14 from changing to a regex based method

-1 thanks to @Giuseppe

-5 thanks to @digEmALl

function(w,n=nchar(w)-1)write(trimws(Map(gsub,"",strrep(" ",n-abs(n:-n)),w)),1)

Try it online!

\$\endgroup\$
10
  • \$\begingroup\$ You can golf 1 byte by removing the space at in(r<-. \$\endgroup\$ Sep 4, 2018 at 17:01
  • 1
    \$\begingroup\$ and you only use s once so you can just use it as an argument in write directly; bringing you down to 99 bytes \$\endgroup\$
    – Giuseppe
    Sep 4, 2018 at 17:08
  • 1
    \$\begingroup\$ should that be a 1 rather than a "" in write? I'm digging your use of Map! \$\endgroup\$
    – Giuseppe
    Sep 4, 2018 at 17:57
  • 1
    \$\begingroup\$ Awesome ! 79 bytes using math :) \$\endgroup\$
    – digEmAll
    Sep 5, 2018 at 7:27
  • 1
    \$\begingroup\$ I was convinced I could beat this with the collapse argument to paste but it just isn't happening... \$\endgroup\$
    – JDL
    Sep 6, 2018 at 16:21
6
\$\begingroup\$

JavaScript (ES6), 53 bytes

Takes input as an array of characters.

f=(s,p=i='',o=s.join(p)+`
`)=>s[++i]?o+f(s,p+' ')+o:o

Try it online!

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

Charcoal, 10 bytes

Eθ⪫θ× κ‖O↓

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

Eθ          Map over characters of input string
  ⪫θ        Join characters of input string using
    ×       a literal space repeated 
      κ     current index number of times
            Implicitly print each result on its own line
       ‖O↓  Reflect vertically with overlap
\$\endgroup\$
3
  • \$\begingroup\$ Charcoal doesn't reflect things like [ to ]-- or is that another separate command? \$\endgroup\$ Sep 6, 2018 at 21:50
  • \$\begingroup\$ @MagicOctopusUrn There are separate commands if you want to transform the reflection. See codegolf.stackexchange.com/a/127164 for example. \$\endgroup\$
    – Neil
    Sep 6, 2018 at 22:19
  • \$\begingroup\$ I thought I had seen it reflect before, but wasn't sure. Neat! \$\endgroup\$ Sep 6, 2018 at 22:23
6
\$\begingroup\$

Canvas, 8 bytes

┐² ×*]──

Try it here!

The 7 byte version was too good for this challenge..

\$\endgroup\$
2
  • \$\begingroup\$ I like your 7 byte version. Would you be willing to explain how it works? Which operator overlaps the two /s into a X? \$\endgroup\$
    – Kaya
    Sep 4, 2018 at 21:31
  • 3
    \$\begingroup\$ @Kaya it's the last character - - vertical palindromize. The palindromization of Canvas does cool things. The overlapping part has it's own character too. \$\endgroup\$
    – dzaima
    Sep 4, 2018 at 21:37
6
\$\begingroup\$

Python 2, 72 70 68 66 65 bytes

-2 bytes thanks to Kevin Cruijssen
-3 bytes thanks to ovs

w=input();c=s=-1
while c:print(' '*~c).join(w);s*=w[:c]>'';c+=s|1

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ c==len(w)-1 can be golfed by 1 byte with c+2>len(w). EDIT: In your new 70-byte version, 0<c can be c. \$\endgroup\$ Sep 4, 2018 at 14:13
6
\$\begingroup\$

05AB1E, 10 9 bytes

Saved 1 bytes thanks to Adnan

εINð×ý}û»

Try it online!

Explanation

ε            # apply to each in input
 I           # push the input
  Nð×        # push <index> spaces
     ý       # merge the input on the spaces
      }      # end loop
       û     # palendromize
        »    # join on newlines
\$\endgroup\$
12
  • \$\begingroup\$ You can drop the S. Input is flexible, so inputting as list is allowed. \$\endgroup\$ Sep 4, 2018 at 14:26
  • 1
    \$\begingroup\$ @KevinCruijssen: Your first version is 9 bytes in legacy though, as » can be omitted. \$\endgroup\$
    – Emigna
    Sep 4, 2018 at 16:22
  • 1
    \$\begingroup\$ Yours actually works in the legacy in 9 bytes as well if you remove » and change the loop v to a map ε. \$\endgroup\$ Sep 4, 2018 at 17:04
  • 1
    \$\begingroup\$ Posted the answer. And currently Japt is beating us with 6 bytes I'm afraid. Or did you mean shortest in 05AB1E (Elixir rewrite) and 05AB1E (Python legacy)? :) \$\endgroup\$ Sep 4, 2018 at 21:09
  • 2
    \$\begingroup\$ Does εINð×ý}û» also work? \$\endgroup\$
    – Adnan
    Sep 4, 2018 at 21:20
5
\$\begingroup\$

Ruby, 54 49 bytes

->a{(-(z=a.size-1)..z).map{|i|a*(?\s*(z-i.abs))}}

Try it online!

Takes input as an array of characters, outputs array of strings.

\$\endgroup\$
5
\$\begingroup\$

Haskell, 60 59 bytes

(init<>reverse).(scanl(?)<*>tail)
a?_=do u<-a;u:[' '|' '<u]

Try it online!

Explanation

For a string (eg. "abc") we apply first

scanl (?) <*> tail

which is the same as

\str -> scanl (?) str (tail str)

This repeatedly applies (?) (appends a space to each character in the range [33..]) to the str until there are that many strings as str has characters: ["abc","a b c ", "a b c "]

Now we only need to concatenate the result (minus the last element) with its reversed counter part:

init<>reverse
\$\endgroup\$
4
\$\begingroup\$

PowerShell, 66 54 bytes

-12 bytes thanks to mazzy

0..($x=($a=$args).count-1)+$x..0|gu|%{$a-join(' '*$_)}

Try it online!

Takes input via splatting, which on TIO manifests as separate command-line arguments for each character.

We first set $a=$args as the input argument. Then we set $x equal to the .count of that array -1. We then need to loop through the letters to construct the spaceship. That's done by constructing a range from 0 to $x, then $x back down to 0, then using Get-Unique to pull out just the appropriate range.

Each iteration, we take our input arguments and -join them together with the corresponding number of spaces. Each of those strings is left on the pipeline, and an implicit Write-Output gives us newlines for free when the program completes.

\$\endgroup\$
2
  • \$\begingroup\$ Try this: 0..($x=($a=$args).count-1)+$x..0|gu|%{$a-join(' '*$_)} \$\endgroup\$
    – mazzy
    Sep 5, 2018 at 13:25
  • 1
    \$\begingroup\$ @mazzy What the ... how does Get-Unique work like that on the range? That's crazy! Thanks! \$\endgroup\$ Sep 5, 2018 at 13:50
4
\$\begingroup\$

05AB1E (legacy), 9 bytes

εINúíJ}û»

Input as list of characters.

Try it online or verify all test cases.

Explanation:

ε     }    # Map each character in the input to:
 I         #  Take the input
  Nú       #  Prepend each with the 0-indexed amount of spaces
           #   i.e. ["t","e","s","t"] & 3 → ["   t","   e","   s","   t"]
    í      #  Reverse each item
           #   i.e. ["   t","   e","   s","   t"] → ["t   ","e   ","s   ","t   "]
     J     #  Join them together to a single string
           #   i.e. ["t   ","e   ","s   ","t   "] → "t   e   s   t   "
       û»  # Palindromize the list, and join by newlines
           #  i.e. ["test","t e s t ","t  e  s  t  ","t   e   s   t   "]
           #   → "test\nt e s t \nt  e  s  t  \nt   e   s   t   \nt  e  s  t  \nt e s t \ntest"
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Oooooo, also this isn't valid because mirror changes the orientation of / to \ when mirrored, same with [, ] and (, ). May want to add those cases to catch charcoal too. (Moved the other comments to Emigna's answer, because he was the answer I originally commented on) \$\endgroup\$ Sep 6, 2018 at 21:46
  • \$\begingroup\$ @MagicOctopusUrn Thanks for letting me know. Fixed it by using û» instead of .∊. \$\endgroup\$ Sep 7, 2018 at 7:07
4
\$\begingroup\$

MATL, 25 22 13 bytes

zZv"Gtz@he!1e

Try it online!

Thanks to Luis Mendo for suggesting a 5 byte golf, which then inspired me to shave off 4 more bytes!

Explanation, with example input 'abc':

         # Implicit input, 'abc'
z        # find number of nonzero elements (length of string)
         # stack: [3]
Zv       # symmetric range
         # stack: [[1 2 3 2 1]]
"        # begin for loop, iterating over [1 2 3 2 1] as the loop indices
G        # push input
         # stack: ['abc']
tz       # dup and push length
         # stack: ['abc', 3]
@        # push loop index, i (for example, 2)
         # stack: ['abc', 3, 2]
h        # horizontally concatenate
         # stack: ['abc', [3, 2]]
e!       # reshape to matrix of 3 rows and i columns, padding with spaces, and transpose
         # stack: [['abc';'   ';'   ']]
1e       # reshape to matrix of 1 row, leaving last value on stack
         # stack: ['a  b  c  ']
         # implicit end of for loop
         # implicit end of program, display stack contents
\$\endgroup\$
0
4
\$\begingroup\$

Japt, 9 8 bytes

-1 byte from @Shaggy

ÊƬqXîÃê

ÊƬqXîÃê        Full program, implicity input U
ÊÆ              Range from 0 to U length and map
  ¬             split U at ""
   qXîà         join U using " " times range current value
        ê       horizontal mirror

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Dang; looks like you ninjaed me again! Lemme know if you'd like me to delete mine. \$\endgroup\$
    – Shaggy
    Sep 4, 2018 at 14:19
  • 1
    \$\begingroup\$ @Shaggy no, keep your answer, you are using array as input while I use a string so they are kind of different xD \$\endgroup\$ Sep 4, 2018 at 14:20
  • 1
    \$\begingroup\$ SpX -> for a 1 byte saving. \$\endgroup\$
    – Shaggy
    Sep 4, 2018 at 14:30
3
\$\begingroup\$

Jelly, 9 bytes

jⱮLḶ⁶ẋƲŒḄ

Try it online!

Returns a list of lines; output prettified over TIO.

\$\endgroup\$
5
  • \$\begingroup\$ A somewhat different 9: ,€⁶$LСŒḄ. Other, more similar, 9's: J’⁶ẋŒḄɓjⱮ and J’⁶ẋŒḄjⱮ@ (I was looking for shorter but no joy yet) \$\endgroup\$ Sep 4, 2018 at 16:33
  • \$\begingroup\$ @JonathanAllan I'm pretty sure this is optimal, I don't think there's any shorter way to write LḶ⁶ẋ or ŒḄ. However, if you manage to find a save, do ping me. :-) \$\endgroup\$ Sep 4, 2018 at 16:35
  • \$\begingroup\$ Thinking about it a bit my ,€⁶$LСŒḄ may not be valid since it has crazy nesting so might need a Y and be a full program. \$\endgroup\$ Sep 4, 2018 at 16:37
  • \$\begingroup\$ @JonathanAllan Yeah, of course it's not. ['I', 'f', [[' '], 't', 'h', [['i']], 's'], ' ', 'i', ['s', ' '], 'a', [[' ', 's', 't'], 'r', ['i', 'n', 'g'], ' '], 'w', ['e', ' ', 'a', 'r', 'e'], ' ', 'd', 'o', ['o'], 'm', [[[[['e']]]]], [[[['d']]]], '!'] At least I've outgolfed 05AB1E... \$\endgroup\$ Sep 4, 2018 at 16:39
  • \$\begingroup\$ I said I outgolfed 05AB1E, eh? Bah, not anymore. :/ \$\endgroup\$ Sep 5, 2018 at 18:00
3
\$\begingroup\$

Pyth, 12 bytes

Just my mandatory Pyth submission. I am quite proud of this so an explanation will likely come soon.

+P_=jRQ*L;_U

Try it here!

+P_=jRQ_.e*d

Try it here!

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

Stax, 10 bytes

Ç·9ƒù▌╘Ä┘e

Run and debug it

Outputs with trailing whitespace on each line.

Explanation:

%R|pmx{]n(m Full program, unpacked, implicit input
%           Length of input
 R          1-based range
  |p        Palindromize
    m       Map:
     x{   m   Map over characters of input:
       ]        Character -> string
        n(      Right-pad to length given by outer map value
              Implicit flatten and output
\$\endgroup\$
3
\$\begingroup\$

Java (JDK 10), 115 bytes

s->{for(int l=s.length(),i=-l;++i<l;)System.out.printf(s.replaceAll(".","%-"+(i<0?l+i:l-i)+"s")+"%n",s.split(""));}

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ l-Math.abs(i) can be golfed to i<0?l+i:l-i for -2 bytes. \$\endgroup\$ Sep 4, 2018 at 14:42
  • \$\begingroup\$ @KevinCruijssen Thanks! :) \$\endgroup\$ Sep 4, 2018 at 14:44
  • \$\begingroup\$ could it be l-i<0?-i:i? \$\endgroup\$
    – Quintec
    Sep 4, 2018 at 18:06
  • \$\begingroup\$ @thecoder16 You would need parentheses: l-(i<0?-i:i) (12 bytes). \$\endgroup\$ Sep 4, 2018 at 18:22
3
\$\begingroup\$

Oracle SQL, 115 bytes

Not a golfing language but...

SELECT TRIM(REGEXP_REPLACE(v,'(.)',LPAD('\1',1+LENGTH(v)-ABS(LEVEL-LENGTH(v)))))FROM t CONNECT BY LEVEL<2*LENGTH(v)

Assuming that the value is in column v of table t:

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE t ( v ) AS
  SELECT 'test' FROM DUAL;

Query 1:

SELECT TRIM(REGEXP_REPLACE(v,'(.)',LPAD('\1',1+LENGTH(v)-ABS(LEVEL-LENGTH(v)))))
FROM   t
CONNECT BY LEVEL<2*LENGTH(v)

Results:

(SQLFiddle prints the values right-aligned in the column for some reason... there are no leading spaces)

| TRIM(REGEXP_REPLACE(V,'(.)',LPAD('\1',1+LENGTH(V)-ABS(LEVEL-LENGTH(V))))) |
|---------------------------------------------------------------------------|
|                                                                      test |
|                                                                   t e s t |
|                                                                t  e  s  t |
|                                                             t   e   s   t |
|                                                                t  e  s  t |
|                                                                   t e s t |
|                                                                      test |
\$\endgroup\$
3
  • \$\begingroup\$ Your SQL Fiddle results seems to only have single spaces in between characters? See this screenshot. I assume this is due to SQL Fiddle and it works locally? Btw, not sure if SQL uses standard regex rules, but can (.) be golfed to . by using \0 instead of \1 like you could in Java for example? EDIT: Never mind, that is for $0, not \0.. (Java example of what I meant). \$\endgroup\$ Sep 6, 2018 at 13:11
  • 1
    \$\begingroup\$ @KevinCruijssen Click on the down arrow next the the "Run SQL" button and change the output to "Plaintext Output" or "Markdown Output" and you will see the spaces. \$\endgroup\$
    – MT0
    Sep 6, 2018 at 13:21
  • \$\begingroup\$ Thanks. It indeed looks good in that case! \$\endgroup\$ Sep 6, 2018 at 13:23
3
\$\begingroup\$

K (oK), 25 24 bytes

Solution:

,/'(1+a,1_|a:!#x)$\:+,x:

Try it online!

Explanation:

Port of my K4 solution:

,/'(1+a,1_|a:!#x)$\:+,x: / the solution
                      x: / save input as x
                     ,   / enlist
                    +    / flip
                 $\:     / pad ($) right by each-left (\:)
   (            )        / do this together
              #x         / count length of input,           e.g. 3
             !           / range 0..length,                 e.g. 0 1 2
           a:            / save as a
          |              / reverse it,                      e.g. 2 1 0
        1_               / drop first,                      e.g. 1 0
      a,                 / join to a,                       e.g. 0 1 2 1 0
    1+                   / add 1,                           e.g. 1 2 3 2 1
,/'                      / flatten (,/) each (')

Notes:

  • -1 byte thanks to ngn
\$\endgroup\$
1
  • 1
    \$\begingroup\$ ,:' -> +,­­ \$\endgroup\$
    – ngn
    Sep 14, 2018 at 10:15
3
\$\begingroup\$

Zsh, 95 bytes

Try it Online!

k=$#1;((k<2))&&<<<$1&&exit
for i ({1..$[k-1]} {$k..1}){for x (${(s::)1})printf %-${i}s $x;echo}

Handling that dang @ test case cost 19 bytes! I feel like this could be smaller by zipping arrays. Then hopefully we can beat the gnarly bash solution!!

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

Bash, 115, 109, 105, 100, 97, 96, 92, 91, 90, 82 bytes

-5 & -3 thanks to Kevin Cruissen -8 thanks to roblogic

for((c=f=1;f;c-=2*(f>=${#1}),f+=c))
{ printf '%-'${f}'.c' `grep -o .<<<"$1"`
echo
}

Try it online!

--- Note that since the \ is a shell escape char, the test case )}/\ should be entered with an extra \ like this: )}/\\.

\$\endgroup\$
7
  • \$\begingroup\$ Hi there. Is it possible to add a TIO-link with test case(s)? Also, I don't know Bash very well, but is it possible to remove the spaces after in and printf like in Python? \$\endgroup\$ Sep 7, 2018 at 13:29
  • 1
    \$\begingroup\$ @KevinCruijssen, Thanks, see revised answer. I'm new to TIO-link however, and am not sure how to use multiple test cases, since this bash code only inputs a string, (i.e. just one line). The test cases all work however, though )}/\ must be single quoted like this <<< ')}/\' read s; ...etc. . The spaces after in and printf are needed. \$\endgroup\$
    – agc
    Sep 7, 2018 at 13:47
  • 1
    \$\begingroup\$ In your new version you can golf 5 more bytes like this. Spaces after for and do can be removed. f=1 can be changed to c=f=1. And f=f+c can be f+=c. \$\endgroup\$ Sep 7, 2018 at 14:31
  • 1
    \$\begingroup\$ Oh, one more small thing to golf. Wasn't sure if it's possible in Bash, but apparently it is (one more reason why a TIO-link is handy ;) ), is by changing f!=0 to f in the for-loop. Just like in JavaScript and Python, 0 is falsey and every other positive/negative integer is apparently truthy in Bash. \$\endgroup\$ Sep 7, 2018 at 14:49
  • 1
    \$\begingroup\$ @roblogic, Good idea -- the OP did say the input was flexible. I was fixated on STDIN... \$\endgroup\$
    – agc
    Dec 16, 2020 at 3:15
2
\$\begingroup\$

Pascal (FPC), 143 135 bytes

var s:string;i,j,l:word;begin read(s);l:=length(s);repeat i:=i+1;for j:=1to l do write(s[j],'':l-abs(l-i)-1);writeln until i=l*2-1 end.

Try it online!

I will probably win only against Lenguage...

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

PHP, 88 89 bytes

for(;++$i<2*$e=count($a=str_split($argn));)echo join(str_pad("",~-$e-abs($i-$e)),$a),"\n";

requires PHP 5 or later for str_split. Run as pipe with -nR or try it online.

\$\endgroup\$
6
  • \$\begingroup\$ Your try-it-online link gives the wrong output I'm afraid. In your output all lines also have leading spaces, instead of only between characters (or optionally trailing). The first character should all be in the same column in the output. \$\endgroup\$ Sep 4, 2018 at 19:06
  • 1
    \$\begingroup\$ @KevinCruijssen Didn´t look at the output close enough. Fixed. (Though imo my previous output looked more like a rocket) ;-) \$\endgroup\$
    – Titus
    Sep 4, 2018 at 19:34
  • \$\begingroup\$ It kinda looked like the head of an arrow to me. :D But +1 now that it's fixed. \$\endgroup\$ Sep 4, 2018 at 20:53
  • \$\begingroup\$ If you place an actual newline instead of the \n, you save a byte :) \$\endgroup\$
    – Martijn
    Sep 6, 2018 at 14:22
  • \$\begingroup\$ @Martijn I actually did only count one byte for it ... forgot to replace it when I pasted the code here. \$\endgroup\$
    – Titus
    Sep 6, 2018 at 22:26
2
\$\begingroup\$

K4, 23 bytes

Solution:

,/'(1+a,1_|a:!#x)$\:$x:

Example:

q)k),/'(1+a,1_|a:!#x)$\:$x:"Spaceship"
"Spaceship"
"S p a c e s h i p "
"S  p  a  c  e  s  h  i  p  "
"S   p   a   c   e   s   h   i   p   "
"S    p    a    c    e    s    h    i    p    "
"S     p     a     c     e     s     h     i     p     "
"S      p      a      c      e      s      h      i      p      "
"S       p       a       c       e       s       h       i       p       "
"S        p        a        c        e        s        h        i        p        "
"S       p       a       c       e       s       h       i       p       "
"S      p      a      c      e      s      h      i      p      "
"S     p     a     c     e     s     h     i     p     "
"S    p    a    c    e    s    h    i    p    "
"S   p   a   c   e   s   h   i   p   "
"S  p  a  c  e  s  h  i  p  "
"S p a c e s h i p "
"Spaceship"

Explanation:

Has trailing whitespace on each line.

,/'(1+a,1_|a:!#x)$\:$x: / the solution
                     x: / save input as x,                 e.g. "abc"
                    $   / string,                          e.g. (,"a";,"b";,"c")
                 $\:    / pad ($) right by each-left (\:)
   (            )       / do this together
              #x        / count length of input,           e.g. 3
             !          / range 0..length,                 e.g. 0 1 2
           a:           / save as a
          |             / reverse it,                      e.g. 2 1 0
        1_              / drop first,                      e.g. 1 0
      a,                / join to a,                       e.g. 0 1 2 1 0
    1+                  / add 1,                           e.g. 1 2 3 2 1
,/'                     / flatten each
\$\endgroup\$
2
\$\begingroup\$

C#, 113 105 98 bytes

s=>{for(int l=s.Length,i=-l;++i<l;)WriteLine(Join("",s.Select(c=>$"{c}".PadRight(i<0?l+i:l-i))));}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Hi there. Currently your answer is a snippet instead of a function or full program. This can cheaply be fixed by adding s=>{ before and } after to make it a lambda-function. In addition, one thing to golf is removing the brackets around the for-loop. Try it online. \$\endgroup\$ Sep 5, 2018 at 11:38
  • \$\begingroup\$ @KevinCruijssen Thanks! Forgot to fix that before posting... \$\endgroup\$
    – RobIII
    Sep 5, 2018 at 11:44
2
\$\begingroup\$

Scala, 82 bytes

for(i<-(0 to a.size)union(-a.size to 0))println(a.map(_+" "*Math.abs(i)).mkString)

Try it online

Scala has lot of shortcuts that are helping me here and that is quite readable! Try Scala

\$\endgroup\$
1
  • \$\begingroup\$ Hi there, welcome to PPCG! Although it's a nice answer, I'm afraid two things are slightly incorrect. The line with the most spaces should only be output once in the middle instead of twice. And currently you print from 0 to length amount of spaces, instead of 0 to length-1 amount of spaces. I don't know Scala too well, but it seem you can fix both issues with +4 bytes (86 bytes in total) like this: for(i<-(0 to a.size-1)union(-a.size+2 to 0))println(a.map(_+" "*Math.abs(i)).mkString) Again welcome, and enjoy your stay! :) \$\endgroup\$ Sep 5, 2018 at 16:53
2
\$\begingroup\$

8086 machine code, 56 53 bytes

00000000  bf 35 01 57 ba 01 00 52  be 82 00 b3 ff ac 59 51  |.5.W...R......YQ|
00000010  aa 3c 0d 74 07 b0 20 e2  f7 43 eb f1 b0 0a aa 59  |.<.t.. ..C.....Y|
00000020  00 d1 e3 08 38 cb d6 08  c2 51 eb dc c6 05 24 5a  |....8....Q....$Z|
00000030  b4 09 cd 21 c3                                    |...!.|
00000035

Assembled from:

org 0x100
use16
        mov di, buffer
        push di
        mov dx, 1
        push dx
nextl:  mov si, 0x82
        mov bl, -1
nextc:  lodsb
        pop cx
        push cx
stor:   stosb
        cmp al, 0x0d
        je cr
        mov al, ' '
        loop stor
        inc bx
        jmp nextc
cr:     mov al, 0x0a
        stosb
        pop cx
        add cl, dl
        jcxz done
        cmp bl, cl
        salc
        or dl, al
        push cx
        jmp nextl
done:   mov [di], byte '$'
        pop dx
        mov ah, 0x09
        int 0x21
        ret
buffer:

Test case:

screenshot

\$\endgroup\$
4
  • \$\begingroup\$ Hi there. I guess there isn't any online compiled for 8086 machine code, but could you perhaps add a screenshot of the output for one of the test cases? Then I can check if everything is correct. :) \$\endgroup\$ Sep 5, 2018 at 16:56
  • \$\begingroup\$ Done. Also shaved off one more byte :) \$\endgroup\$ Sep 5, 2018 at 17:02
  • \$\begingroup\$ Thanks for the screenshot! Unfortunately there is one small mistake in the output. The middle line now has length amount of spaces and there are 9 lines in total, but the middle line should have length-1 amount of spaces and there should be a total of 7 lines instead (for the 4-letter word 'test'`).. :( I hope it's not to expensive in terms of bytes to fix? \$\endgroup\$ Sep 5, 2018 at 17:10
  • 1
    \$\begingroup\$ Ohh I see. That's fixed now, I even reduced it by one more byte. \$\endgroup\$ Sep 5, 2018 at 17:22
2
\$\begingroup\$

Perl 5 + -M5.010 -nlF, 34 bytes

$,=$"x($#F-abs),say@F for-$#F..$#F

Try it online!

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

Haskell, 64 60 59 bytes

(""#)
a#s|l<-(:a)=<<s,w<-' ':a=l:[x|w<(' '<$s),x<-w#s++[l]]

Try it online!

a#s                         -- take a string of spaces 'a' and the input string 's'
 |l<-(:a)=<<s               -- let 'l' be the current line, i.e. the spaces in 'a'
                            -- appended to each char in 's'
  w<-' ':a                  -- let 'w' be 'a' with an additional space   
 =l                         -- return that 'l'
   :[   |w<(' '<$s)   ]     -- and, if 'w' is shorter than 's',
     x  ,x<-w#s++[l]        -- followed by a recursive call with 'w' 
                            -- and by another copy of 'l'

(""#)                       -- start with an empty 'a'
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 43 bytes

{(0....comb-1...0)>>.&{join ' 'x$^a,.comb}}

Try it online!

Returns a list of lines.

Explanation:

 {                                         }  # Anonymous code block
  (0....comb-1...0) # A list from
   0                  # 0
    ...               # to
       .comb-1        # the length of the input string -1
              ...     # back to
                 0    # 0
                   >>.&{                  }  # Map each number to
                        join        ,.comb   # Join the list of characters
                             ' 'x$^a         # With the number of spaces
\$\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.