June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,15 +1,13 @@
function find_missing_permutations{T<:String}(a::Array{T,1})
std = unique(sort(split(a[1], "")))
needsperm = trues(factorial(length(std)))
for s in a
b = split(s, "")
p = map(x->findfirst(std, x), b)
isperm(p) || throw(DomainError())
needsperm[nthperm(p)] = false
using Combinatorics
function missingperm(arr::Vector)
allperms = permutations(arr[1])
for perm in allperms
perm = convert(eltype(arr), perm)
if perm ∉ arr return perm end
end
mperms = T[]
for i in findn(needsperm)[1]
push!(mperms, join(nthperm(std, i), ""))
end
return mperms
end
arr = ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD",
"CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC",
"CBDA", "DBCA", "DCAB"]
@show missingperm(arr)

View file

@ -1,24 +1,17 @@
test = ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD",
"BADC", "BDAC", "CBDA", "DBCA", "DCAB"]
missperms = find_missing_permutations(test)
print("The test list is:\n ")
i = 0
for s in test
print(s, " ")
i += 1
i %= 5
i != 0 || print("\n ")
end
i == 0 || println()
if length(missperms) > 0
println("The following permutations are missing:")
for s in missperms
println(" ", s)
function missingperm1(arr::Vector{<:AbstractString})
missperm = string()
for pos in 1:length(arr[1])
s = Set()
for perm in arr
c = perm[pos]
if c ∈ s pop!(s, c) else push!(s, c) end
end
missperm *= first(s)
end
else
println("There are no missing permutations.")
return missperm
end
using BenchmarkTools
@btime missingperm(arr)
@btime missingperm1(arr)