Same Object or Just Equal?

Medium ⏱ 12 min 46% acceptance ★★★★☆ 4.3
Write a function compare(a, b) that returns "same object" if a is b, "equal but different" if a == b but a is not b, and "different" if neither holds. This tests the distinction between is (identity) and == (value equality).

Examples

Example 1
Input
x = [1, 2]; compare(x, x)
Output
'same object'
Explanation

x and x reference the exact same list.

Example 2
Input
compare([1, 2], [1, 2])
Output
'equal but different'
Explanation

Equal values, distinct list objects.

Constraints

  • `a` and `b` can be any comparable objects.
  • Check identity before equality.

Topics

Variables & Data Typesidentity vs equality

Companies

MicrosoftMeta

Hints

Hint 1

Order the checks: is, then ==.

Hint 2

Two separately created lists are never the same object even with equal contents.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.