avatarNuno Bispo

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4825

Abstract

his problem is <b>545040</b>, so Codex got it <b>right</b>!!</p><p id="c582">This time there was no need to fix/correct the code generated by Codex, it both run and returned the correct response on the first try.</p><p id="2455">A quick analysis of the code shows a function to check if a number is a palindrome, filtering the range sequence to only consider those numbers and a final sum. Overall a good piece of code.</p><p id="2e20">It passes the second test!!!</p><h1 id="8d59">Challenge 3— Legionnaries</h1><p id="4b06">Now for a more abstract problem, let’s use roman numberal numbers.</p><p id="a9c1">In the range 1–13 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) the digit 1 occurs 6 times.</p><p id="bba4">Let’s ask Codex to solve the following challenge:</p><p id="1725">“In the range, 1–2660 (half the number of Romans in a legion), expressed in Roman numerals, how many times does the numeral “X” occur?”</p><figure id="7b20"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ReSRDZIS8tjTNQLHI0T_PA.png"><figcaption>OpenAI Codex Playground — Legionnaries — Image by Author</figcaption></figure><p id="809a">The generated code:</p><div id="4c06"><pre><span class="hljs-keyword">import</span> re

<span class="hljs-keyword">def</span> <span class="hljs-title function_">make_romans</span>(<span class="hljs-params">n</span>): <span class="hljs-string">"""Convert integers to Roman numerals."""</span> ints = (<span class="hljs-number">1000</span>, <span class="hljs-number">900</span>, <span class="hljs-number">500</span>, <span class="hljs-number">400</span>, <span class="hljs-number">100</span>, <span class="hljs-number">90</span>, <span class="hljs-number">50</span>, <span class="hljs-number">40</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>, <span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>) nums = (<span class="hljs-string">'M'</span>, <span class="hljs-string">'CM'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'CD'</span>,<span class="hljs-string">'C'</span>, <span class="hljs-string">'XC'</span>,<span class="hljs-string">'L'</span>,<span class="hljs-string">'XL'</span>,<span class="hljs-string">'X'</span>,<span class="hljs-string">'IX'</span>,<span class="hljs-string">'V'</span>,<span class="hljs-string">'IV'</span>,<span class="hljs-string">'I'</span>) result = [] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(ints)): count = <span class="hljs-built_in">int</span>(n / ints[i]) result.append(nums[i] * count) n -= ints[i] * count <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>.join(result)

<span class="hljs-keyword">def</span> <span class="hljs-title function_">count_x</span>(<span class="hljs-params">s</span>): <span class="hljs-string">"""Count occurrences of 'X' in a string."""</span> <span class="hljs-keyword">return</span> <span class="hljs-built_in">len</span>(re.findall(<span class="hljs-string">r'[Xx]'</span>, s))

<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>(): <span class="hljs-string">"""Count occurrences of 'X' in Roman numerals."""</span> num = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2660</span>): num += count_x(make_romans(i)) <span class="hljs-built_in">print</span>(num)

<span class="hljs-keyword">if</span> name == <span class="hljs-string">"main"</span>: main()</pre></div><p id="9287">The output:</p><div id="729c"><pre><span class="hljs-number">3976</span></pre></div><p id="b97b">The answer for this problem is <b>3977</b>, so Codex got it <b>wrong</b> !!</p><p id="e0e6">A quick analysis of the code shows a good division of functions, one to calculate the roman numeral, another to check the presence of ‘X’ and a main function for the range and summing of the values.</p><p id="b9b1">There is however a small error, due to the fact that range is exclusive in the stop value, the sequence only goes to 2659 and 2660 in roman numerals is MMDCLX.</p><p id="4da9">This means that the code generated by Codex fails to account for the last ‘X’ in the last value because it doesn’t implement correctly the upper limit.</p><p id="87b1">It fails the third test!!!</p><h1 id="2894">Conclusion</h1><p id="0d3d">What can we conclude from testing OpenAI’s Codex with these programming challenges?</p><p id="ee6e">In all 3 examples, the produced code is high quality, some of it

Options

