Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Code only needed where #YOUR CODE HERE is, everything else is set that way. First picture is the question, second picture is the tests that
Code only needed where #YOUR CODE HERE is, everything else is set that way. First picture is the question, second picture is the tests that check if it the method is correct or not, third picture is the variables function to identify variables. Thanks in advance! (PYTHON)
Question 5: value equality We ask you to write the value_equality method. Given the two expressions e and f, first compute the set of variables V that appear in either e or f. Then, the idea consists in performing num_sample times the following test for equality: First, produce a variable assignment (a dictionary) mapping each variable in V to a random value. Choose these random values from the gaussian distribution centered around 0 and with standard deviation 10 (for instance; any continuous distribution with infinite domain would work). You can obtain such numbers using random.gauss(0, 10). Then, compute the values of e and f with respect to that variable evaluation. If the values are closer than a specified tolerance tolerance, you consider e and f equal (for that variable valuation). Otherwise, you can stop and return that e and f are different. If you can repeat the process num_sample times, and e and f are considered equal every time, then you declare them equal. [356] ### Exercise: implementation of value equality import random def value_equality(e, f, num_samples=1000, tolerance=1e-6): ***"Return True if the two expressions self and other are numerically equivalent. Equivalence is tested by generating num_samples assignments, and checking that equality holds for all of them. Equality is checked up to tolerance, that is, the values of the two expressions have to be closer than tolerance. It can be done in less than 10 lines of code." ### YOUR CODE HERE ### Tests for value equality. 4 points. ('+', ('*', 'x', 1), ("*', 'y', o)) e2 = 'x' assert value_equality(ei, e2) = e3 (7, ('*', 'x', 'x'), ('*', 'x', 1)) assert value_equality(e1, e3) e4 = ('7', 'y', 2) assert not value_equality(e1, e4) assert not value_equality(e3, e4) assert not value_equality(e4, e3) e5 = ("+", "cat", ("-", "dog", "dog")) assert value_equality(e5, "cat") assert value_equality("cat", e5) = ("-", "hello", "hello") assert value_equality(e6, 0) assert value_equality(@, e) Question 5: value equality We ask you to write the value_equality method. Given the two expressions e and f, first compute the set of variables V that appear in either e or f. Then, the idea consists in performing num_sample times the following test for equality: First, produce a variable assignment (a dictionary) mapping each variable in V to a random value. Choose these random values from the gaussian distribution centered around 0 and with standard deviation 10 (for instance; any continuous distribution with infinite domain would work). You can obtain such numbers using random.gauss(0, 10). Then, compute the values of e and f with respect to that variable evaluation. If the values are closer than a specified tolerance tolerance, you consider e and f equal (for that variable valuation). Otherwise, you can stop and return that e and f are different. If you can repeat the process num_sample times, and e and f are considered equal every time, then you declare them equal. [356] ### Exercise: implementation of value equality import random def value_equality(e, f, num_samples=1000, tolerance=1e-6): ***"Return True if the two expressions self and other are numerically equivalent. Equivalence is tested by generating num_samples assignments, and checking that equality holds for all of them. Equality is checked up to tolerance, that is, the values of the two expressions have to be closer than tolerance. It can be done in less than 10 lines of code." ### YOUR CODE HERE ### Tests for value equality. 4 points. ('+', ('*', 'x', 1), ("*', 'y', o)) e2 = 'x' assert value_equality(ei, e2) = e3 (7, ('*', 'x', 'x'), ('*', 'x', 1)) assert value_equality(e1, e3) e4 = ('7', 'y', 2) assert not value_equality(e1, e4) assert not value_equality(e3, e4) assert not value_equality(e4, e3) e5 = ("+", "cat", ("-", "dog", "dog")) assert value_equality(e5, "cat") assert value_equality("cat", e5) = ("-", "hello", "hello") assert value_equality(e6, 0) assert value_equality(@, e)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started