Non-transitive dice: Difference between revisions

Content added Content deleted
m (compatibility fix for versions < 1.5)
Line 286: Line 286:
{{trans|Python}}
{{trans|Python}}
<lang julia>import Base.>, Base.<
<lang julia>import Base.>, Base.<
using Combinatorics
using Memoize, Combinatorics
struct Die
struct Die
Line 308: Line 308:
""" True iff ordering of die in dice is non-transitive """
""" True iff ordering of die in dice is non-transitive """
isnontrans(dice) = length(dice) > 1 && all(x -> x[1] < x[2], zip(dice[2:end-1], dice[2:end])) && dice[1] > dice[end]
isnontrans(dice) = all(x -> x[1] < x[2], zip(dice[1:end-1], dice[2:end])) && dice[1] > dice[end]
findnontrans(alldice, n=3) = [perm for perm in permutations(alldice, n) if isnontrans(perm)]
findnontrans(alldice, n=3) = [perm for perm in permutations(alldice, n) if isnontrans(perm)]
Line 343: Line 343:
nontrans = unique(map(x -> sort!(x, lt=(x, y)->x.name<y.name), findnontrans(dice, N)))
nontrans = unique(map(x -> sort!(x, lt=(x, y)->x.name<y.name), findnontrans(dice, N)))
println("\n Unique sorted non_transitive length-$N combinations found: $(length(nontrans))")
println("\n Unique sorted non_transitive length-$N combinations found: $(length(nontrans))")
for list in (isempty(nontrans) || faces < 5 ? nontrans : [nontrans[end]])
for list in (faces < 5 ? nontrans : [nontrans[end]])
println(faces < 5 ? "" : " Only printing last example for brevity")
println(faces < 5 ? "" : " Only printing last example for brevity")
for (i, die) in enumerate(list)
for (i, die) in enumerate(list)