New answers tagged

1

R, 121 bytes Or R>=4.1, 100 bytes by replacing the three function appearances with \s. function(s,Never=0,Unit=1,Bool=2,Order=3,Option=function(x)x+1,Either=`+`,Pair=`*`,Func=function(a,b)b^a)eval(parse(t=s)) Try it online! R doesn't have a simple eval, you need to parse the string first.


2

Charcoal, 66 61 60 bytes F⮌S«≡ιd⊞υ³B⊞υ²U⊞υ¹N⊞υ⁰p⊞υ⊕⊟υE⊞υ⁺⊟υ⊟υP⊞υ×⊟υ⊟υF«≔⊟υθ⊞υX⊟υθ»»Iυ Try it online! Link is to verbose version of code. Edit: Saved 5 bytes thanks to @KevinCruijssen pointing out that d and p uniquely identify Order and Option. Saved 1 byte by finding a slightly shorter way to exponentiate. Explanation: F⮌S« Loop over the characters of the ...


1

Ruby, 120 95 bytes p=i=->x,y=1{x+y} a=->x,y{x*y} u=->x,y{y**x} f=->s{eval s.gsub(/\w+/){$&[1]}.tr'enor()','0-3[]'} Try it online! Thanks to @Dingus for the 25 Bytes saved!


6

05AB1E, 77 31 28 bytes Rv"0123>+*m ""NUBdpEPF"ykè.V Byte-count more than halved and sped up a lot by porting @Neil's Charcoal answer, so make sure to upvote him as well! Try it online or verify all test cases. Explanation: R # Reverse the (implicit) input-string v # Loop over each ...


0

Perl 5, 105 bytes s/\w(.)\w+/$1/g;y/enor/0123/;1while s|([piau]).(\d+)(,(\d+))?\)|($4//1).({a,'*',u,'**'}->{$1}//'+').$2|ee Try it online!


0

Pari/GP, 92 bytes Never=0 Unit=1 Bool=2 Order=3 Option(x)=1+x Either(x,y)=x+y Pair(x,y)=x*y Func(x,y)=y^x eval Try it online! The same as @hyper-neutrino's and @wasif's answers.


3

Julia 1.0, 87 bytes eval∘Meta.parse Never,Unit,Bool,Order=0:3 Option,Pair,Either=x->x+1,*,+ Func(x,y)=y^x Try it online! Same idea as the other answers


4

JavaScript (ES7), 103 bytes A regex-based solution. This is longer than using @hyper-neutrino's method, but not as much longer as I was expecting. s=>eval(s.replace(/\w+/g,s=>(i="enorpiau".search(s[1]))<4?i:`((x,y)=>y${['|1+','+','*','**'][i&3]}x)`)) Try it online! How? All keywords can be unambiguously identified by looking at the ...


4

Retina, 129 bytes Or Q {T`NUBQ)(l`0-3;_ O(\d+); $.(_$1* E(\d+),(\d+); $.($1*_$2* P(\d+),(\d+); $.($1*$2* F(\d+) F$1* (F_*)_(,\d+;) P$1$2$2 F,\d+; 1 Try it online! Link includes test cases. Explanation: Or Q Change Order to Qder to avoid confusion with Option, and also because O has special meaning for Transliterate. { Repeat the remaining transformations ...


5

JavaScript (V8), 98 bytes Never=0 Unit=1 Bool=2 Order=3 Option=x=>1+x Either=(x,y)=>x+y Pair=Math.imul Func=(x,y)=>y**x eval Try it online! Same idea as @hyper-neutrino, though its shorter in js -1 byte thanks to @Arnauld


17

Proton, 80 bytes Never,Unit,Bool,Order=0..4 Option=(1+) Either=(+) Pair=(*) Func=(x,y)=>y**x eval Try it online! This solution is a whole lot shorter in Proton. Original Python solution included below. Python 3, 106 bytes Never=0 Unit=1 Bool=2 Order=3 Option=1 .__add__ Either=int.__add__ Pair=int.__mul__ Func=int.__rpow__ eval Try it online! -3 bytes ...


0

Charcoal, 19 bytes FS≡ι(«ι⊞υυ»)«ι¿¬⊟υ⸿ Try it online! Link is to verbose version of code. Explanation: FS≡ι Switch over each character of the input string. («ι⊞υυ» If it's a ( then print it and push the predefined empty list to itself. The list therefore contains itself as many times as there are unbalanced (s. )«ι If it's a ( then print it, and... ¿¬⊟υ⸿ ...


0

Retina 0.8.2, 25 bytes \w !`\(((\()|(?<-2>.))*. Try it online! Link includes test cases. Explanation: The first two lines simply delete letters and digits. The final line matches groups of (assumed) parentheses. .NET balancing groups keep track of the number of unmatched (s and don't allow more )s than (s. This means that the inner loop stops when it ...


0

Retina 0.8.2, 131 bytes (f.(\d;)*(;(\d;)*)*)(;[^]]*].(?<-3>;)*(?(3)^))(\d);? $1$6$5 }`\[\] }`(f(.(\d|((?=;)))(?=[];]))*);?(].(?<-4>;)*)([^]]+)? $1$#6$*;$6 Try it online! Explanation: Uses .NET balancing groups to shuffle arguments into slots. The first two lines shuffle arguments from the second application into free slots in the first. The ...


0

PowerShell, 62 bytes switch($args){'('{$r+=$_;++$l}')'{$r+=$_;if(!--$l){$r;$r=''}}} Try it online!


0

Lua, 46 bytes print(arg[1]:gsub("%w",""):gsub("%b()","%1;")) Try it online! Looks like it happened working. Though I don't know much about Lua.


1

Perl 5, 26 bytes y/()//cd;s/\((?R)*\)/$& /g Try it online! y/()//cd; #delete all chars from input line except ( and ) s/\((?R)*\)/$& #print clusters separated by newline /g #found by recursive regexp with (?R)


1

JavaScript, 55 53 bytes Outputs a space delimited string with a trailing space. s=>s.replace(/./g,x=>x>{}?``:(n+=x>s||-1)?x:`) `,n=0) Try it online!


1

R, 99 92 bytes Or R>=4.1, 85 bytes by replacing the word function with \. -7 bytes thanks to @Dominic van Essen. function(s,x=utf8ToInt(s))Map(intToUtf8,split(a<-x[x<42],head(diffinv(!cumsum(a*2-81)),-1))) Try it online! As always, R is terrible in string challenges...


2

Ruby, 37 bytes ->s{s.tr('^()','').scan /\(\g<0>*\)/} Try it online! Returns an array of clusters. The numbered subpattern \g<0> nests the balanced-parentheses-matching regex within itself.


1

C (gcc), 70 bytes I think at this point I have to admit I'm on a ternary operator abuse spree. Big thanks to ErikF for -14 bytes. And thanks the ceilingcat for -4 more! c;f(d){for(d=0;read(0,&c,1);c>47||(d+=2*putchar(c)-81)||putchar(32));} Try it online!


1

PowerShell Core, 89 80 bytes -9 bytes thanks to @mazzy!!! $args-replace"\w"|sls "(\((?=\(*(?<S>\)))|\k<S>(?<-S>))+?(?(S)(?!))"-a|% M*|% V* Try it online!


2

Java (JDK), 86 80 79 bytes n->{int o=0;for(var c:n)if(o!=(o+=c>41?0:81-2*c))System.out.print(o<1?") ":c);} Try it online! -7 thanks to Kevin Kruijssn and Zaelin Goodman!


2

Pip, 28 25 24 23 bytes Fca@X^pI$==_NyPBcMpY/Py Try it here! Or, with a variable definition in the header, you can also Try it online! Explanation Pushes one parenthesis at a time onto y, checks whether y is balanced, and if so, outputs and resets it. Fca@X^pI$==_NyPBcMpY/Py a is cmdline arg; p is "()"; y is "" (...


5

Jelly, 11 bytes fØ(µO-*ĬṖk Try it online! The Footer just runs all the test cases, joins each cluster by a single newline and each test case by 2. How it works fØ(µO-*ĬṖk - Main link. Takes a string S on the left fØ( - Remove all non-"()" characters µ - Use this string of parentheses P as the argument from here: O - ...


2

Java 8, 89 82 81 bytes s->{int t=0;for(var c:s)System.out.print(c==40?++t>0?c:c:c==41?--t<1?") ":c:"");} -7 bytes thanks to @ZaelinGoodman Input as character-array; output as a space-delimited string. Try it online. Explanation: s->{ // Method with character-array as parameter and no return int t=0; ...


3

Python 3, 75 bytes def f(s,x=1): for i in s:y=(i==")")-(i=="(");x+=y;y and print(i,end=" "*x) Try it online! prints the clusters separated by spaces Python 3, 88 bytes def f(s,x=1,w=""): for i in s:y=(i==")")-(i=="(");x+=y;w+=y*y*i+" "*x return w.split() Try it online! returns a ...


2

Japt v2.0a0, 15 13 12 bytes r\w ó@T±JpXc Try it r\w ó@T±JpXc :Implicit input of string U r :Replace \w : RegEx /[a-z0-9]/gi ó :Partition after each character X @ :That returns falsey (0) when passed through the following function T± : Increment T (initially 0) by J ...


2

JavaScript (ES6),  61 60  59 bytes Saved 1 byte thanks to @tsh Outputs a string with one cluster per line. s=>s.replace(/./g,c=>c=='('?(s=-~s,c):c==')'?--s?c:`) `:'') Try it online!


2

05AB1E, 18 17 14 bytes žuÃDÇÈ·<.¥_Å¡¦ -1 byte because of the rule change from all printable ASCII to just parenthesis and alphanumeric characters in the input -3 bytes and improved performance thanks to @cairdCoinheringaahing Output as a list of list of characters. Try it online or verify all test cases. Explanation: žu # Push constant ...


3

APL(Dyalog Unicode), 26 bytes SBCS {⍵⊂⍨¯1⌽0=+\¯1*')'=⍵}∩∘'()' Try it on APLgolf! or Try it with step by step output ∩∘'()' intersect right argument with (). This removes all other characters. { ... } call the dfn with the cleaned string as its right argument ⍵. ')'=⍵ for each character in the string, is it equal to )? ¯1* this maps ( to 1 and ) to ¯1. +\ ...


9

Vim, 20 bytes :s/\w//g qq%a <Esc>@qq@q Try it online! Programmer's text editor builtins ftw! \o/ Explanation: :s/\w//g # Remove all letters and numbers qq # Start macro 'q' %a # Jump to matching parenthesis and insert newline <Esc> @q # Call macro 'q' q # End macro 'q' @q # Call macro 'q'


5

J, 35 bytes (](]<;.2~0=[:+/\_1+2*i.)[-.-.)&'()' Try it online! Remove all parens from the input. Turn ( into -1 and ) into 1, and take the scan sum, then convert that into a mask which is 1 wherever the sum is 0. Finally, use that mask to cut the cleaned input into chunks.


2

JavaScript (Node.js), 83 bytes A=>[...A.replace(/[^()]/g,(o=0,t=''))].map(l=>(t+=l,!(o+=l<")"||-1)&&t+(t="")||"")) Try it online! If it accepts array having , in between parentheses otherwise : JavaScript (Node.js), 101 100 95 bytes A=>[...A.replace(/[^()]/g,(o=0,a=[],t=''))].map(l=>(o+=l<")&...


Top 50 recent answers are included