Skip to content

Commit b1e3d4d

Browse files
author
boraxpr
committed
bite 80
1 parent 169f141 commit b1e3d4d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

80/equality.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from enum import Enum
2+
3+
4+
class Equality(Enum):
5+
SAME_REFERENCE = 4
6+
SAME_ORDERED = 3
7+
SAME_UNORDERED = 2
8+
SAME_UNORDERED_DEDUPED = 1
9+
NO_EQUALITY = 0
10+
11+
12+
def check_equality(list1, list2):
13+
"""Check if list1 and list2 are equal returning the kind of equality.
14+
Use the values in the Equality Enum:
15+
- return SAME_REFERENCE if both lists reference the same object
16+
- return SAME_ORDERED if they have the same content and order
17+
- return SAME_UNORDERED if they have the same content unordered
18+
- return SAME_UNORDERED_DEDUPED if they have the same unordered content
19+
and reduced to unique items
20+
- return NO_EQUALITY if none of the previous cases match"""
21+
if list1 is list2:
22+
return Equality.SAME_REFERENCE
23+
24+
if list1 == list2:
25+
return Equality.SAME_ORDERED
26+
27+
if not list1 == list2 and sorted(list1) == sorted(list2):
28+
return Equality.SAME_UNORDERED
29+
set1 = set(list1).__reduce__()
30+
set2 = set(list2).__reduce__()
31+
if set1 == set2:
32+
return Equality.SAME_UNORDERED_DEDUPED
33+
return Equality.NO_EQUALITY
34+
35+
# check_equality([],[])
36+
# set1 = set([3,2,2,2,2,2])
37+
# print(set1)

80/test_equality.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from equality import Equality, check_equality
2+
3+
4+
def test_same_reference():
5+
a = [1, 2, 3, 4]
6+
b = a
7+
# shallow copy (do not change original), alternatively use the copy module
8+
c = a[:]
9+
assert check_equality(a, b) == Equality.SAME_REFERENCE
10+
assert check_equality(a, c) != Equality.SAME_REFERENCE
11+
12+
13+
def test_same_ordered():
14+
a = [1, 2, 3, 4]
15+
b = a[:]
16+
c = a
17+
assert check_equality(a, b) == Equality.SAME_ORDERED
18+
assert check_equality(a, c) != Equality.SAME_ORDERED # SAME_REFERENCE
19+
20+
21+
def test_same_unordered():
22+
a = [1, 2, 3, 4]
23+
b = a[::-1]
24+
c = b[:] + [5]
25+
assert check_equality(a, b) == Equality.SAME_UNORDERED
26+
assert check_equality(a, c) != Equality.SAME_UNORDERED
27+
28+
29+
def test_same_unordered_deduped():
30+
a = [1, 2, 2, 3, 4]
31+
b = a[:] + [1, 3, 4, 4]
32+
c = b[:] + [5]
33+
assert check_equality(a, b) == Equality.SAME_UNORDERED_DEDUPED
34+
assert check_equality(a, c) != Equality.SAME_UNORDERED_DEDUPED
35+
36+
37+
def test_not_same():
38+
a = [1, 2, 3]
39+
b = [4, 5, 6]
40+
assert check_equality(a, b) == Equality.NO_EQUALITY

0 commit comments

Comments
 (0)