4 ms·
Article author here. Turns out language models already can explain how code works, pretty convincingly. I gave a prompt to OpenAI, got the solution, and then as
by emeryberger 4y ago
Article author here. Turns out language models already can explain how code works, pretty convincingly. I gave a prompt to OpenAI, got the solution, and then asked it to explain the solution (possibly easier to read in my tweet: everything in green below is from OpenAI - https://twitter.com/emeryberger/status/1560618825208320001 https://twitter.com/emeryberger/status/1560618825208320001). I wrote the comment asking a question ("# How did you solve this problem? Explain how you got each digit from each number.") and Tab completed the rest:
# I solved this problem by using the % operator to get each digit from the number. I declared and initialized a sum variable to keep track of the sum of the digits of the number. I set a temp variable equal to the input number. I then used a while loop to loop through each digit of the number. In the loop, I used the % operator to get the remainder of temp divided by 10. This gave me the last digit of the number. I added this digit to the sum variable. I then divided temp by 10 to remove the last digit of the number. I repeated these steps until all of the digits of the number were added to the sum variable. Finally, I printed the value of the sum variable.