39  AI: Let it draft

Let’s have the AI write code we can explain and recognize as correct

This is where we begin using code drafted by the AI. Since you are the one producing your code, you need to do so in a way that lets you stay in control of what is happening. So for starters, we only let it draft pieces of code that are small enough that we can read them, test them, and, most crucially, explain them. Code you cannot read and explain is code you do not understand, and code you do not understand is code you do not control.

This is not to say you can only use code you could have written yourself. It means that you can only use code you can recognize as correct once you see it. You may not be able to produce a decent drawing of a cow, but you can recognize picture-perfect drawing when you see one. Or think of it as the difference between your active and passive English vocabulary. You may not be able to produce a great English poem yourself, but you can recognize that If by Rudyard Kipling is a pretty good one.

Your passive coding skill are much larger than your active one and grows as your active skills improve. The AI is your coding superpower because it is limited only by your passive skill. But crucially, it is limited and the only way to move that limit is to improve your active, and in turn, your passive skill.

TipThe order, every time
  1. Specify. Write the function’s name, its inputs and outputs, and one worked example, before prompting anything. Write one obvious test and one testing some corner-case.
  2. Draft. Ask for exactly that one function, giving it the specification, nothing more.
  3. Test. Subject what it turns to you tests.
  4. Read. Go through what it returns line by line. Make sure you can explain every part of it.

Exercise 39-1

AI: Drafter

A is_palindrome(dna) function that returns True if the DNA string reads the same as its own reverse complement. It should pass this test assert is_palindrome('GAATTC') == True. Write as many tests you can come up with before you prompt the AI.

Exercise 39-2

AI: Drafter

A count_kmer(dna, kmer) function that returns a how many times different a substring (kmer) appears in the larger string (dna). Substrings are allowed to overlap. It should pass this test assert count_kmer('ATGATGC', 'ATG') == 2. Write as many tests you can come up with before you prompt the AI. What if the kmer is not in dna at all? What if dna is shorter than kmer?

Exercise 39-3

AI: Drafter

A most_common_base(dna) function that returns the single most frequent base in a DNA string. It should pass this test most_common_base('GGGATC') == 'G'. Write as many tests you can come up with before you prompt the AI. What if dna is an empty string? What if there are N characters in the dna string?

Exercise 39-4

AI: Drafter

A gc_content(dna) function that returns the fraction of bases that are G or C. It should pass this test assert gc_content('GGCC') == 1.0. Write as many tests you can come up with before you prompt the AI.

Exercise 39-5

AI: Drafter

Specify gc_content(dna) again, but this time specify it to be case-insensitive. I.e., it should count lowercase g/c the same as uppercase. It should pass the tests assert gc_content('GGCC') == 1.0 and assert gc_content('ggcc') == 1.0. Your initial version of the function from the previous exercise may pass the first test but fail teh second. Have the AI draft the updated version and make sure it passes both tests. Then make sure you can explain which changes from the initial version that lets it do that.

Exercise 39-6

AI: Drafter

Specify a small Codon class with a bases attribute holding the three bases and a translate(self) method that returns the corresponding amino acid letter for a short, fixed table of codons (dict) you provide (four or five entries is enough). The method should pass the test assert Codon('ATG').translate() == 'M'. Write as many tests you can come up with before you prompt the AI.

Logbook

For your logbook this week, name the one function or method from today where reading it line by line, before testing, made you suspicious enough to write a nastier test than you otherwise would have. Say what specifically in the code raised your suspicion, and whether the nasty test confirmed it or turned out to be unfounded.