Error:MethodError: no method matching getindex(::typeof(LCSlength), ::Int64)

Hello,
I am new to programming. I am learning Julia through online resources. I tried to implement a code in dynamic programming to find Longest common sub sequence. But I get some error, that I do not understand.
All sorts of helps are highly appreciated.
julia1

Welcome to discourse. Please avoid posting screenshots of your code: it will be easier for us to help you if we can copy-paste your buggy code to try and improve it.

See below for some instructions about how to correctly quote source code here:


As for your problem, I can’t see very well (yet another problem with screenshots), but isn’t the x in your 5th line lowercase when it should be uppercase?

1 Like

Note also that your code is not correct for general Unicode (non-ASCII) strings.

  1. Use lastindex and not length to get the last index in a string.

  2. Use prevind(X, a) instead of a-1 to get the previous index in X, and similarly for b.

1 Like

Hey @diwas07!

It seem like you have a typo in the elseif, you probably need capitalize the x.
Because x is not the string you are inputting (but probably some other variable in your environment), Julia doesn’t know how to access it as an array.

Another bug seems to be the final println. In the line above you probably want to assign that to a variable and then print the variable. Instead printing the function itself which doesn’t have the result you have (see below).

julia> function sum(a,b)
       return a+b
       end
sum (generic function with 1 method)
julia> sum(1,2)
3
julia> println("$sum")
sum

Hope this helps and good luck!