Last active
August 29, 2015 14:17
-
-
Save JFFail/a4663dec248b9334fdd1 to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer #207
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/env/ruby | |
#Reddit Daily Programmer 207 | |
#Includes bonus. | |
#Function for parsing a protein sequence. | |
def proteins(dna) | |
#Create hash for the codons. | |
codons = {"TTT"=>"Phe","TTC"=>"Phe","TTA"=>"Leu","TTG"=>"Leu","TCT"=>"Ser","TCC"=>"Ser","TCA"=>"Ser","TCG"=>"Ser","TAT"=>"Tyr","TAC"=>"Tyr","TAA"=>"STOP","TAG"=>"STOP","TGT"=>"Cys","TGC"=>"Cys","TGA"=>"STOP","TGG"=>"Trp","CTT"=>"Leu","CTC"=>"Leu","CTA"=>"Leu","CTG"=>"Leu","CCT"=>"Pro","CCC"=>"Pro","CCA"=>"Pro","CCG"=>"Pro","CAT"=>"His","CAC"=>"His","CAA"=>"Gln","CAG"=>"Gln","CGT"=>"Arg","CGC"=>"Arg","CGA"=>"Arg","CGG"=>"Arg","ATT"=>"Ile","ATC"=>"Ile","ATA"=>"Ile","ATG"=>"Met","ACT"=>"Thr","ACC"=>"Thr","ACA"=>"Thr","ACG"=>"Thr","AAT"=>"Asn","AAC"=>"Asn","AAA"=>"Lys","AAG"=>"Lys","AGT"=>"Ser","AGC"=>"Ser","AGA"=>"Arg","AGG"=>"Arg","GTT"=>"Val","GTC"=>"Val","GTA"=>"Val","GTG"=>"Val","GCT"=>"Ala","GCC"=>"Ala","GCA"=>"Ala","GCG"=>"Ala","GAT"=>"Asp","GAC"=>"Asp","GAA"=>"Glu","GAG"=>"Glu","GGT"=>"Gly","GGC"=>"Gly","GGA"=>"Gly","GGG"=>"Gly"} | |
#Now loop through. | |
counter = 0 | |
while counter < (dna.length - 2) | |
codon = dna[counter] + dna[counter+1] + dna[counter+2] | |
if codons[codon] == "Met" | |
break | |
end | |
counter += 1 | |
end | |
#Create an array to store the proteins. | |
proteins = Array.new | |
#Now process until we hit a STOP. | |
while counter < (dna.length - 2) | |
current_vals = dna[counter] + dna[counter+1] + dna[counter+2] | |
current_prot = codons[current_vals] | |
proteins.push(current_prot) | |
end | |
counter += 1 | |
end | |
#Create an array to store the proteins. | |
proteins = Array.new | |
#Now process until we hit a STOP. | |
while counter < (dna.length - 2) | |
current_vals = dna[counter] + dna[counter+1] + dna[counter+2] | |
current_prot = codons[current_vals] | |
proteins.push(current_prot) | |
#Increment the counter. | |
counter += 3 | |
#Break if we hit a stop. | |
if current_prot == "STOP" | |
break | |
end | |
end | |
#Print it. | |
puts proteins.join("-") | |
end | |
#Main code begins here with the input sequence. | |
sequence = "A T G T T T C G A G G C T A A" | |
sequence_arr = sequence.split(" ") | |
result = "" | |
#Break if we hit a stop. | |
if current_prot == "STOP" | |
break | |
end | |
end | |
#Print it. | |
puts proteins.join("-") | |
end | |
sequence = "A T G T T T C G A G G C T A A" | |
sequence_arr = sequence.split(" ") | |
result = "" | |
counter = 0 | |
#Loop through the sequence values. | |
sequence_arr.each do |item| | |
#Add a space if we're not at the first instance. | |
if counter != 0 | |
result += " " | |
end | |
#Figure out the corresponding value. | |
if item == "A" | |
result += "T" | |
elsif item == "T" | |
result += "A" | |
elsif item == "G" | |
result += "C" | |
else | |
result += "G" | |
end | |
counter += 1 | |
end | |
#Print the final results. | |
puts "#{sequence}" | |
proteins(sequence_arr) | |
puts "#{result}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment