How AI Can Make You a Better Programmer

June 9, 2022

We’ve all been blown away at the power of OpenAI’s Generative Pre-trained Transformer-3 in domains such as copywriting and summarization (ie copy.ai). Here’s another one, which I’m sure will be turned into a great SaaS product soon: programing assitant.

As a data scientist and developer, I’m not particularly interested in solving problems that are already solved, so when I need to write some code for something I would consider boring albeit not immediately obvious, I realized I could use GPT-3’s codex model to help out.

Let me explain with an example. A lot of times coding interview questions are boring and are just a proxy to tell if you have some baseline knowledge. Here’s an example from this site:

“Write a code in Python to create a Morse code translator. You can take a string with alphanumeric characters in lower or upper case. The string can also have any special characters as a part of the Morse code. Special characters can include commas, colons, apostrophes, exclamation marks, periods, and question marks. The code should return the Morse code that is equivalent to the string.”

I can put this into the OpenAI playground and here is what I get:

"""
Write a code in Python to create a Morse code translator. You can take a string with alphanumeric characters in lower or upper case. The string can also have any special characters as a part of the Morse code. Special characters can include commas, colons, apostrophes, exclamation marks, periods, and question marks. The code should return the Morse code that is equivalent to the string.
"""
def morse_code(string):
   morse_code_dict = {
       "a": ".-",
       "b": "-...",
       "c": "-.-.",
       "d": "-..",
       "e": ".",
       "f": "..-.",
       "g": "--.",
       "h": "....",
       "i": "..",
       "j": ".---",
       "k": "-.-",
       "l": ".-..",
       "m": "--",
       "n": "-.",
       "o": "---",
       "p": ".--.",
       "q": "--.-",
       "r": ".-.",
       "s": "...",
       "t": "-",
       "u": "..-",
       "v": "...-",
       "w": ".--",
       "x": "-..-",
       "y": "-.--",
       "z": "--..",
       "1": ".----",
       "2": "..---",
       "3": "...--",
       "4": "....-",
       "5": ".....",
       "6": "-....",
       "7": "--...",
       "8": "---..",
       "9": "----.",
       "0": "-----",
       ",": "--..--",
       ".": ".-.-.-",
       "?": "..--..",
       "/": "-..-.",
       "@": ".--.-.",
       " ": " ",
   }
   morse_code_string = ""
   for char in string:
       morse_code_string += morse_code_dict[char]
   return morse_code_string
print(morse_code("hello"))
print(morse_code("hello world"))
print(morse_code("hello world, how are you?"))

Almost a perfect answer. It doesn’t have all the special characters, and misses the upper case entirely, but the task is basically solved and I didn’t have to waste brainpower on it. So aside from students using this tool to cheat on their homework, using GPT-3 as a development aid is going to be a game-changer for a lot of people. It is for me.


Thomas Duquemin