-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutations.py
59 lines (48 loc) · 1.41 KB
/
permutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from itertools import permutations as _permutations
import pytest
class permutations:
def __init__(self, iterable, r=None):
raise NotImplementedError()
def test_product_is_iterator():
"""Verify that `permutations` is (or, at least, looks like) an iterator."""
assert hasattr(permutations, "__next__")
assert hasattr(permutations, "__iter__")
p = permutations([])
assert iter(p) is p
def test_product_is_class():
"""Verify that `permutations` was defined as a class."""
assert isinstance(
permutations, type
) # Check that `permutations` is defined as a class...
assert permutations.__name__ == "permutations" # ... with the correct name.
@pytest.mark.parametrize(
["iterable", "r"],
[
(range(5), 1),
(range(5), 2),
(range(5), 3),
(range(5), 4),
(range(5), 5),
("ABC", 2),
("ABC", 3),
("XAZYPTN", 3),
("XAZYPTN", 4),
("ABAB", 2),
("ABAB", 3),
],
)
def test_permutations(iterable, r):
"""Test permutations."""
assert list(permutations(iterable, r)) == list(_permutations(iterable, r))
@pytest.mark.parametrize(
["iterable"],
[
(range(5),),
("ABC",),
("XAZYPTN",),
("ABAB",),
],
)
def test_permutations_without_r(iterable):
"""Test `permutations`."""
assert list(permutations(iterable)) == list(_permutations(iterable))