Non-transitive dice: Difference between revisions
Content added Content deleted
(→{{header|Python}}: supersede with faster solution) |
(→{{header|Python}}: some cleanup; one bug fix to prevent sub loops in longer sequences that's not possible in examples required by task) |
||
Line 1,928: | Line 1,928: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
Trivial rotations of the same loop are not shown |
Trivial rotations of the same loop are not shown. |
||
<lang python>from itertools import combinations_with_replacement as cmbr |
<lang python>from itertools import combinations_with_replacement as cmbr |
||
from time import time |
from time import time |
||
def dice_gen(n, faces, m): |
def dice_gen(n, faces, m): |
||
dice = list(cmbr(faces, n)) |
dice = list(cmbr(faces, n)) |
||
succ = [set(j for j, b in enumerate(dice) |
succ = [set(j for j, b in enumerate(dice) |
||
if |
if sum((x>y) - (x<y) for x in a for y in b) > 0) |
||
for |
for a in dice] |
||
def loops(seq): |
|||
⚫ | |||
if len(seq) == m: |
|||
if |
if seq[0] in s: yield seq |
||
return |
|||
⚫ | |||
else: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
yield from (tuple(''.join(dice[s]) for s in x) |
yield from (tuple(''.join(dice[s]) for s in x) |
||
for i, v in enumerate(succ) |
for i, v in enumerate(succ) |
||
for x in loops((i,) |
for x in loops((i,))) |
||
t = time() |
t = time() |
||
for n, faces, loop_len in [(4, '1234', 3), (4, '1234', 4), (6, '123456', 3), (6, '1234567', 3)]: |
for n, faces, loop_len in [(4, '1234', 3), (4, '1234', 4), (6, '123456', 3), (6, '1234567', 3)]: |
||
for i, x in enumerate(dice_gen(n, faces, loop_len)): pass |
for i, x in enumerate(dice_gen(n, faces, loop_len)): pass |
||
print(f'{n}-sided, markings {faces}, loop length {loop_len}:') |
print(f'{n}-sided, markings {faces}, loop length {loop_len}:') |
||
print(f'\t{i + 1}*{loop_len} solutions, e.g. {" > ".join(x)} > [loop]') |
print(f'\t{i + 1}*{loop_len} solutions, e.g. {" > ".join(x)} > [loop]') |