我不知道你的代码到底出了什么问题-为了帮助解决这个问题,你可能需要更多地解释它应该如何工作-但是我能够复制你的代码失败的情况。如果你跑你的车
prod2Sum
prod2Sum 1 20 -4 -5
CodeWars的预期结果是
[[|75; 104|]; [|85; 96|]]
. 我没有弄清楚您的代码是如何试图解决这个问题的,但是一些实验表明,负值会被过滤掉
MoreRule
按以下行:
|> List.filter(fun x -> x > 0)
List.map abs
在本例中有效,但对其他一些输入无效。
作为记录,找到问题所在的一个简单方法是在日志中添加一些日志
产品2
let prod2Sum (a: int) (b: int) (c: int) (d: int): int[] list =
let number = (float)(((a*a)+(b*b))*((c*c)+(d*d)))
let coefficients = MoreRule a b c d
let res =
factor number coefficients
|> List.map(fun arr -> if (arr.[0]>arr.[1]) then [|arr.[1];arr.[0]|] else [|arr.[0];arr.[1]|])
|> List.distinct
if res = [] then
printfn "%A" (a,b,c,d) // Log inputs for which we returned wrong result
r
有了这个,您应该能够找出其他哪些测试用例失败了。