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 return false, 0
end end
function HammingNeighbours(InputWord::String) function HammingNeighbours(w::String)
neighbours = Vector{String}() neighbours = Vector{String}()
word = collect(InputWord) w = collect(w)
for i in eachindex(word) for i in eachindex(w)
for c in 'a':'z' for c in union('a':'z','A':'Z')
wordCopy = copy(word) word = copy(w)
wordCopy[i] = c word[i] = c
push!(neighbours, String(wordCopy)) push!(neighbours, String(word))
wordCopy = copy(word)
wordCopy = insert!(wordCopy, i, c) word = insert!(copy(w),i,c)
push!(neighbours, String(wordCopy)) push!(neighbours, String(word))
end end
for c in 'A':'Z' word = deleteat!(copy(w), i)
wordCopy = copy(word) push!(neighbours, String(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))
end end
for c in 'a':'z'
wordCopy = copy(word) for c in union('a':'z','A':'Z')
push!(wordCopy, c) word = copy(w)
push!(neighbours, String(wordCopy)) push!(word, c)
end push!(neighbours, String(word))
for c in 'A':'Z'
wordCopy = copy(word)
push!(wordCopy, c)
push!(neighbours, String(wordCopy))
end end
return neighbours return neighbours
end end