I done did made a spaceship maw!

39

3

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:
)}/\
) } / \
)  }  /  \
)   }   /   \
)  }  /  \
) } / \
)}/\

Kevin Cruijssen

Posted 2018-09-04T13:49:49.600

Reputation: 67 575

Can we return a list of lines? – Erik the Outgolfer – 2018-09-04T16:17:56.303

@EriktheOutgolfer Sure. – Kevin Cruijssen – 2018-09-04T16:18:24.527

1Gets all just jittery !!!SPACESHIP!!! – WallyWest – 2018-09-05T22:03:28.783

1I knew I recognized that output. I love that this idea is still going. – Carcigenicate – 2018-09-05T23:01:30.150

2TFW you vaguely recognize a pattern in a question ಠ_ಠ then realize it's because you accidentally made it a year ago ಠ⌣ಠ. – Magic Octopus Urn – 2018-09-06T17:25:10.170

1@MagicOctopusUrn Thanks for the inspiration. ;D – Kevin Cruijssen – 2018-09-06T17:26:00.247

3@KevinCruijssen thanks for keeping the goofy quote haha! – Magic Octopus Urn – 2018-09-06T17:26:15.910

Answers

11

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

Shaggy

Posted 2018-09-04T13:49:49.600

Reputation: 24 623

S.ç() FTW once again :-) – ETHproductions – 2018-09-04T16:00:29.767

1Serious question: would it be feasible to search iteratively through all 1 to 6 bytes long solutions for a puzzle like this? – filip – 2018-09-05T07:22:56.327

2@filip No: there are more than 256**6=281474976710656 (at least naive) combinations. It's like guessing a password. – Kirill Bulygin – 2018-09-05T14:44:15.980

@KirillBulygin I guess you need some kind of heuristic to avoid useless paths. Maybe a genetic algorithm. Must research this. – filip – 2018-09-05T14:56:18.280

3@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?! – Shaggy – 2018-09-05T15:46:16.620

Oop, pinged the wrong person in that comment; it was meant for you @filip – Shaggy – 2018-09-05T20:08:11.423

6@Shaggy: "where's the fun in letting a bot do your golfing for you?!" What if you made the bot really, really short? – Oddthinking – 2018-09-07T02:17:29.793

11

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!

J.Doe

Posted 2018-09-04T13:49:49.600

Reputation: 2 379

You can golf 1 byte by removing the space at in(r<-. – Kevin Cruijssen – 2018-09-04T17:01:51.150

you can also swap the <- to = in the statement Kevin mentions – Giuseppe – 2018-09-04T17:04:44.283

1

and you only use s once so you can just use it as an argument in write directly; bringing you down to 99 bytes

– Giuseppe – 2018-09-04T17:08:10.630

1should that be a 1 rather than a "" in write? I'm digging your use of Map! – Giuseppe – 2018-09-04T17:57:08.807

Ah yeah, thanks! write has a whole lot of parameters to misremember. – J.Doe – 2018-09-04T17:58:42.900

1

Awesome ! 79 bytes using math :)

– digEmAll – 2018-09-05T07:27:04.973

Great solution! – J.Doe – 2018-09-05T08:19:40.513

1I was convinced I could beat this with the collapse argument to paste but it just isn't happening... – JDL – 2018-09-06T16:21:48.610

A lookbehind Map(gsub,"(?<=.)",strrep(" ",n-abs(n:-n)),w,F,T) nearly works for 72 bytes. Unfortunately I don't know how to cat out a list in <7 bytes... – J.Doe – 2018-09-07T08:25:03.030

since IO is flexible you can probably just return a list of lines and do trimws(Map(gsub,"",strrep(" ",n-abs(n:-n)),w)) for 70 bytes. See for instance the two K submissions. – JayCe – 2018-09-07T16:01:43.907

6

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

Emigna

Posted 2018-09-04T13:49:49.600

Reputation: 50 798

