任何副作用勾股三角所以我有這個天賦,我想解決的(我沒有寫規範)...與等於零
it "returns false if any of the arguments are 0" do
# [0, 1, 1].permutation(3) returns all permutations of [0, 1, 1]
length = rand(0.01..100.0)
[0, length, length].permutation(3).all? { |(a,b,c)| valid_triangle?(a,b,c) }.should be_false
end
這裏是我的代碼
def valid_triangle?(a, b, c)
#A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2.
if a > 0 && b > 0 && c > 0
one = a**2
two = b**2
three = c**2
if one+two=three
return "true"
else
return "false"
end
else
return "false"
end
end
我怎樣才能通過我的規範?我錯過了什麼?
這可能與浮點不精確有關。在乘法之前嘗試將參數轉換爲'BigDecimal'。 – Matt
你應該重新命名你的方法'valid_right_triangle?',或者更好,只是'right_triangle?'。順便說一句,我很好奇:什麼是從你的眼角? –