Skip to content

Instantly share code, notes, and snippets.

@hpoit
Last active March 10, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpoit/4daca2a4d24e668d990c323b4e1f70b1 to your computer and use it in GitHub Desktop.
Save hpoit/4daca2a4d24e668d990c323b4e1f70b1 to your computer and use it in GitHub Desktop.
Khan palindrome (iterative)
julia> function ispalindrome(S::String)
i1 = 1
i2 = length(S)
while i2 > i1
if S[i1] != S[i2]
println(S[i1])
println(S[i2])
return false # `false` is not a variable, that's why `return` is necessary
end
i1 += 1
i2 -= 1
end
return true
end
ispalindrome (generic function with 2 methods)
julia> ispalindrome("tat")
true
julia> ispalindrome("t") # base case 1
true
julia> ispalindrome("") # base case 2, `typeof("") => String`, correct behavior, but find out why
true
julia> ispalindrome("roter")
o
e
false
julia> ispalindrome("tatff")
t
f
false
julia> ispalindrome("@@") # is this a palindrome?
true
julia> ispalindrome("ADA")
true
julia> ispalindrome("ADDa") # palindrome?
A
a
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment