We just launched a new product: Collectives on Stack Overflow. How do they work? Listen to learn more.

# Tag Info

## Hot answers tagged string

18

Charcoal, 32 29 27 bytes ≔⁰θＦＳ≔⁺×θＸ³℅ι℅ιθ‹³﹪θ⁸§1ijkθ Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer: Multiplying by i is equivalent to tripling the integer and ...

17

Pip, 6 bytes RP.q Try it online! Explanation Um... well, this is interesting. It just so happens that Pip's Pattern type (used for regex) is delimited with backticks. And that backticks can be escaped within a Pattern using backslashes. ... And furthermore, that the code for generating the Pip repr of a Pattern is apparently incorrect, because it should ...

16

C (gcc), 267 ... 230 222 bytes -30 bytes thanks to @EasyasPi -8 more bytes thanks to @EasyasPi and @ceilingcat #define P puts( char*m;a(){if(*m==73){P"<");for(++m;*m^69;*m-69&&P","))p();m++;P">");}}p(){if(*m==78){for(++m;*m^69;P"::"))p();++m;}a(m+=write(1,m,strtol(m,&m,!a())));}z(char*t){p(m=t+2);...

14

J, 23 bytes ~:/I.@,~4|[:-/1#;._1@,] Try it online! Takes a boolean vector where 0 represents r and 1 represents s, and returns the result in the same encoding. How it works Imagine evaluating the chunks of $r^n s r^m s$ from the start. If we evaluate $sr$ in the middle $m$ times, we get $r^{n+3m}s^2 = r^{n+3m}$. We can repeat the process to the end. ...

12

Python 3, 460 417 362 355 bytes Try it online! -43 bytes thanks to @Easyaspi -55 bytes thanks to @ovs -7 bytes thanks to @Arnauld import re m=input()[2:] o="" def a(): global m,o if"I"==m[:1]: m=m[1:];o+="<" while"E"!=m[:1]:p();o+="," m=m[1:];o=o[:-1];o+=">" def p(): global m,o if&...

11

MATL, 29 28 24 bytes z2Y6ttX*,GbZ+5Mz=z]yytvs Input is a binary matrix with 1 for '+' and 0 for '-'. Try it online! Or verify all test cases. Explanation Convolution is the key to success z % Implicit input. Number of nonzeros. This is the number of naive pluses 2Y6 % Push [0 1 0; 1 1 1; 0 1 0] (predefined literal): pattern of double plus ttX* ...

10