You can drop the S. Input is flexible, so inputting as list is allowed. – Kevin Cruijssen – 2018-09-04T14:26:57.907

Tried to find something shorter, but I've only been able to find this 10-byte alternative εINúíJ}».∊ and some 11-byters like āεIsjíJ}».∊.

– Kevin Cruijssen – 2018-09-04T15:03:06.007

@KevinCruijssen: Oh yeah, this is 10 bytes as well with input as a list of chars. Was in too much of a hurry for my train that I didn't really read the fine print :) – Emigna – 2018-09-04T16:12:56.057

1@KevinCruijssen: Your first version is 9 bytes in legacy though, as » can be omitted. – Emigna – 2018-09-04T16:22:24.290

Ah, good point. Hmm, will post it as a separate answer in that case, unless you want to add both to your answer? Either is fine by me. – Kevin Cruijssen – 2018-09-04T16:52:27.260

1Yours actually works in the legacy in 9 bytes as well if you remove » and change the loop v to a map ε. – Kevin Cruijssen – 2018-09-04T17:04:58.923

@KevinCruijssen: I'd say yours is sufficiently different to post separately. And if you do yours in legacy we can aim for the shortest in each :) – Emigna – 2018-09-04T20:15:57.517

1Posted 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)? :) – Kevin Cruijssen – 2018-09-04T21:09:50.067

2Does εINð×ý}û» also work? – Adnan – 2018-09-04T21:20:11.720

@Adnan: Doh! How did I not think of û. I've used it plenty of times, while I've never used .∊ before. Thanks :) – Emigna – 2018-09-05T05:50:49.593

@KevinCruijssen: Yes, that's what I meant. I very much doubt there is anything we can do to beat Japt here ;) – Emigna – 2018-09-05T05:54:08.770

gÝûvyúJ, I done did made a fancier spaceship maw! And I was all excited about suggesting a 7-byte edit. (Sorry, it had to come full circle) – Magic Octopus Urn – 2018-09-06T21:47:58.073

6

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!

Rod

Posted 2018-09-04T13:49:49.600

Reputation: 17 588

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. – Kevin Cruijssen – 2018-09-04T14:13:44.630

6

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!

Arnauld

Posted 2018-09-04T13:49:49.600

Reputation: 111 334

1tried it with a string from the answer to RegEx match open tags except XHTML self-contained tags and the output looks interesting – Tom M – 2018-09-20T12:48:20.257

6

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

Neil

Posted 2018-09-04T13:49:49.600

Reputation: 95 035

Charcoal doesn't reflect things like [ to ]-- or is that another separate command? – Magic Octopus Urn – 2018-09-06T21:50:09.140

@MagicOctopusUrn There are separate commands if you want to transform the reflection. See https://codegolf.stackexchange.com/a/127164 for example.

– Neil – 2018-09-06T22:19:22.687

I thought I had seen it reflect before, but wasn't sure. Neat! – Magic Octopus Urn – 2018-09-06T22:23:09.703

6

Canvas, 8 bytes

┐² ×*]──

Try it here!

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

dzaima

Posted 2018-09-04T13:49:49.600

Reputation: 19 048

I like your 7 byte version. Would you be willing to explain how it works? Which operator overlaps the two /s into a X? – Kaya – 2018-09-04T21:31:02.883

3

@Kaya it's the last character - - vertical palindromize. The palindromization of Canvas does cool things. The overlapping part has it's own character too.

– dzaima – 2018-09-04T21:37:02.203

5

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.

Kirill L.

Posted 2018-09-04T13:49:49.600

Reputation: 6 693

4

Japt, 9 8 bytes

-1 byte from @Shaggy

ÊƬqXîÃê

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

Try it online!

Luis felipe De jesus Munoz

Posted 2018-09-04T13:49:49.600

Reputation: 9 639

Dang; looks like you ninjaed me again! Lemme know if you'd like me to delete mine. – Shaggy – 2018-09-04T14:19:41.797

