Given a string, your task is to collapse it into a zigzag-like structure as described below.
Folding into a Zigzag
We'll take the string "Mississippi"
as an example:
First, output the longest prefix consisting of unique characters only:
Mis
When you reach the first duplicate character C, ignore it, and output the longest prefix consisting of unique characters of the remaining string (
issippi
) vertically, below the first occurrence of C:Mis i s
Repeat the process, alternating between horizontal and vertical continuation. But now be careful (at step 1) to continue outputting horizontally from latest occurrence of the duplicate character, which is not necessarily the last one, as in this case:
Mis i sip ----- Mis i sip i
Rules
- The string will only contain printable ASCII characters, but won't contain any kind of whitespace.
- You can compete in any programming language and can take input and provide output through any standard method and in any reasonable format1, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest submission (in bytes) for every language wins.
- 1 Input: String / List of characters / whatever else your language uses to represent strings. Output: Multiline string, list of strings representing lines or list of lists of characters/length-1 strings, but please include a pretty-print version of your code in your answer, if possible.
- Regarding additional spaces, the output may contain:
- Leading / trailing newlines
- Trailing spaces on each line / at the end
- A consistent number of leading spaces on each line
- You must begin to output horizontally, you may not start vertically.
Test cases
Inputs:
"Perfect" "Mississippi" "Oddities" "Trivialities" "Cthulhu" "PPCG" "pOpOpOpOpOpOp" "ABCCCE" "ABCCCECCEEEEC" "abcdcebffg" "abca" "AAAAAAAA"
Corresponding outputs:
Perf c t
Mis i sip i
Od ies t
Triv a l ies t
Cthul u
P C G
pO OpO pOp p
ABC C E
ABC CCE E EC
abcd e b fg
abc
A AA A
5 Answers 5
Wolfram Language (Mathematica), 143 bytes
{#}//.{q___,a_,r___,a_,Longest@s___}:>{q}~f@{a,r}~{{s}}//.{q_~f@a_~s_}/;s~FreeQ~f:>(PadLeft@{q~Join~#,##2}&)@@PadRight@Join[{a},s]/. 0->" "&
Contains 0xF8FF
, which corresponds to the \[Transpose]
operator.
Phew, it was tough making the result into a string. Getting each branch isn't so tough: #//.{q___,a_,r___,a_,Longest@s___}:>{q,a,{r},{s}}&
Python 2, 131 bytes
X=Y=y=0
s=input()
o=()
l={}
for i in s:o+=[' ']*len(s),;exec('l[i]=X,Y','y^=1;X,Y=l[i];l={}')[i in l];o[Y][X]=i;X+=y<1;Y+=y
print o
-1 thanks to Lynn.
Prints as a tuple of lists of length-1 strings. Pretty-printed output.
Python 2, (削除) 184 (削除ここまで) (削除) 176 (削除ここまで) (削除) 175 (削除ここまで) 168 bytes
-5 bytes thanks to Mr. Xcoder
def f(x):i,k=[p for p in enumerate(map(x.find,x+"z"))if cmp(*p)][0];return[x[:i]+' '*len(x)]+[' '*k+''.join(d)+i*' 'for d in zip(*f(x[i+1:]))]if x[len(set(x)):]else[x,]
-
\$\begingroup\$ I don't think the latest save is valid; what if the input contains
\
? Also, you can output as a list of lists of length-1 strings, as I do in my solution, per the OP. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年06月20日 18:55:30 +00:00Commented Jun 20, 2018 at 18:55 -
\$\begingroup\$ @EriktheOutgolfer same byte count :c \$\endgroup\$Rod– Rod2018年06月20日 19:04:48 +00:00Commented Jun 20, 2018 at 19:04
CJam, 81 bytes
{_,_S*a*a\+{__L#){V!:V;L#)LM:L;=~:U;:T}{[TU]a+L+:L}?;U@_U=T4$tt\;TV!+:T;UV+:U;}*}
Python3, 175 bytes
K=[(0,1),(1,0)]
def f(s):
b,x,y,V,d=[[' ']*len(s)for _ in s],0,0,0,{}
for i in s:
if i in d:x,y=d[i];d={};V+=1
else:b[x][y]=i;d[i]=(x,y)
X,Y=K[V%2];x+=X;y+=Y
return b
-
\$\begingroup\$ 150 bytes \$\endgroup\$ceilingcat– ceilingcat2025年09月21日 17:54:55 +00:00Commented Sep 21 at 17:54
b
, as you should only consider duplicates in the remaining string, that is, after "branching". Once you reach the secondc
, you output the longest prefix of unique chars of the remaining string, which isebffg
(thus outputtingebf
vertically and continuing horizontally after that), so you don't have to worry about the characters from the part of the string that was already outputted before switching orientation. If it still feels unclear to you, I'll make another step-by-step example with this test case. \$\endgroup\$ABCcde
\$\endgroup\$"A" ≠ "a"
. The output forABCcde
would just beABCcde
\$\endgroup\$AAAAAAAA
\$\endgroup\$