Haskell, 26 bytes f(x:y)=([x],0)*>span(/=x)y Try it online! Shortens Zgarb's OG solution f(x:y)|(a,b)<-span(/=x)y=(x:a,b) by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). It would be nice it we could do (x:)<$>(a,b), but that gives (a,x:b) -- the Functor instance of tuples lets us act ... 10 APL (Dyalog Extended), 35 bytes ↑{⊃{0~⍨⍺-1-∊1⍵0⍵}/-⌽⍬⊂⍛,1↓⊤1+≢⍵}↑¨⊢ Try it online! Takes a string and puts each command on its own line top to bottom, indenting from left to right. The algorithm Given the length of the input n, the indentation pattern (the length of each line) looks like the following: f(1) =  f(2n + 2) = let prev = f(n) in  ++ (2+... 9 APL (Dyalog Unicode), 11 bytes ~∘' '⍤1∘⍉⍣2 Try it online! A train taking and returning a character matrix. ⍣2 ⍝ repeat 2 times: ⍉ ⍝ transpose the character matrix ~∘' ' ⍝ remove spaces ⍤1 ⍝ in each row ⍝ each row is padded with spaces to keep the matrix shape APL (Dyalog Extended), 8 bytes -2 bytes thanks ... 9 AWK, 17 14 bytes sub(FS$1,RS$1) Try it online! Thanks to Pedro Maimere for the hint to lop off 3 bytes The interactions of the rules in the contest allow for pretty trivial AWK solution... Assuming the input can be a blank delimited string of numbers, this will work. If it has to include the brackets and commas (which I wasn't sure about from the linked ... 9 Python 3, 117 115 bytes -2 thanks to ovs (use mod 8 in place of mod 6 allowing the bitwise-OR in place of exponentiation; remove a space from 5!=x or with x!=5or.) My golf of hyper-neutrino's Python 3 answer. k= o="" for c in input():x=ord(c)%8;o+="() ( )"[x]*(x<5or k.pop());k=+k+*(x==3);k[-1]+=x<3and-x|1 print(o) Try ... 9 Charcoal, 26 bytes ＦＳ≡ι)⊟υ]Ｗ∧υ⊟υκ«(Ｆ⁼ι[⊞υω⊞υ) Try it online! Link is to verbose version of code. Explanation: ＦＳ≡ι Switch over each character of the input. )⊟υ If it's a ) then pop and print the top of the stack (which should always be a ) at this point). ]Ｗ∧υ⊟υκ If it's a ] then pop and print the top of the stack until it becomes empty or an empty ... 9 Perl 5 -p, 28 bytes 1while s/@(.K)(\1*K)\1*K/$2/ Try it online! How it works Each term in the $K$ calculus normalizes to $a_i = (\texttt{@K})^i\texttt K$ for some $i \ge 0$, because $\texttt K = a_0$, $\texttt @a_0a_j = a_{j + 1}$, and $\texttt @a_{i + 1}a_j \to a_i$. So the innermost redex always looks like $\texttt @(\texttt{@K})^{i + 1}\... 9 brainfuck, 494 bytes >>+++++[->++++++++<]>>>>>,,,[<+>>+++++++[-<---------->]<+[----[<<[<<[.<]>[>]++++++++[-<----->]<[>>-<<[->+<]]>[-<+>]++++++++[-<+++++>]>[<<++++<++++++[-<+++++++>]<->>>>-]]>>-----[>+++++[-<+... 9 Python 2, 103 91 bytes -12 bytes thanks to PurkkaKoodari! lambda s:f(zip(*s)) f=lambda s:len(s)and-~min(f({r[r==x:]for r in s}-{()})for x in s) Try it online! The last testcase times out. The first function just transposes the input, if we can take input as a list of columns it can be omitted. 8 perl -p, 32 bytes while(s/rrrr|ss//||s/sr/rrrs/){} Takes advantage of substitution returning truthy iff anything was substituted. 8 PicoLisp, 211 bytes (de f(x k)[or k(set'k(0][set'a(car x][set'b(cdr x](if x(cond[(="("a)(prin a)(f b(cons(+(car k)1)(cdr k][(=")"a)(prin a)(f b(cons(-(car k)1)(cdr k][(="["a)(prin"(")(f b(cons 1 k](1[prin(need(car k)")"](f b(cdr k] Try it online! -1 byte thanks to Wzl First time using picolisp, so it's ... 8 Haskell, 41 bytes f x|sum x<1=x f(0:y:x)=y:f x f(1:x)=2:f x Try it online! Here's a solution. Just to show that this is possible and give a potential starting point for others. Explanation The first thing we can notice is that any valid$f\$must preserve the number of zeros at the end of the input in the output. The proof of this is pretty simple. ... 8 Smalltalk, 325 bytes [:k|k inject:OrderedCollection new into:[:a :b|(a isEmpty not and:[a last=0])ifTrue:[(b=$')ifTrue:[a removeLast. a add:b. a]ifFalse:[a removeLast. a add:b. a add:0. a]]ifFalse:[(a isEmpty not and:[a last=$"])ifTrue:[(b=$")ifTrue:[a removeLast. a]ifFalse:[a]]ifFalse:[(b=$')ifTrue:[a add:b. a add:0. a]ifFalse:[a add:b. a]]]]] ... 8 J, 67 bytes g=:(0,1+$:@<:)([:(,0,])1+$:@<.@-:)0:@.(=&1+2&|) f=:' '&,.#"1~1,.~g@# Try it online! This produces an upside-down left-to-right tree, which should be valid per the comments. For example, f '-1+!2' produces: - 1 + ! 2 the idea First notice the numerical pattern in the number of space indents: 1 -> 0 ... 8 Haskell, 257 bytes (l:r)%x=l:tail(x>>=(',':))++r h('I':r)|(x,e:r)<-f r=("<>"%x,r) h e=("",e) f('N':r)|(x,e:r)<-f r,(h:t,r)<-f r=(((x>>=(++"::"))++h):t,r) f r|[(n,r)]<-reads r,(i,u)<-h(drop n r),(x,u)<-f u=((take n r++i):x,u) f e=([],e) p[x]=x p(x:y)=x++"()"%y z=p.fst.f.drop 2 Try ... 8 Charcoal, 57 bytes ＷＳ⊞υι≔⟦Ｅθ⮌⭆υ§λκ⟧η≔⁰ζＷ⌊η«≦⊕ζ≔ηθ≔⟦⟧ηＦθＦκ⊞ηΦＥκΦμ∨π¬⁼ξ§λ⁰μ»Ｉζ Try it online! Link is to verbose version of code. Uses brute force so too slow for the last test case on TIO. Explanation: ＷＳ⊞υι Input the pile. ≔⟦Ｅθ⮌⭆υ§λκ⟧η Rotate the pile by 90° and put it into a list. ≔⁰ζ Start counting the number of steps. Ｗ⌊η« Repeat until there is at ... 8 Pyth, 22 bytes L&lbhSmhyfT>RqhkhdbbyC Try it online! Or run all test cases at once. This is basically ovs's Python solution translated verbatim to Pyth (it seems that their solution's exact algorithm is also shortest in Pyth), so I'm posting it as community wiki. Doesn't time out thanks to Pyth's memoization. 8 Python 3, 35 bytes lambda s:'%s'%s.replace('','\') Try it online! -1 thanks to dingledooper. 7 Python 2, 58 bytes @dingledooper saves 12 bytes by switch to Python 2 and finding out some bit-wise operators expression which I cannot understand! p=q=0 for b in input():p^=b;q+=~p/~b^p print q%4*+p* Try it online! I/O as an array of integers, r = 0, s = 1. JavaScript needs too many bytes to repeat some strings. 7 Jelly, 5 bytes Zḟ€⁶Z Try it online! Zḟ€⁶Z Main Link Z Transpose the matrix ḟ Filter out € From each row ⁶ Spaces Z Transpose the matrix 7 Vyxal, 18 7 bytes Ṙṫ:‟€vp Try it Online! 7 Factor, 37 35 bytes [ dup first 1 pick index-from cut ] Try it online! Explanation: dup Duplicate the input. Stack: (e.g.) { 0 2 2 3 0 1 0 1 } { 0 2 2 3 0 1 0 1 } first Get first element. Stack: { 0 2 2 3 0 1 0 1 } 0 1 Push 1. Stack: { 0 2 2 3 0 1 0 1 } 0 1 pick Put a copy of the object third from the top on top of the stack. Stack: { 0 2 2 3 0 1 0 1 } ... 7 Vim, 3 bytes/keystrokes *O<esc> Jump to next occurrence of word and make a new line. V (vim), 2 bytes *O Try it online! Since V has implicit escape after O (?), we can use just 2 bytes. Old (general) 8-byter: Y/<C-r>"<BS><CR>O<esc> Input is each list item on a single line. Output is two lists separated by a blank line. ... 7 JavaScript (Node.js), 287 233 bytes Saved a ton of bytes thanks to @A username, @FZs, @Arnauld and @Recursive Co.! (Hope I didn't miss anyone!) (i,r="",P=2,s=c=>i[P]!=c,e=_=>{if(!sN)for(P++;sE||!++P;r+="::")e();r+=i.slice(P+=b=${l=parseInt(i.slice(P))}.length,P+=l);sI||P++&a<>&P++},a=([[p,q]])=>{for(r+=p;...

Only top voted, non community-wiki answers of a minimum length are eligible