even with comments detailing the purpose of each function.</p><p id="5612">Functions to properly do code separation were used and overall the logic is sane and except for the last test, all of them produced the correct result.</p><p id="ecbc">Watching the OpenAI live demo and see how they use Codex to quickly produce a game in a conversational style interaction with Codex is quite powerfull, check it out:</p> <figure id="03ec"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSGUCcjHTmGY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSGUCcjHTmGY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FSGUCcjHTmGY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><p id="52e8">Does this mean that our jobs as a programmer are over?</p><p id="692e">I don’t think so. Yes, Codex can generate high quality code and the more it is being used, the more it will learn and improve the code generation patterns.</p><p id="25db">But programming is still an art that requires (sometimes) complex pattern thinking (what AI is good at) but also understanding of the end goal of the requirements (something AI struggles with).</p><p id="7228">I believe the future is more something like GitHub’s Copilot, human and AI working together, were humans set the stage and AI completes the boring (and repetitive) parts of programming.</p><p id="47d5">Check out GitHub’s Copilot at: <a href="https://copilot.github.com/">https://copilot.github.com/</a></p><p id="b6e3">You can also check out some of my other stories:</p><div id="82d8" class="link-block"> <a href="https://readmedium.com/how-to-deploy-a-python-api-with-fastapi-with-nginx-and-docker-1328cbf41bc"> <div> <div> <h2>The cheap way how to use Docker to deploy your FastAPI</h2> <div><h3>FastAPI is a powerful API framework for Python that allows to quickly create and develop APIs in Python. But where to…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*du7p50wS_fIsaC_lR18qsg.png)"></div> </div> </div> </a> </div><div id="dfdc" class="link-block"> <a href="https://readmedium.com/how-to-build-a-django-membership-site-with-payment-integration-part-1-163552292aed"> <div> <div> <h2>Django and Stripe subscriptions — Part 1</h2> <div><h3>So you want to build a membership site or charge for your digital product/downloads and don’t know what tool to use…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*SCnrYXsd_oqPwZ1or9uyag.png)"></div> </div> </div> </a> </div><div id="34a1" class="link-block"> <a href="https://readmedium.com/python-shorts-nlp-sentiment-analysis-and-text-summary-a5988ce81513"> <div> <div> <h2>Python Shorts — NLP Sentiment Analysis and Text Summary</h2> <div><h3>What if you could do sentiment analysis and text summaries with Python and NLP without the need of expensive APIs from…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*ZxGjdUe_1zJFk9Zf)"></div> </div> </div> </a> </div><p id="e7f1">Follow me on Twitter: <a href="https://twitter.com/DevAsService">https://twitter.com/DevAsService</a></p><p id="b394">Check out my website at: <a href="https://developer-service.io/">https://developer-service.io/</a></p><p id="455f">If you enjoyed reading this article and found it usefull, you can support me by <a href="https://blog.developer-service.io/membership">signing up for a Medium membership</a> (if you are not a member). It will only cost you $5 a month — this will give you access to all stories on Medium! (and I will receive a small commission)</p><p id="acfb">Besides that, if you want to stay updated when I post a new story, you can <a href="https://blog.developer-service.io/subscribe">signup for my free newsletter</a>!</p></article></body>

Using OpenAI Codex to solve programing challenges

OpenAI’s Codex has opened the world to the general use of AI to write code, but just how good is it at coding?

OpenAI Codex logo

What is OpenAPI Codex ?

OpenAI’s Codex is an AI system that translates natural language to code.

This means that we can supply it with a description of the intended problem to solve and the AI will genereate the corresponding code.

It is proficient in more than a dozen programming languages and powers the GitHub Copilot funcionality.

OpenAI release the public beta access to Codex with their API, you can register at: https://openai.com/join

How are we going to test it ?

I’ve recently obtain access to the public beta and have been testing Codex and it’s code generation capabilities.

According to the documentation, Codex, is more proficient on Python but can easily generate code in any programming language.

One of the ways to test the code generation capabilities is to solve coding challenges.

In this article I will go trough a series of programming challenges, ask Codex to solve them and then run and check how good is the generated code.

I will use OpenAI Playground feature to interact with Codex and use the ‘davinci-codex’ model (the most powerful one) to generate the code.

Challenge 1 — Fibonacci

The Fibonacci sequence is a well know and common algorithm to solve with code and begins like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 (each number is the sum of the previous two).

Let’s ask Codex to solve the following challenge:

“What is the sum of all odd numbers in the Fibonacci sequence that are less than 10,000?”

OpenAI Codex Playground — Fibonacci — Image by Author

