Introduction:
Inspired by this comment of @MagicOctopusUrn on @Emigna's 05AB1E answer for my "It was just a bug" challenge:
×ばつý}».∊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
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 code-golf, 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:
)}/\
) } / \
) } / \
) } / \
) } / \
) } / \
)}/\
49 Answers 49
Japt, (削除) 8 (削除ここまで) 6 bytes
Takes input as an array of characters, outputs an array of strings.
£qYçÃê
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ÄÃê
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
-
\$\begingroup\$
S.ç()FTW once again :-) \$\endgroup\$ETHproductions– ETHproductions2018年09月04日 16:00:29 +00:00Commented 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– filip2018年09月05日 07:22:56 +00:00Commented 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\$Kirill Bulygin– Kirill Bulygin2018年09月05日 14:44:15 +00:00Commented 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– Shaggy2018年09月05日 15:46:16 +00:00Commented 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\$Oddthinking– Oddthinking2018年09月07日 02:17:29 +00:00Commented Sep 7, 2018 at 2:17
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)
-
\$\begingroup\$ You can golf 1 byte by removing the space at
in(r<-. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 17:01:51 +00:00Commented Sep 4, 2018 at 17:01 -
1
-
1\$\begingroup\$ should that be a
1rather than a""inwrite? I'm digging your use ofMap! \$\endgroup\$Giuseppe– Giuseppe2018年09月04日 17:57:08 +00:00Commented Sep 4, 2018 at 17:57 -
1\$\begingroup\$ Awesome ! 79 bytes using math :) \$\endgroup\$digEmAll– digEmAll2018年09月05日 07:27:04 +00:00Commented Sep 5, 2018 at 7:27
-
1\$\begingroup\$ I was convinced I could beat this with the
collapseargument topastebut it just isn't happening... \$\endgroup\$JDL– JDL2018年09月06日 16:21:48 +00:00Commented Sep 6, 2018 at 16:21
05AB1E, (削除) 10 (削除ここまで) 9 bytes
Saved 1 bytes thanks to Adnan
×ばつý}û»
Explanation
ε # apply to each in input
I # push the input
×ばつ # push <index> spaces
ý # merge the input on the spaces
} # end loop
û # palendromize
» # join on newlines
-
\$\begingroup\$ You can drop the
S. Input is flexible, so inputting as list is allowed. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 14:26:57 +00:00Commented Sep 4, 2018 at 14:26 -
1\$\begingroup\$ @KevinCruijssen: Your first version is 9 bytes in legacy though, as
»can be omitted. \$\endgroup\$Emigna– Emigna2018年09月04日 16:22:24 +00:00Commented 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 loopvto a mapε. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 17:04:58 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 21:09:50 +00:00Commented Sep 4, 2018 at 21:09
-
2\$\begingroup\$ Does
εINð×ý}û»also work? \$\endgroup\$Adnan– Adnan2018年09月04日 21:20:11 +00:00Commented Sep 4, 2018 at 21:20
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
-
1\$\begingroup\$ tried it with a string from the answer to RegEx match open tags except XHTML self-contained tags and the output looks interesting \$\endgroup\$Tom M– Tom M2018年09月20日 12:48:20 +00:00Commented Sep 20, 2018 at 12:48
Charcoal, 10 bytes
×ばつ κ‖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
-
\$\begingroup\$ Charcoal doesn't reflect things like
[to]-- or is that another separate command? \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年09月06日 21:50:09 +00:00Commented 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– Neil2018年09月06日 22:19:22 +00:00Commented Sep 6, 2018 at 22:19
-
\$\begingroup\$ I thought I had seen it reflect before, but wasn't sure. Neat! \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年09月06日 22:23:09 +00:00Commented Sep 6, 2018 at 22:23
-
\$\begingroup\$ I like your 7 byte version. Would you be willing to explain how it works? Which operator overlaps the two
/s into aX? \$\endgroup\$Kaya– Kaya2018年09月04日 21:31:02 +00:00Commented 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– dzaima2018年09月04日 21:37:02 +00:00Commented Sep 4, 2018 at 21:37
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
-
\$\begingroup\$
c==len(w)-1can be golfed by 1 byte withc+2>len(w). EDIT: In your new 70-byte version,0<ccan bec. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 14:13:44 +00:00Commented Sep 4, 2018 at 14:13
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"
-
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\$Magic Octopus Urn– Magic Octopus Urn2018年09月06日 21:46:39 +00:00Commented Sep 6, 2018 at 21:46 -
\$\begingroup\$ @MagicOctopusUrn Thanks for letting me know. Fixed it by using
û»instead of.∊. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月07日 07:07:37 +00:00Commented Sep 7, 2018 at 7:07
Haskell, (削除) 60 (削除ここまで) 59 bytes
(init<>reverse).(scanl(?)<*>tail)
a?_=do u<-a;u:[' '|' '<u]
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
Vyxal j, 5 bytes
ż∞v↲∩
Try it Online! -5 bytes thanks to lyxal.
ż # range from 1 to length of input
∞ # Palindromised
v↲ # For each character of the input, pad left by each of the above
∩ # Transpose
# (j flag formats list of lines as a string)
-
\$\begingroup\$ Try it Online! for 6 bytes, almost 2 years later \$\endgroup\$2023年07月22日 08:19:13 +00:00Commented Jul 22, 2023 at 8:19
-
\$\begingroup\$ @lyxal Found a 5 \$\endgroup\$emanresu A– emanresu A2023年07月22日 10:09:21 +00:00Commented Jul 22, 2023 at 10:09
-
\$\begingroup\$ Ooh clever. Wouldn't have thought of using transpose \$\endgroup\$2023年07月22日 10:30:27 +00:00Commented Jul 22, 2023 at 10:30
PowerShell, (削除) 66 (削除ここまで) 54 bytes
-12 bytes thanks to mazzy
0..($x=($a=$args).count-1)+$x..0|gu|%{$a-join(' '*$_)}
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.
-
\$\begingroup\$ Try this:
0..($x=($a=$args).count-1)+$x..0|gu|%{$a-join(' '*$_)}\$\endgroup\$mazzy– mazzy2018年09月05日 13:25:24 +00:00Commented Sep 5, 2018 at 13:25 -
1\$\begingroup\$ @mazzy What the ... how does
Get-Uniquework like that on the range? That's crazy! Thanks! \$\endgroup\$AdmBorkBork– AdmBorkBork2018年09月05日 13:50:07 +00:00Commented Sep 5, 2018 at 13:50
MATL, (削除) 25 (削除ここまで) (削除) 22 (削除ここまで) 13 bytes
zZv"Gtz@he!1e
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
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
-
\$\begingroup\$ Dang; looks like you ninjaed me again! Lemme know if you'd like me to delete mine. \$\endgroup\$Shaggy– Shaggy2018年09月04日 14:19:41 +00:00Commented 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\$Luis felipe De jesus Munoz– Luis felipe De jesus Munoz2018年09月04日 14:20:37 +00:00Commented Sep 4, 2018 at 14:20
-
1\$\begingroup\$
SpX->Xîfor a 1 byte saving. \$\endgroup\$Shaggy– Shaggy2018年09月04日 14:30:10 +00:00Commented Sep 4, 2018 at 14:30
-
\$\begingroup\$ A somewhat different 9:
,6ドル$LСŒḄ. Other, more similar, 9's:J’6ẋŒḄɓjⱮandJ’6ẋŒḄjⱮ@(I was looking for shorter but no joy yet) \$\endgroup\$Jonathan Allan– Jonathan Allan2018年09月04日 16:33:30 +00:00Commented 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Ḷ6ẋorŒḄ. However, if you manage to find a save, do ping me. :-) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年09月04日 16:35:25 +00:00Commented Sep 4, 2018 at 16:35 -
\$\begingroup\$ Thinking about it a bit my
,6ドル$LСŒḄmay not be valid since it has crazy nesting so might need aYand be a full program. \$\endgroup\$Jonathan Allan– Jonathan Allan2018年09月04日 16:37:37 +00:00Commented 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\$Erik the Outgolfer– Erik the Outgolfer2018年09月04日 16:39:39 +00:00Commented Sep 4, 2018 at 16:39 -
\$\begingroup\$ I said I outgolfed 05AB1E, eh? Bah, not anymore. :/ \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年09月05日 18:00:40 +00:00Commented Sep 5, 2018 at 18:00
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
+P_=jRQ_.e*d
Stax, 10 bytes
Ç·9ƒù▌╘Ä┘e
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
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(""));}
- -2 bytes thanks to Kevin Cruijssen
-
1\$\begingroup\$
l-Math.abs(i)can be golfed toi<0?l+i:l-ifor -2 bytes. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 14:42:06 +00:00Commented Sep 4, 2018 at 14:42 -
\$\begingroup\$ @KevinCruijssen Thanks! :) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2018年09月04日 14:44:15 +00:00Commented Sep 4, 2018 at 14:44
-
\$\begingroup\$ could it be
l-i<0?-i:i? \$\endgroup\$Quintec– Quintec2018年09月04日 18:06:50 +00:00Commented Sep 4, 2018 at 18:06 -
\$\begingroup\$ @thecoder16 You would need parentheses:
l-(i<0?-i:i)(12 bytes). \$\endgroup\$Jonathan Frech– Jonathan Frech2018年09月04日 18:22:56 +00:00Commented Sep 4, 2018 at 18:22
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:
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 |
-
\$\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 using0円instead of1円like you could in Java for example? EDIT: Never mind, that is for0ドル, not0円.. (Java example of what I meant). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月06日 13:11:59 +00:00Commented 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– MT02018年09月06日 13:21:34 +00:00Commented Sep 6, 2018 at 13:21
-
\$\begingroup\$ Thanks. It indeed looks good in that case! \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月06日 13:23:29 +00:00Commented Sep 6, 2018 at 13:23
Perl 6, 43 bytes
{(0....comb-1...0)>>.&{join ' 'x$^a,.comb}}
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
K (oK), (削除) 25 (削除ここまで) 24 bytes
Solution:
,/'(1+a,1_|a:!#x)$\:+,x:
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
-
1\$\begingroup\$
,:'->+, \$\endgroup\$ngn– ngn2018年09月14日 10:15:29 +00:00Commented Sep 14, 2018 at 10:15
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
}
---
Note that since the \ is a shell escape char, the test case )}/\ should be entered with an extra \ like this: )}/\\.
-
\$\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
inandprintflike in Python? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月07日 13:29:37 +00:00Commented 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
bashcode 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 afterinandprintfare needed. \$\endgroup\$agc– agc2018年09月07日 13:47:07 +00:00Commented Sep 7, 2018 at 13:47 -
1\$\begingroup\$ In your new version you can golf 5 more bytes like this. Spaces after
foranddocan be removed.f=1can be changed toc=f=1. Andf=f+ccan bef+=c. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月07日 14:31:29 +00:00Commented 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!=0tofin the for-loop. Just like in JavaScript and Python,0is falsey and every other positive/negative integer is apparently truthy in Bash. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年09月07日 14:49:54 +00:00Commented 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– agc2020年12月16日 03:15:59 +00:00Commented Dec 16, 2020 at 3:15
Zsh, (削除) 95 (削除ここまで) 77 bytes
Try it online!
(削除) 95 bytes (削除ここまで)
j=$[$#1-1];for i ({-$j..$j})echo&&for x (${(s::)1})printf %-$[$#1-${i#-}]s $x
Takes input from argument 1ドル. Set j to length-1. Iterate i from -j to j. For each character x in the input string, print x and append a number of spaces equal to length minus the absolute value of i
-
\$\begingroup\$ More idiomatic padding of
$x(same byte-count):printf ${(r:$#1-${i#-}:)x}\$\endgroup\$roblogic– roblogic2023年10月13日 13:52:06 +00:00Commented Oct 13, 2023 at 13:52
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.
I will probably win only against Lenguage...
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.
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 19:06:46 +00:00Commented 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– Titus2018年09月04日 19:34:07 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2018年09月04日 20:53:09 +00:00Commented Sep 4, 2018 at 20:53
-
\$\begingroup\$ If you place an actual newline instead of the \n, you save a byte :) \$\endgroup\$Martijn– Martijn2018年09月06日 14:22:15 +00:00Commented 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– Titus2018年09月06日 22:26:27 +00:00Commented Sep 6, 2018 at 22:26
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
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))));}
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2018年09月05日 11:38:45 +00:00Commented Sep 5, 2018 at 11:38 -
\$\begingroup\$ @KevinCruijssen Thanks! Forgot to fix that before posting... \$\endgroup\$RobIII– RobIII2018年09月05日 11:44:45 +00:00Commented Sep 5, 2018 at 11:44
Scala , 82 bytes
for(i<-(0 to a.size)union(-a.size to 0))println(a.map(_+" "*Math.abs(i)).mkString)
Scala has lot of shortcuts that are helping me here and that is quite readable! Try Scala
-
\$\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
0tolengthamount of spaces, instead of0tolength-1amount 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\$Kevin Cruijssen– Kevin Cruijssen2018年09月05日 16:53:57 +00:00Commented Sep 5, 2018 at 16:53
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:
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2018年09月05日 16:56:33 +00:00Commented Sep 5, 2018 at 16:56
-
\$\begingroup\$ Done. Also shaved off one more byte :) \$\endgroup\$user5434231– user54342312018年09月05日 17:02:49 +00:00Commented 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
lengthamount of spaces and there are 9 lines in total, but the middle line should havelength-1amount 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\$Kevin Cruijssen– Kevin Cruijssen2018年09月05日 17:10:30 +00:00Commented Sep 5, 2018 at 17:10 -
1\$\begingroup\$ Ohh I see. That's fixed now, I even reduced it by one more byte. \$\endgroup\$user5434231– user54342312018年09月05日 17:22:36 +00:00Commented Sep 5, 2018 at 17:22
ಠ_ಠthen realize it's because you accidentally made it a year agoಠ⌣ಠ. \$\endgroup\$