Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Perm((1, 2, 4, 0, 3, 5)).ascii_plot() == " | | | | | |\n"
"-+-+-+-+-+-●-\n"
" | | | | | |\n"
"-+-+-●-+-+-+-\n"
" | | | | | |\n"
"-+-+-+-+-●-+-\n"
" | | | | | |\n"
"-+-●-+-+-+-+-\n"
" | | | | | |\n"
"-●-+-+-+-+-+-\n"
" | | | | | |\n"
"-+-+-+-●-+-+-\n"
" | | | | | |"
)
for _ in range(10):
perm = Perm.random(random.randint(0, 20))
plot = perm.ascii_plot(cell_size=0).split("\n")
for i in range(len(perm)):
assert plot[len(perm) - perm[i] - 1][2 * i] == "\u25cf"
def test_reverse_complement():
for _ in range(100):
perm = Perm.random(random.randint(0, 20))
assert perm.reverse_complement() == perm.rotate().rotate()
def test_rotate_180():
for _ in range(100):
perm = Perm.random(random.randint(0, 20))
assert perm.rotate(times=2) == perm.rotate().rotate()
counter[Perm((2, 0, 3, 1))] == 0,
counter[Perm((2, 1, 0, 3))] == 0,
counter[Perm((2, 1, 3, 0))] == 0,
counter[Perm((2, 3, 0, 1))] == 0,
counter[Perm((2, 3, 1, 0))] == 0,
counter[Perm((3, 0, 1, 2))] == 0,
counter[Perm((3, 0, 2, 1))] == 0,
counter[Perm((3, 1, 0, 2))] == 0,
counter[Perm((3, 1, 2, 0))] == 0,
counter[Perm((3, 2, 0, 1))] == 0,
counter[Perm((3, 2, 1, 0))] == 0,
)
)
)(Perm((1, 0, 3, 5, 2, 4)).fourpats())
for _ in range(20):
perm = Perm.random(random.randint(0, 20))
fourpatdict = perm.fourpats()
for key, val in fourpatdict.items():
assert key.count_occurrences_in(perm) == val
assert all(v == 0 for v in Perm((0,)).threepats().values())
assert all(v == 0 for v in Perm((0, 1)).threepats().values())
assert (
lambda counter: all(
(
counter[Perm((0, 1, 2))] == 0,
counter[Perm((0, 2, 1))] == 0,
counter[Perm((1, 0, 2))] == 3,
counter[Perm((1, 2, 0))] == 0,
counter[Perm((2, 0, 1))] == 0,
counter[Perm((2, 1, 0))] == 1,
)
)
)(Perm((2, 1, 0, 3)).threepats())
for _ in range(20):
perm = Perm.random(random.randint(0, 20))
threepatdict = perm.threepats()
for key, val in threepatdict.items():
assert key.count_occurrences_in(perm) == val
return Perm(perm)
assert Perm([3, 7, 0, 8, 1, 6, 5, 2, 4]).contained_in(
Perm([3, 7, 0, 8, 1, 6, 5, 2, 4])
)
assert Perm([3, 7, 0, 8, 1, 6, 5, 2, 4]).contains(Perm([3, 7, 0, 8, 1, 6, 5, 2, 4]))
assert Perm([]).contained_in(Perm([]))
assert Perm([]).contained_in(Perm([3, 7, 0, 8, 1, 6, 5, 2, 4]))
assert Perm([0]).contained_in(Perm([0]))
assert not Perm([7, 3, 0, 8, 1, 6, 5, 2, 4]).contained_in(
Perm([3, 7, 0, 8, 1, 6, 5, 2, 4])
)
for i in range(100):
n = random.randint(0, 4)
patt = Perm.random(n)
perm = generate_contained(random.randint(n, 8), list(patt))
assert patt.contained_in(perm)
assert perm.contains(patt)
assert not Perm([0]).contained_in(Perm([]))
assert not Perm([0, 1]).contained_in(Perm([]))
assert not Perm([0, 1]).contained_in(Perm([0]))
assert not Perm([1, 0]).contained_in(Perm([0, 1]))
assert not Perm([0, 1, 2]).contained_in(Perm([0, 1]))
assert not Perm([1, 0, 2]).contained_in(Perm([0, 1, 3, 4, 2]))
assert not Perm([0, 1, 2]).contained_in(Perm([2, 1, 3, 0]))
assert not Perm([2, 1, 3, 0]).contained_in(Perm([2, 0, 3, 1]))
assert not Perm([0, 2, 1]).contained_in(Perm([2, 0, 1, 3]))
assert not Perm([2, 0, 1, 3]).contained_in(Perm([5, 3, 2, 7, 1, 0, 6, 4]))
assert not Perm([0, 1, 2, 3]).contained_in(Perm([4, 7, 5, 1, 6, 2, 3, 0]))
def test_ltrmin():
assert list(Perm().ltrmin()) == []
assert list(Perm((0,)).ltrmin()) == [0]
assert list(Perm((2, 4, 3, 0, 1)).ltrmin()) == [0, 3]
for _ in range(100):
perm = Perm.random(random.randint(2, 20))
ltrmin_list = list(perm.ltrmin())
assert ltrmin_list[0] == 0
for i in range(len(ltrmin_list)):
for j in range(ltrmin_list[i] + 1, len(perm)):
if perm[j] < perm[ltrmin_list[i]]:
assert ltrmin_list[i + 1] == j
break
def test_rank_encoding():
assert Perm().rank_encoding() == []
assert Perm((0,)).rank_encoding() == [0]
for _ in range(20):
perm = Perm.random(random.randint(0, 20))
for index, val in enumerate(perm.rank_encoding()):
invs = 0
for i in range(index + 1, len(perm)):
if perm[i] < perm[index]:
invs += 1
assert invs == val
def test_eq():
assert Perm([]) == Perm([])
assert Perm([0]) == Perm([0])
assert Perm([]) != Perm([0])
assert Perm([0]) != Perm([])
assert not (Perm([]) != Perm([]))
assert not (Perm([0]) != Perm([0]))
assert not (Perm([]) == Perm([0]))
assert not (Perm([0]) == Perm([]))
for _ in range(100):
a = Perm.random(random.randint(0, 10))
b = Perm(a)
c = Perm.random(random.randint(0, 10))
if a == c:
continue
assert a == b
assert a != c
assert b == a
assert c != a
def test_random():
for length in range(11):
for _ in range(10):
perm = Perm.random(length)
assert len(perm) == length
assert set(perm) == set(range(length))