The generated code:

def fib():
    a = 0
    b = 1
    while a <= 10000:
        a, b = b, a + b
        if a % 2 == 1:
            yield a

print(sum(fib()))

The output:

14328

The answer for this problem is 14328, so Codex got it right!!

But let’s check a small issue with the generated code, the line 11 on the Codex Playground output doesn’t work, it needs to be corrected with the missing parenthesis.

Other than that small issue, Codex solves the problem correcty and with some good designed code.

It passes the first test!!!

Challenge 2 — Palindromes

A palindrome is a word, number, phrase, or another sequence of characters which reads the same backward as forward, such as madam, racecar, or the number 10801.

Let’s ask Codex to solve the following challenge:

“What is the sum of all numeric palindromes that are less than 10,000?”

OpenAI Codex Playground — Palindromes — Image by Author

The generated code:

def is_palindrome(num):
    num = str(num)
    return num == num[::-1]

def main():
    numbers = [x for x in range(1,10000) if is_palindrome(x)]
    print(sum(numbers))

if __name__ == '__main__':
    main()

The output:

545040

The answer for this problem is 545040, so Codex got it right!!

This time there was no need to fix/correct the code generated by Codex, it both run and returned the correct response on the first try.

A quick analysis of the code shows a function to check if a number is a palindrome, filtering the range sequence to only consider those numbers and a final sum. Overall a good piece of code.

It passes the second test!!!

Challenge 3— Legionnaries

Now for a more abstract problem, let’s use roman numberal numbers.

In the range 1–13 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) the digit 1 occurs 6 times.

Let’s ask Codex to solve the following challenge:

“In the range, 1–2660 (half the number of Romans in a legion), expressed in Roman numerals, how many times does the numeral “X” occur?”

OpenAI Codex Playground — Legionnaries — Image by Author

The generated code:

import re

def make_romans(n):
    """Convert integers to Roman numerals."""
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = []
    for i in range(len(ints)):
        count = int(n / ints[i])
        result.append(nums[i] * count)
        n -= ints[i] * count
    return ''.join(result)

def count_x(s):
    """Count occurrences of 'X' in a string."""
    return len(re.findall(r'[Xx]', s))

def main():
    """Count occurrences of 'X' in Roman numerals."""
    num = 0
    for i in range(1, 2660):
        num += count_x(make_romans(i))
    print(num)

if __name__ == "__main__":
    main()

The output:

3976

The answer for this problem is 3977, so Codex got it wrong !!

A quick analysis of the code shows a good division of functions, one to calculate the roman numeral, another to check the presence of ‘X’ and a main function for the range and summing of the values.

There is however a small error, due to the fact that range is exclusive in the stop value, the sequence only goes to 2659 and 2660 in roman numerals is MMDCLX.

This means that the code generated by Codex fails to account for the last ‘X’ in the last value because it doesn’t implement correctly the upper limit.

It fails the third test!!!

Conclusion

What can we conclude from testing OpenAI’s Codex with these programming challenges?

In all 3 examples, the produced code is high quality, some of it even with comments detailing the purpose of each function.

Functions to properly do code separation were used and overall the logic is sane and except for the last test, all of them produced the correct result.

Watching the OpenAI live demo and see how they use Codex to quickly produce a game in a conversational style interaction with Codex is quite powerfull, check it out:

Does this mean that our jobs as a programmer are over?

I don’t think so. Yes, Codex can generate high quality code and the more it is being used, the more it will learn and improve the code generation patterns.

But programming is still an art that requires (sometimes) complex pattern thinking (what AI is good at) but also understanding of the end goal of the requirements (something AI struggles with).

I believe the future is more something like GitHub’s Copilot, human and AI working together, were humans set the stage and AI completes the boring (and repetitive) parts of programming.

Check out GitHub’s Copilot at: https://copilot.github.com/

You can also check out some of my other stories:

Follow me on Twitter: https://twitter.com/DevAsService

Check out my website at: https://developer-service.io/

If you enjoyed reading this article and found it usefull, you can support me by signing up for a Medium membership (if you are not a member). It will only cost you $5 a month — this will give you access to all stories on Medium! (and I will receive a small commission)

Besides that, if you want to stay updated when I post a new story, you can signup for my free newsletter!

Techonology
Programming
AI
OpenAI
Python
Recommended from ReadMedium