davecom/ClassicComputerScienceProblemsInPython

nucleotides dictionary

GreatBahram opened this issue · 3 comments

Hi, first of all thanks for your great book.
You could just save the nucleotide inside a dictionary, instead of iterating on each one.

NUCLEOTIDES_MAP = {
    'A': 0b00,
    'C': 0b01,
    'G': 0b10,
    'T': 0b11,
}
RMAP = {v: k for k, v in NUCLEOTIDES_MAP.items()}

for nucleotide in gene.upper():
self.bit_string <<= 2 # shift left two bits
if nucleotide == "A": # change last two bits to 00
self.bit_string |= 0b00
elif nucleotide == "C": # change last two bits to 01
self.bit_string |= 0b01
elif nucleotide == "G": # change last two bits to 10

Moreover, you could pass self.bit_string to bin function which returns a binary string representation. Then split the text into two-character group, and each of which would be a nucleotide. What's the benefit? In this case, you don't need to bitwise anything or reverse the output.

You're totally right, last paragraph make everything clear.

Hi @GreatBahram ,
Sorry for not replying sooner. Yes, I was going to point you to that paragraph.
Thanks for reading,
Dave