给定在它们之间有运算符的一组数字:"5 +4*9/3-8“,返回基本运算顺序的每一个排列的所有可能的结果:/,*+,-。
发布于 2019-09-08 23:57:16
x=>{int c=0,j,t=1,i;for(;c++<25;t=c){var r="*+-/".ToList();for(i=j=1;j++<4;t=t/j+1)(r[j-1],r[t%j])=(r[t%j],r[j-1]);float k(float z,int p=4){char d;int l;float m;return i<x.Count&&(l=r.IndexOf(d=x[i][0]))<p?k((m=k(x[(i+=2)-1],l))*0+d<43?z*m:d<44?z+m:d<46?z-m:z/m,p):z;}Print(k(x[0]));}}x=>{ //Lambda taking in a List<dynamic>
int c=0,j,t=1,i; //A bunch of delcarations jammed together to save bytes
for(;c++<25;t=c){ //Loop 24 times (amount of permutations a set of length 4 can have)
var r="/+*-".ToList(); //Initialize r as list of operators
for(i=j=1;j++<4;t=t/j+1) //Create the Tth permutation, saving result in r, also reset i to 1
(r[j-1],r[t%j])=(r[t%j],r[j-1]);
float k(float z,int p=4) { //Define local function 'k', with z as current value accumalated and p as current precedence
char d;int l;float m; //Some helper variables
return i<x.Count //If this is not the last number
&&(l=r.IndexOf(d=x[i][0]))<p? // And the current operator's precedence is higher than the current precedence
k( // Recursive call with the accumalative value as
(m=k(x[(i+=2)-1],l)) // Another recursive call with the next number following the current operator as seed value,
// And the next operator's precedence as the precedence value, and store that in variable 'm'
*0+d<43?z*m:d<44?z+m:d<46?z-m:z/m, // And doing the appropriate operation to m and current value ('z')
p) // Passing in the current precedence
:z; //Else just return the current number
}
Print(k(x[0])); //Print the result of calling k with the first number as starting value
}
}发布于 2019-09-10 04:03:00
f=lambda e,s={*"+-*/"}:[str(eval(p.join(g)))for p in s for g in zip(*map(f,e.split(p),[s-{p}]*len(e)))]or[e]该函数以单个字符串作为输入,并返回可能的结果列表。
def get_all_eval_results(expr, operators={*"+-*/"}):
results = []
for operator in operators:
remaining_operators = operators - {operator}
# Split expression with the current operator and recursively evaluate each subexpression with remaining operators
sub_expr_results = (get_all_eval_results(sub_expr, remaining_operators) for sub_expr in expr.split(operator))
for result_group in zip(*sub_expr_results): # Iterate over each group of subexpression evaluation outcomes
expr_to_eval = operator.join(result_group) # Join subexpression outcomes with current operator
results.append(str(eval(expr_to_eval))) # Evaluate and append outcome to result list of expr
return results or [expr] # If results is empty (no operators), return [expr]发布于 2019-09-10 10:52:37
f(t)=get(t,(),[f.([t[1:i-1];t[i+1](t[i],t[i+2]);t[i+3:end]] for i=1:2:length(t)-2)...;])julia> f([2, +, 3, *, 4])
2-element Array{Int64,1}:
20
14
julia> f([18, /, 3, *, 2, -, 1])
6-element Array{Float64,1}:
11.0
6.0
2.0
3.6
6.0
6.0以数字向量和infix函数的形式获取磁带,计算每个函数调用,然后递归地将每个得到的磁带传回给自己,直到只剩下一个数字。不幸的是,get(t, (), ...)在Julia1.0中不能正常工作,因此需要更新版本。
如果一组嵌套数组可接受为输出,则可以保存6个字节:
f(t)=get(t,(),f.([t[1:i-1];t[i+1](t[i],t[i+2]);t[i+3:end]] for i=1:2:length(t)-2))输出:
julia> f([18, /, 3, *, 2, -, 1])
3-element Array{Array{Array{Float64,1},1},1}:
[[11.0], [6.0]]
[[2.0], [3.6]]
[[6.0], [6.0]] https://codegolf.stackexchange.com/questions/191500
复制相似问题