Jump to content

Non-transitive dice: Difference between revisions

m
compatibility fix for versions < 1.5
m (→‎{{header|Phix}}: minor tidy)
m (compatibility fix for versions < 1.5)
Line 286:
{{trans|Python}}
<lang julia>import Base.>, Base.<
using Memoize, Combinatorics
 
struct Die
name::String
faces::Vector{Int}
end
 
""" Compares two die returning 1, -1 or 0 for operators >, < == """
function cmpd(die1, die2)
Line 303:
return (tot[3] > tot[1]) - (tot[1] > tot[3])
end
 
Base.:>(d1::Die, d2::Die) = cmpd(d1, d2) == 1
Base.:<(d1::Die, d2::Die) = cmpd(d1, d2) == -1
 
""" 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]
 
findnontrans(alldice, n=3) = [perm for perm in permutations(alldice, n) if isnontrans(perm)]
 
function possible_dice(sides, mx)
println("\nAll possible 1..$mx $sides-sided dice")
Line 322:
return uniquedice
end
 
""" Compares two die returning their relationship of their names as a string """
function verbosecmp(die1, die2)
Line 331:
return win1 > win2 ? "$n1 > $n2" : win1 < win2 ? "$n1 < $n2" : "$n1 = $n2"
end
 
""" Dice cmp function with verbose extra checks """
function verbosedicecmp(dice)
return join([[verbosecmp(x, y) for (x, y) in zip(dice[1:end-1], dice[2:end])];
[verbosecmp(dice[1], dice[end])]], ", ")
end
 
function testnontransitivedice(faces)
dice = possible_dice(faces, faces)
Line 343:
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))")
for list in (isempty(nontrans) || faces < 5 ? nontrans : [nontrans[end]])
println(faces < 5 ? "" : " Only printing last example for brevity")
for (i, die) in enumerate(list)
println(" $(i != 0 ? " " : "[")$die$(i < N - 1 ? "," : "]")")
end
end
Line 356:
end
end
 
@time testnontransitivedice(3)
@time testnontransitivedice(4)
4,107

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.