1@Shaggy no, keep your answer, you are using array as input while I use a string so they are kind of different xD – Luis felipe De jesus Munoz – 2018-09-04T14:20:37.880

1SpX -> for a 1 byte saving. – Shaggy – 2018-09-04T14:30:10.403

4

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.

AdmBorkBork

Posted 2018-09-04T13:49:49.600

Reputation: 41 581

Try this: 0..($x=($a=$args).count-1)+$x..0|gu|%{$a-join(' '*$_)} – mazzy – 2018-09-05T13:25:24.337

1@mazzy What the ... how does Get-Unique work like that on the range? That's crazy! Thanks! – AdmBorkBork – 2018-09-05T13:50:07.203

4

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"

Kevin Cruijssen

Posted 2018-09-04T13:49:49.600

Reputation: 67 575

1Oooooo, 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) – Magic Octopus Urn – 2018-09-06T21:46:39.217

@MagicOctopusUrn Thanks for letting me know. Fixed it by using û» instead of .∊. – Kevin Cruijssen – 2018-09-07T07:07:37.247

4

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

Giuseppe

Posted 2018-09-04T13:49:49.600

Reputation: 21 077

4

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

ბიმო

Posted 2018-09-04T13:49:49.600

Reputation: 15 345

3

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!

Olivier Grégoire

Posted 2018-09-04T13:49:49.600

Reputation: 10 647

1l-Math.abs(i) can be golfed to i<0?l+i:l-i for -2 bytes. – Kevin Cruijssen – 2018-09-04T14:42:06.560

@KevinCruijssen Thanks! :) – Olivier Grégoire – 2018-09-04T14:44:15.650

could it be l-i<0?-i:i? – Quintec – 2018-09-04T18:06:50.760

@thecoder16 You would need parentheses: l-(i<0?-i:i) (12 bytes). – Jonathan Frech – 2018-09-04T18:22:56.463

3

Jelly, 9 bytes

jⱮLḶ⁶ẋƲŒḄ

Try it online!

Returns a list of lines; output prettified over TIO.

Erik the Outgolfer

Posted 2018-09-04T13:49:49.600

Reputation: 38 134

A somewhat different 9: ,€⁶$LСŒḄ. Other, more similar, 9's: J’⁶ẋŒḄɓjⱮ and J’⁶ẋŒḄjⱮ@ (I was looking for shorter but no joy yet) – Jonathan Allan – 2018-09-04T16:33:30.517

@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. :-) – Erik the Outgolfer – 2018-09-04T16:35:25.827

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. – Jonathan Allan – 2018-09-04T16:37:37.213

@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... – Erik the Outgolfer – 2018-09-04T16:39:39.873

I said I outgolfed 05AB1E, eh? Bah, not anymore. :/ – Erik the Outgolfer – 2018-09-05T18:00:40.683

3

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!

Mr. Xcoder

Posted 2018-09-04T13:49:49.600

Reputation: 39 774

3

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

streetster

Posted 2018-09-04T13:49:49.600

Reputation: 3 635

1,:' -> +,­­ – ngn – 2018-09-14T10:15:29.367

3

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

wastl

Posted 2018-09-04T13:49:49.600

Reputation: 3 089

2

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...

AlexRacer

Posted 2018-09-04T13:49:49.600

Reputation: 979

2

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.

Titus

Posted 2018-09-04T13:49:49.600

Reputation: 13 814

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. – Kevin Cruijssen – 2018-09-04T19:06:46.453

1@KevinCruijssen Didn´t look at the output close enough. Fixed. (Though imo my previous output looked more like a rocket) ;-) – Titus – 2018-09-04T19:34:07.460

It kinda looked like the head of an arrow to me. :D But +1 now that it's fixed. – Kevin Cruijssen – 2018-09-04T20:53:09.150

If you place an actual newline instead of the \n, you save a byte :) – Martijn – 2018-09-06T14:22:15.223

