"CodeEquivalence" (Comparison Method)

Determine if two pieces of code are equivalent after transformations.

Details

  • The "CodeEquivalence" method attempts to determine intentional equivalence by transforming expressions into a canonical representation.
  • When equivalence can be determined by transformation, no evaluation is performed and the comparison is not based on the expression's output. In particular, this makes it possible to compare code that produces non-deterministic outputs (e.g. random values, dates, etc.).
  • The "CodeEquivalence" method is directly based on the CodeEquivalenceUtilities paclet.
  • For mathematical expressions, "CodeEquivalence" relies on the Simplify function to determine equivalence. For more control, use the specific math methods "ArithmeticResult", "CalculusResult", "PolynomialResult" and "AlgebraicValue".

Examples

open allclose all

Basic Examples  (1)

Create an AssessmentFunction for a coding question:

Use it to assess equivalent code:

Scope  (1)

The "CodeEquivalence" method supports the comparison of code with random outputs:

Compare with equivalent code for random numbers with an increasing range:

This code is different; each number has the same range:

Applications  (2)

Create a QuestionObject for a coding problem:

Create a QuestionObject for a coding problem:

Create a classical coding challenge:

Properties & Relations  (2)

Symbol names need not match exactly. Undefined symbols will match any other unique undefined symbols:

The "CodeEquivalence" method can compare mathematical expressions:

Dedicated mathematical methods like "CalculusResult" also find the equivalence:

Code equivalence will treat expressions as code, where symbol names are arbitrary and any distinct symbols are considered equivalent:

Mathematical comparison methods require symbol names to match:

Possible Issues  (1)

Answers that evaluate to a deterministic output are considered equivalent to that output:

If this is not desired, use an answer key that does not have a deterministic output: