Last active
May 6, 2023 06:44
-
-
Save CalK16/058a8319b43e1cda38c34db40aa9b16e to your computer and use it in GitHub Desktop.
HW: expected value calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 加载 math 库,这个库有很多有用的模块可以用。你可以在本次作业中使用 | |
# math.isclose(a, b, rel_tol=1e-10) 来比较 a 和 b。并且只比较到小数点后10位。 | |
# 例如 math.isclose(5.0, 4.99998, rel_tol=1e-10) 会返回 True, 意思是 5.0 跟 4.99998 足够相似了。 | |
# 如果你认为你不需要这个函数,可以忽略它。 | |
import math | |
# 在此处定义你的 `expected_value` 函数 | |
# def .... | |
# ..... | |
# 这个函数输入的参数,分成两半,前面的一半是x的值,后面的一半是 f(x) 的值。 | |
# 下面这些代码是用来测试你的函数的,写完之后执行这个文件的代码,如果报 AssertionError 错误说明 | |
# 你的函数有问题,跟答案不一致 | |
# 例如: | |
assert expected_value(1, 2, 3, 0.5,0.3,0.2) == 1.7 | |
# 上面这行代码的意思是,如果我调用函数 expected_value(1, 2, 3, 0.5,0.3,0.2), 这个函数应该返回1.7。 | |
assert expected_value( | |
1, 2, 3, 4, 5, 6, | |
0.1666666666666667, | |
0.1666666666666667, | |
0.1666666666666667, | |
0.1666666666666667, | |
0.1666666666666667, | |
0.1666666666666667,) == 3.5 | |
assert expected_value( | |
0, 1, | |
0.5, 0.5 | |
) == 0.5 | |
assert expected_value( | |
5, | |
1 | |
) == 5 | |
assert expected_value( | |
0,1,2,3, | |
0.2,0.45,0.15,0.2 | |
) == 1.350 | |
assert expected_value( | |
1,5,1,5,45,1,4,54,631,6,5465,51,51,5,15,15,13,4,5,13,15,16,14,13,4,65,41,5,43,15,1,35,13, | |
0.00014992503748125936, 0.0007496251874062968, 0.00014992503748125936, 0.0007496251874062968, | |
0.006746626686656672, 0.00014992503748125936, 0.0005997001499250374, 0.008095952023988006, | |
0.09460269865067467, 0.0008995502248875562, 0.8193403298350824, 0.007646176911544228, | |
0.007646176911544228, 0.0007496251874062968, 0.0022488755622188904, 0.0022488755622188904, | |
0.0019490254872563718, 0.0005997001499250374, 0.0007496251874062968, 0.0019490254872563718, | |
0.0022488755622188904, 0.0023988005997001498, 0.002098950524737631, 0.0019490254872563718, | |
0.0005997001499250374, 0.009745127436281859, 0.0061469265367316344, 0.0007496251874062968, | |
0.006446776611694153, 0.0022488755622188904, 0.00014992503748125936, 0.005247376311844078, | |
0.0019490254872563718, | |
) == 4540.592 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment