simplify HammingNeigbours function

This commit is contained in:
Jonas Seiler 2023-06-16 10:47:48 +02:00
parent 58bec526d1
commit 05e22b3c79

47
main.jl
View File

@ -50,39 +50,26 @@ function binarySearch(word::String, words::Vector{String})
return false, 0
end
function HammingNeighbours(InputWord::String)
function HammingNeighbours(w::String)
neighbours = Vector{String}()
word = collect(InputWord)
for i in eachindex(word)
for c in 'a':'z'
wordCopy = copy(word)
wordCopy[i] = c
push!(neighbours, String(wordCopy))
wordCopy = copy(word)
wordCopy = insert!(wordCopy, i, c)
push!(neighbours, String(wordCopy))
w = collect(w)
for i in eachindex(w)
for c in union('a':'z','A':'Z')
word = copy(w)
word[i] = c
push!(neighbours, String(word))
word = insert!(copy(w),i,c)
push!(neighbours, String(word))
end
for c in 'A':'Z'
wordCopy = copy(word)
wordCopy[i] = c
push!(neighbours, String(wordCopy))
wordCopy = copy(word)
wordCopy = insert!(wordCopy, i, c)
push!(neighbours, String(wordCopy))
end
wordCopy = copy(word)
wordCopy = deleteat!(wordCopy, i)
push!(neighbours, String(wordCopy))
word = deleteat!(copy(w), i)
push!(neighbours, String(word))
end
for c in 'a':'z'
wordCopy = copy(word)
push!(wordCopy, c)
push!(neighbours, String(wordCopy))
end
for c in 'A':'Z'
wordCopy = copy(word)
push!(wordCopy, c)
push!(neighbours, String(wordCopy))
for c in union('a':'z','A':'Z')
word = copy(w)
push!(word, c)
push!(neighbours, String(word))
end
return neighbours
end