#RNA-Translation Lua

1 messages · Page 1 of 1 (latest)

eager drift
#

Here is my code for the rna-transcription exercise:

local function dna_to_rna(dna)
 if dna == "G" then return "C"
  elseif dna == "C" then return "G"
  elseif dna == "T" then return "A"
  elseif dna == "A" then return "U"
    end
  end
  
return function(dna)
    local rna = {}
    for index = 1, #dna do
     string.sub(dna, index)
     individual_rna = dna_to_rna(dna)
     table.insert(rna, individual_rna)
    end
  return table.concat(rna)
end

It works fine for individual dna fragments, but as soon as there's a string of multiple characters as an input, all it returns is an empty string and I don't understand why. Can anyone explain?

tribal haven
#

Are tables ordered?

#

"AT" and "TA" and "TAA" all need to generate different results

#

Does string.sub allow you to access multiple substrings or will that only work once?

eager drift
# tribal haven Are tables ordered?

I'm not sure what you mean by "are tables ordered"
Sorry I'm very new to all this 😅
string.sub can indeed use multiple substrings as far as I know, I'm just not great at using it conjunction with indices, so there may well be some sort of mistake in there
Also yes, AT, TA, and TAA should all generate different results, I think that aspect of things works fine

tribal haven
#
     string.sub(dna, index)
     individual_rna = dna_to_rna(dna)

With input TAA, what is the value of dna after the first loop iteration?

#

@eager drift ?

eager drift
#

I don't actually know
I am using the lua console that's built into Exercism and haven't gotten around to teaching myself how to test different different inputs etc
As I said, very new to all this
However if I simply copy-paste the code into Visual Studio Code and open the terminal I get an error message for line 12 which reads as follows:
"The return values of this function cannot be discarded."
Idk if that information is any use at all, but it's all I have atm

tribal haven
#

Can you walk me through what you expect your code to do, line by line, when the input is TA?

#

Specifically, what do these lines do?

    for index = 1, #dna do
     string.sub(dna, index)
     individual_rna = dna_to_rna(dna)
     table.insert(rna, individual_rna)
    end
eager drift
#

I figured it out! Your questions made me look through the code again and I was missing a pretty important line
Thank you very much!

tribal haven
#

Congrats! You're very welcome

brisk sleetBOT
#

If everything is resolved, we ask that the author of the top/original post react with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!