@Martijn I actually did only count one byte for it ... forgot to replace it when I pasted the code here. – Titus – 2018-09-06T22:26:27.133

@Titus - I like the bitwise not ~ I always forget about that one (and the new line). I may steal your loop, by the way. But not for this puzzle, I did one with 144 but I wrapped it in a function that's not required? B4 Someone told me i had to. I used chunk_split and str_repeat – ArtisticPhoenix – 2018-09-06T23:47:02.433

2

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

streetster

Posted 2018-09-04T13:49:49.600

Reputation: 3 635

2

C (gcc), 131 129 111 bytes

i;x;f(k,j)char*k,*j;{x=strlen(k)-1;for(i=0;i<x-~x;i+=puts(""))for(j=k;*j;)printf("%c%*s",*j++,i<x?i:2*x-i,"");}

Try it online!

-20 bytes thanks to ceilingcat!

#import<string.h>
i;x;f(k,j)char*k,*j;{x=strlen(k)-1;for(i=0;i<x-~x;i+=puts(""))for(j=k;*j;)printf("%c%*s",*j++,i<x?i:2*x-i,"");}

Try it online!

Or, if length can be accepted as a parameter:

C (gcc), 105 102 bytes

-1 byte thanks to ceilingcat!

i;x;f(k,x,j)char*k,*j;{for(i=!x--;i<x-~x;i+=puts(""))for(j=k;*j;)printf("%c%*s",*j++,i<x?i:2*x-i,"");}

Try it online!

Conor O'Brien

Posted 2018-09-04T13:49:49.600

Reputation: 36 228

@ceilingcat huh! – Conor O'Brien – 2018-09-14T02:05:20.467

1In your 102 bytes long golf, I think the global x is shadowed and thus redundantly declared. – Jonathan Frech – 2018-09-19T22:50:16.383

2

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!

RobIII

Posted 2018-09-04T13:49:49.600

Reputation: 397

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.

– Kevin Cruijssen – 2018-09-05T11:38:45.250

@KevinCruijssen Thanks! Forgot to fix that before posting... – RobIII – 2018-09-05T11:44:45.927

2

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

Andrew Kuleshov

Posted 2018-09-04T13:49:49.600

Reputation: 21

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! :)

– Kevin Cruijssen – 2018-09-05T16:53:57.963

2

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

user5434231

Posted 2018-09-04T13:49:49.600

Reputation: 1 576

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. :) – Kevin Cruijssen – 2018-09-05T16:56:33.993

Done. Also shaved off one more byte :) – user5434231 – 2018-09-05T17:02:49.320

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? – Kevin Cruijssen – 2018-09-05T17:10:30.033

1Ohh I see. That's fixed now, I even reduced it by one more byte. – user5434231 – 2018-09-05T17:22:36.333

2

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 |

MT0

Posted 2018-09-04T13:49:49.600

Reputation: 3 373

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).

– Kevin Cruijssen – 2018-09-06T13:11:59.363

1@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. – MT0 – 2018-09-06T13:21:34.250

Thanks. It indeed looks good in that case! – Kevin Cruijssen – 2018-09-06T13:23:29.357

2

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'

nimi

Posted 2018-09-04T13:49:49.600

Reputation: 34 639

2

PHP, 148 146 143 141 Bytes

function s($s){for(;$i<strlen($s);++$i)f($i,$s);for(--$i;--$i>=0;)f($i,$s);}function f($i,$s){echo chunk_split($s,1,str_repeat(' ',$i))."
";}

You can test it like this:

<?php
error_reporting(0);

$s = 1234567890;
function s($s){for(;$i<strlen($s);++$i)f($i,$s);for(--$i;--$i>=0;)f($i,$s);}function f($i,$s){echo chunk_split($s,1,str_repeat(' ',$i))."
";}

Output

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

Sandbox

Expanded version

 function s($s){
    //loop upwards 0-10
    for(;$i<strlen($s);++$i) f($i,$s);
     //decrement so it's odd, from 9 loop downwards to 0
    for(--$i;--$i>=0;)f($i,$s);
 }
 //2nd function to save space
 function f($i,$s){
     //chunk it, split 1 char, insert $i number of spaces
     echo chunk_split($s,1,str_repeat(' ',$i))."
";}

Attempt 2, 92 bytes

after seeing @Titus answer I reduced mine to this:

for(;++$i<2*$e=strlen($s=$argn);)echo chunk_split($s,1,str_repeat(' ',~-$e-abs($i-$e)))."
";

I was trying to think of a way to use 1 loop, instead of 2... Believe it or not, I almost never use the for loop in "real" code. It was the ~ bitwise Not, that I was missing...

It's sill a tiny bit longer at 92 so I don't feel so bad. But I will put it in as a second attempt anyway.

$argn is the input from the command line

Run as pipe with -nR or try it online.

Sandbox

ArtisticPhoenix

Posted 2018-09-04T13:49:49.600

Reputation: 329

It seems to me that your first version would be 4 bytes shorter if you used a named function f instead of assigning an anonymous one to $f - function f( saves 2 bytes over $f=function(, and you save another byte every time you call f(...) instead of $f(...). Alternatively, you could capture the $s, saving 2 bytes - ($i)use($s) is 4 bytes longer than ($i,$s), but you save 3 bytes for each call to $f($i) instead of $f($s,$i); in languages with automatic capture, like JS, this is more often a viable saving, because you don't pay the penalty of the use statement. – IMSoP – 2018-09-07T16:12:53.407

I do appreciate the help, I'm pretty new to code golf and don't really do it that much, one does get bored on the regular SO pages, though. I did think about using use but it feels longer, to do that sense $i is dynamic, it would have to be passed in by reference. So it has to be use(&$i) and the $i has to be defined before passing it by reference to $f. Which means setting it in the parent function, or before any other. For the function it could be function s($s,$i) and just know it has to be called with s($s,0) but it seems ugly, and that is around 11 bytes, use(&$i),$i – ArtisticPhoenix – 2018-09-07T17:14:40.167

We could use $s though and change the \n to a real line return. That gets it to 143 2 from the line ending and 1 from use – ArtisticPhoenix – 2018-09-07T17:31:20.143

Yes, I hadn't even considered putting $i into the use, because $s felt the "natural" capture, but it's always worth calculating the net saving. However, you can still save 2 bytes by just declaring a named function f instead of a closure: function s($s){for(;$i<strlen($s);++$i)f($i,$s);for(--$i;--$i>=0;)f($i,$s);}function f($i,$s){echo chunk_split($s,1,str_repeat(' ',$i))." ";} Extra functions like this are allowed according to this meta post: https://codegolf.meta.stackexchange.com/questions/7614/what-are-our-rules-about-additional-code-accompanying-function-submissions

– IMSoP – 2018-09-08T09:06:53.933

Updated, saved a couple – ArtisticPhoenix – 2018-09-08T11:27:13.037

I think your code block misses three bytes in your post's second line. – Jonathan Frech – 2018-09-19T22:51:57.350

That was just a display issue in the post because of the line return. Using a physical line return instead of "\n", saves 2 bytes (because you don't need the \ or the n) but because SO needs 4 leading spaces for a code block, I omitted those 4 leading spaces from the second line. So the line return on here actually is \n\s\s\s\s so it will display as part of the code block, but the actual code doesn't need the spaces. In other words, it is 141 bytes including the "}; that was outside of the code bock on the post. – ArtisticPhoenix – 2018-09-20T18:32:03.417

2

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

-5 & -3 thanks to Kevin Cruissen

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

Try it online!


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

agc

Posted 2018-09-04T13:49:49.600

Reputation: 141

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?

– Kevin Cruijssen – 2018-09-07T13:29:37.267

1@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. – agc – 2018-09-07T13:47:07.543

Thanks. And a single test case for TIO is fine. It's mainly to verify if everything is working as expected, which indeed seems the case. +1 from me. :) – Kevin Cruijssen – 2018-09-07T13:49:24.133

1

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.

– Kevin Cruijssen – 2018-09-07T14:31:29.580

1Oh, 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. – Kevin Cruijssen – 2018-09-07T14:49:54.137

2

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

Jo King

Posted 2018-09-04T13:49:49.600

Reputation: 38 234

1

Python 2, 67 bytes

s=input()
r=range(len(s))
for i in r+r[-2::-1]:print(' '*i).join(s)

Try it online!

TFeld

Posted 2018-09-04T13:49:49.600

Reputation: 19 246

1

C (gcc), 135 133 129 112 bytes

#define L{for(k=0;s[k];)printf("%c%*s",s[k++],i,"");puts("");}
i;k;f(char*s){for(i=0;s[i];i++)L for(--i;~--i;)L}

Try it online!

-2 bytes from Kevin Cruijssen

-4 bytes from ceilingcat

-17 bytes from ceilingcat

Ungolfed:

int i, k;
int f(char *s){
    for(i = 0; s[i] != '\x00'; i++) {
        for(k = 0; s[k] != '\x00'; k++) {
            printf("%c%*s", s[k], i, "");
            // print the character followed by the empty string padded with i spaces
        }
        putchar('\n');
    }
    for(i = i-2; i > -1; --i) {
        for(k = 0; s[k] != '\x00'; k++) {
            printf("%c%*s", s[k], i, "");
            // print the character followed by the empty string padded with i spaces
        }
        putchar('\n');
    }
}

pizzapants184

Posted 2018-09-04T13:49:49.600

Reputation: 3 174

1You can golf 2 bytes by changing putchar(s[k]);for(j=0; to for(putchar(s[k]),j=0; so the brackets around the outer for-loop can be removed. – Kevin Cruijssen – 2018-09-04T16:56:59.913

108 bytes – ceilingcat – 2018-12-22T00:38:49.417

1

Red, 86 bytes

func[s][repeat n l: 2 *(length? s)- 1[foreach c s[prin pad c min n l - n + 1]print""]]

Try it online!

Galen Ivanov

Posted 2018-09-04T13:49:49.600

Reputation: 13 815

1

Clean, 87 81 bytes

-2 thanks to Kevin Cruijssen

import StdEnv,Text
$s#l=length s
=[concat[rpad{c}(l-abs i)' '\\c<-s]\\i<-[~l..l]]

Try it online!

Οurous

Posted 2018-09-04T13:49:49.600

Reputation: 7 916

Hi there. I'm afraid the output is slightly incorrect. Currently all lines also have leading spaces before the first character. The first character should all be in the same column, and the spaces should only be in between the characters (with optionally trailing spaces). – Kevin Cruijssen – 2018-09-05T09:27:22.920

@KevinCruijssen fixed. It was a typo lpad -> rpad – Οurous – 2018-09-05T20:29:08.243

Hi. I only now notice one more mistake a lot of other answers had too at first. Your middle line has length amount of spaces instead of length-1. For the 4-letter word 'test' for example, there should be 7 output lines instead of 9. You can fix that, and save 2 bytes at the same time, by removing the +1 it seems. :) – Kevin Cruijssen – 2018-09-06T07:06:27.907

1@KevinCruijssen Fixed, and thanks! – Οurous – 2018-09-06T07:44:47.213

1

Attache, 26 bytes

Bounce@{Join[_,Iota@_*sp]}

Try it online!

Explanation

Bounce@{Join[_,Iota@_*sp]}

       {                 }    anonymous lambda. input: _ (character array)
        Join[_,         ]     join each character by (vectorizes)
               Iota@_           the indices of _ (0 ... #_)
                     *sp        repeat a space by each index
Bounce@                       bounce the results (append reverse, but middle only once)

Conor O'Brien

Posted 2018-09-04T13:49:49.600

Reputation: 36 228

1

Brainbash, 142 bytes

-[-[-<]>>+<]>-<++++++++++>>*,+[~+~->,+]~[-*[.~>[-<<.>>>+<]>[<->+]<<~>]~>+<<<.>>]>--[->+<<+>]>[-<+>]<<+[-*[.~>[-<<.>>>+<]>[<->+]<<~>]~>-<<<.>>]

Try it online!

In one block:

-[-[-<]>>+<]>-<++++++++++>>*,+[~+~->,+]~[-*[.~>[-<<.>>>+<]>[<->+]<<~>]~
>+<<<.>>]>--[->+<<+>]>[-<+>]<<+[-*[.~>[-<<.>>>+<]>[<->+]<<~>]~>-<<<.>>]

Explanation

-[-[-<]>>+<]>-          constant for 32
<++++++++++             constant for 10
>>                      move to counter
*                       swap tapes while retaining pointer
,+[~+~->,+]             take input in tape while keeping track of how many characters
~                       move to counter on other tape
[                       repeat counter times:
    -                       update counter
    *                       swap tapes
    [                       for each character:
        .                       output it
        ~>[                     repeat secondary counter times:
            -                       update secondary counter
            <<.                     output a space
            >>>+<                   make a copy of the secondary counter
        ]
        >[<->+]                 restore secondary counter    
        <<~>                return to counter
    ]
    ~>+<<<.>>           update ternary counter and output a newline
]

>--                     fix offset

[->+<<+>]>[-<+>]<<+     various counter restoring

    [-*[.~>[-<
    <.>>>+<]>[
    <->+]<<~>]
    ~>-<<<.>>]          same as above but subtracting instead of adding

Conor O'Brien

Posted 2018-09-04T13:49:49.600

Reputation: 36 228

1

T-SQL, 215 214 bytes

DECLARE @v CHAR(32),@p VARCHAR(999),@ INT,@j INT,@k INT
SELECT @v=v,@=LEN(v),@j=-@+1FROM t
r:SET @p=''SET @k=1c:SET @p+=SUBSTRING(@v,@k,1)+SPACE(@+~ABS(@j))SET @k+=1
IF @k!>@ GOTO c
PRINT @p
SET @j+=1IF @j<@ GOTO r

Per our IO rules, input is taken from a pre-existing table t with varchar field v. Max size I've allowed is 32 characters, which tops out with the middle line being 32*31=992 characters long.

I've used a pure brute force nested-loop (using GOTO which is shorter than WHILE) with manual string construction, not very SQL-like at all. Still the best I could do (MS T-SQL has no native support for REGEX, except using CLR assemblies and manually importing System.Text.RegularExpressions).

EDIT: Here's the output for input "test":

test
t e s t 
t  e  s  t  
t   e   s   t   
t  e  s  t  
t e s t 
test

And here's a partial screenshot of the output for input "abcdefghijklmnopqrstuvwxyz":

Screenshot of output for 26-character input

BradC

Posted 2018-09-04T13:49:49.600

Reputation: 6 099

Hi there. Do you perhaps have a screenshot for one of the test cases (like 'test' for example) so I could verify everything is correct with the output? – Kevin Cruijssen – 2018-09-07T07:41:50.453

@KevinCruijssen Added two sample outputs. – BradC – 2018-09-07T13:45:57.290

Thanks, that seems to work great. +1 from me. :) – Kevin Cruijssen – 2018-09-07T13:51:07.670

1

Also, I barely know anything about T-SQL, but is it possible to golf @-ABS(@j)-1 to @+~ABS(@j)? I saw that T-SQL has a binary-not ~, but not sure if it works in the same way as Java/JavaScript/Python/etc. regarding this tip.

– Kevin Cruijssen – 2018-09-07T13:54:45.970

1@KevinCruijssen Actually, that does work! Down to 214 bytes! – BradC – 2018-09-07T14:32:30.760

1

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

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

Try it online!

Dom Hastings

Posted 2018-09-04T13:49:49.600

Reputation: 16 415

1

Pip -l, 14 9 bytes

aJsXPZ,#a

Takes the input string as a command-line argument. Try it online!

Note: the PZ operator is newer than this question.

Explanation

WIth example input abc:

           a is 1st cmdline arg; s is space (implicit)
      ,#a  Range(length(a))
           [0; 1; 2]
    PZ     Palindromize (append the reverse, but without doubling the center item)
           [0; 1; 2; 1; 0]
  sX       Repeat space that many times (vectorized)
           [""; " "; "  "; " "; ""]
aJ         Join a on those strings (vectorized)
           ["abc"; "a b c"; "a  b  c"; "a b c"; "abc"]
           Print (implicit)
           Each list element is printed on its own line, due to -l flag

DLosc

Posted 2018-09-04T13:49:49.600

Reputation: 21 213

1

JavaScript (Node.js), 102 100 98 96 bytes

f=(s,n=s.length,m=n+--n)=>[...Array(m)].map((_,i)=>[...s].join(` `.repeat(i<n?i:2*n-i))).join`

-2 by using [...]
-2 from @Kevin
-2 by tweak @Kevin

Try it online!

user58120

Posted 2018-09-04T13:49:49.600

Reputation:

1Hi there! I'm afraid it's slightly incorrect. With input 'test' for example you are outputting 9 lines instead of 7. The middle longest line should have length-1 amount of spaces as delimiter, but currently your middle longest line has length amount of spaces as delimiter. – Kevin Cruijssen – 2018-09-14T10:55:17.710

changed from J to JS – None – 2018-09-20T10:25:46.750

1You can golf n-Math.abs(i-n) to i-n<0?i:2*n-i for -2 bytes. – Kevin Cruijssen – 2018-09-20T11:00:56.267

2

Welcome to PPCG! Here's a 74 byte version of your solution, taking input as an array of characters and outputting an array of lines: https://tio.run/##HcxBDsIgEADAux/prpY9eDRS48EXeGwa2QCtNHQhtDHx4tfR@ICZmV@82hLypiQ5X0ddWXe96I9iil6m7dkS0bUUfoMcBAdaOAM82oC6Y5pTEDCGMrubOAhnuYTTcS8qIGK1SdYUPcU0wQj9L2ruma1vBvxLszNYvw

– Shaggy – 2018-09-20T12:30:53.420

@Shaggy Nice! but taking and returning array feels incomplete solution to me – None – 2018-09-20T12:35:28.847

2It's allowed by the challenge spec: "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." – Shaggy – 2018-09-20T12:36:33.573

0

Lua: 132 characters

a=io.read()s=' 'l=function(o,n,z)for j=o,n,z do c=''for _,i in ipairs(a)do 
c=c..i..s:rep(j)end print(c)end end l(0,#a,1)l(#a-1,0,-1)

Long form:

a=io.read() -- using the input as a table, lua's version of arrays (and 
-- using it like an array anyway) Not 100% sure this is how the input goes
space=' ' -- need to declare this it seems so we can repeat it a number of times 
later
l = function(initialNumSpaces,maxNumSpaces,increment) 
for j=initialNumSpaces, maxNumSpaces, increment do 
  collector=''
  for _,i in ipairs(a)do  -- loop through the inputted string char by char
     collector=collector..i..space:rep(j) -- concat the char and spaces
  end 
  print(collector) -- print the collector
end 
end
l(0,#a,1) -- call the first half of the spaceship
l(#a-1,0,-1) -- call the second half of the spaceship

Dolgubon

Posted 2018-09-04T13:49:49.600

Reputation: 1

1

Hi there, welcome to PPCG! Would it be possible add a TryItOnline-link with test code for your Lua program/function? (PS: You can add a trailing # in front of your "Lua: 132 characters" to make it a title like most of the other answers. :)

– Kevin Cruijssen – 2018-09-14T08:06:13.803