I Can Fly With You: Python Part 8

I Can Fly With You: Python Part 8

This morning I got to meditate with my meditation group from around the world. It was beautiful to sit in peace, meditation, beauty and silence.

The truth is that we are all friends.

We are all friends who haven't met each other yet. There are no strangers - only friend we haven't met yet. W. B. Yeates said it: "There are no strangers here; Only friends you haven't yet met." If you only ever take one thing away from my blogs, take this.

An image of a rock with a rainbow filter and the quote from WB Yeates: "There are no strangers here; only friends you haven't yet met."


3 blogs; well over 300 posts; years and years and hours and hours of writing. 

If you take only one thing away; take this.

p.s. why the blog title? it's from a song I wrote about Alan Turing earlier this year and I hope to share it soon.

It's about continuing his legacy...

Complementary DNA

Again, I've said goodbye to the level 8 kyu problems (too easy!) and kissed the level 6 kyu problems goodbye (too hard! at least for now).

I have solved the Complementary DNA problem today. 

That was a lot of fun! Nice and easy.

I thought that I could solve it in 5 minutes and I did - except that I had to realise that both the input and the output type were actually strings, not list. And I therefore had to call the join() method on my list. Also I made a spelling mistake.

My purpose is to solve problems

old way.

And so here is my first solution to the problem.
def DNA_strand(dna: str) -> str:
    complementary_strand = []
    for side in dna:
        if side == "A":
            complementary_strand.append("T")
        elif side == "T":
            complementary_strand.append("A")
        elif side == "C":
            complementary_strand.append("G")
        else:
            complementary_strand.append("C")
    complementary_strand_list = ''.join(complementary_strand)
    return complementary_strand_list

But before I even submit - even I know that this could probably be done better.

However, having said that, I am really impressed at my growing levels of confidence - coding seems less and less scary and mysterious and I can usually get more and more confident that something has worked. Basically - I have demystified it just a little bit for myself. Wow.

I can already see that there could've been a more intelligent way to do this - using .join() and something else. 

But I still can't place it, so shall we find out? As it is nearly 11 pm here!

My purpose is to solve problems elegantly

And so I am looking for one line solutions to these problems.

Alternative Solution 1:

import string
def DNA_strand(dna):
    return dna.translate(string.maketrans("ATCG","TAGC"))
    # Python 3.4 solution || you don't need to import anything :)
    # return dna.translate(str.maketrans("ATCG","TAGC"))
So what is this one doing please? Thank you. Well before I can do anything else, I will find our what translate() is (and maketrans()). And then I'm going to bed. And continuing tomorrow.

From W3 Schools then:

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.

So far, so good:

Use the maketrans() method to create a mapping table.

Aaaaaargh!

The next day: 

Oh no I had forgotten what an insanely incredibly difficult note I had left this on. Drat!

Oh apparently this is written for Python 2! Glad I checked.

"It looks like the code snippet you've provided is written for Python 2. To make it work in Python 3, you should use str.maketrans instead of string.maketrans." (From Chat GPT).

WAIT WHAT? OKAY SO: This is the sickest method and the coolest method that I have ever seen in the world. This method:
  • Literally allows you to make a table of comparions
  • Literally allows you to translate across the two options. 
Okay so, WOW.

They have literally created a table where A maps to T, T maps to A, C maps to G, and then G maps to C.

That is what .maketrans() does.

And then translate() makes that translation.

THIS IS THE COOLEST METHOD THAT I HAVE EVER SEEN! I LOVE PYTHON! Okay I did not imagine that something like this was even possible. I LOVE PYTHON! Okay, WOW!

So in summary:

  • this solution takes a string dna
  • it calls the .translate() method on dna
  • it calls the str.maketrans() method inside of that, giving the two parameters that need to be mapped. Oh okay then - wow!!
  • It returns the translation of that. Okay, then, wow! Wow wow wow wow wow wow wow wow WOW.
They didn't have that in JavaScript... at least not that I was aware of or that I ever saw...

Alternative Solution 2:

Okay, so what is going on here please? Thanks.

And actually it looks like solutions two and three are almost identical. Whoops! The phrasing is different so let me compare the two of them together (solution 3 is below).
  • Solution two defines a dictionary - pairs. 
  • It then calls the function DNA_strand on the input string of dna and then it joins a list as a string - which it then joins and then returns (that list).
  • BUT; WHAT HAPPENS INSIDE OF THE JOIN FUNCTION???!
    • Well this is called a list comprehension
    • We normally start by reading at the "for" statement
    • So for every letter ('x') in dna,
      • We append to the list the value of what "x" is in the KEY-VALUE PAIR
      • So x will be the key
      • And whatever it is that will be accessed and therefore appended to the list will be the corresponding value on the dictionary called "pairs"
  • Then all of this list is joined into a string
  • And then this string is returned and it is the solution
pairs = {'A':'T','T':'A','C':'G','G':'C'}
def DNA_strand(dna):
    return ''.join([pairs[x] for x in dna])

Let's see below...

Alternative Solution 3

This solution is basically the same one as above.

def DNA_strand(dna):
    reference = { "A":"T",
                  "T":"A",
                  "C":"G",
                  "G":"C"
                  }
    return "".join([reference[x] for x in dna])

  • The only difference is that the dictionary is defined in the function body itself and not outside of the function, and in the global scope
  • Each strand of dna is a key in a key value pair
  • Each complementary strand is the corresponding value in a key-value pair
  • The list comprehension builds up a new list
  • So inside of the list comprehension we loop through the string dna
  • For every letter of the string dna, we append the correspending value of the dictionary by accessing reference through dictionary notation
  • so we access reference
  • but then we use [square brackets] to access the corresponding key that we want
  • and then this returns a value and we append it to the list
  • Which we then join into a string
  • And we return

How many solutions can I handle?

I would say that capping at 3 is a good thing.

I would probably be better off doing 1 or 2 but I find it so interesting that I just can't stop.

Anyway, thank you!!! I will just go for the number of solutions that it makes sense to follow through and to study.

OH HOLY MOLY I JUST SAW THE MOST BEAUTIFUL WIND FARM (I'M ON THE TRAIN) SOMEWHERE NORTH OF LONDON OKAY BYE (WIND FARMS!!!!) 😍😍

Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing