首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我看到你的BIDMAS,给你带了一个BADMIS

我看到你的BIDMAS,给你带了一个BADMIS
EN

Code Golf用户
提问于 2019-09-08 22:07:10
回答 3查看 2.5K关注 0票数 22

我看到你的BIDMAS并给你一个BADMIS

挑战

给定在它们之间有运算符的一组数字:"5 +4*9/3-8“,返回基本运算顺序的每一个排列的所有可能的结果:/,*+,-。

规则

  • 标准漏洞严禁
  • I/O
    • 输入必须使用infix操作进行排序,但这是最简单的(字符串或数组)。
    • 您不需要支持一元运算符(例如"-3 *8/ +2")
    • 对于隐式解析类型的语言(例如,45⟶45.0),可以用浮点数替换整数。
    • 输出必须是表达式的所有可能结果,没有指定的格式或顺序。

  • 所有输入都是有效的(例如,不需要处理"7 /3+ *")。这也意味着你永远不需要除以零。
  • 运算符都是左联想的,所以"20 /4/ 2“= "(20 / 4) / 2”。
  • 这是代码高尔夫所以赢的字节数最少

测试用例(附解释)

  • "2 +3* 4“= 14、20
    • 2+ (3 * 4)⟶2+ (12)⟶14
    • (2 + 3) *4⟶(5) *4⟶20

  • "18 /3*2-1“= 11、2、6
    • (18/ 3) * 2) -1⟶(6)*2-1⟶(12) -1⟶11
    • (18 / 3) *(2-1)⟶(6) * (1)⟶6
    • (18 / (3 * 2)) -1⟶(18 / (6)) -1⟶(3) -1⟶2
    • 18 / (3 *(2-1))⟶18 / (3 * (1))⟶6
    • 18 /(3* 2) - 1)⟶18 /5⟶3.6

测试用例(没有解释)

  • "45 /8+ 19 / 45 * 3“= 6.891666666666667,18.141666666666666,0.11111111111111113,0.01234567901234568,0.01234567901234568,5.765740740740741
  • "2 +6*7*2+6/ 4“= 112 196 23 87.5
EN

回答 3

Code Golf用户

发布于 2019-09-08 23:57:16

C# (可视化C#交互式编译器),285个字节

代码语言:javascript
复制
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]));}}

在网上试试!

代码语言:javascript
复制
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
  }
}
票数 3
EN

Code Golf用户

发布于 2019-09-10 04:03:00

Python 3,108个字节

代码语言:javascript
复制
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]

在网上试试!

该函数以单个字符串作为输入,并返回可能的结果列表。

Ungolfed

代码语言:javascript
复制
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]

在网上试试!

票数 2
EN

Code Golf用户

发布于 2019-09-10 10:52:37

朱莉娅1.2,88 (82)字节

代码语言:javascript
复制
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)...;])
代码语言:javascript
复制
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个字节:

代码语言:javascript
复制
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))

输出:

代码语言:javascript
复制
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]] 
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/191500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档