An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."
Given: A DNA string
sof length at most 1000 nt.
Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in
20 12 17 21
Since I'm still on my quest to learn both regex and Ruby, I decided to go that route:
def countACGT(str) list = [0,0,0,0] str.scan(/A|C|G|T/) do |sub| if sub == "A" list += 1 end if sub == "C" list += 1 end if sub == "G" list += 1 end if sub == "T" list += 1 end end return list end
I'm not a big fan of long
if chains. Luckily, Ruby has a
case statement as well:
def countACGT(str) list = [0,0,0,0] str.scan(/A|C|G|T/) do |sub| puts case sub when "A" list += 1 when "C" list += 1 when "G" list += 1 when "T" list += 1 end end return list end
Both can be invoked like:
Yes, the dataset I had to solve was that long.
However, my interpreter suddenly feels the need to print the updated
list[x] every time a new value gets assigned. This leads to a significant decrease in performance.
So I downloaded the official Ruby interpreter via the downloader (version 2.2.3 (x64)) and the exact same thing happens.
It almost looks like
case is not the preferred way of doing this, but that's not intuitive.
I'm mainly looking for a definitive answer on which version I should stick with (and why) and general maintainability improvements. I'm perfectly aware regex may not be the optimal solution here, but I'd like to stick with it anyway.