Programming: three models compared on code tasks

Below is a side-by-side comparison of models solving the same task in the field of programming.

Generating a code snippet

Task

Generate a Python function that computes the sum of all even numbers in a list.

Prompt:

Write a Python function that takes a list of numbers and returns the sum of all the even ones.

Result:

ChatGPT 4o

Of course! Here is an example of a Python function that takes a list of numbers and returns the sum of all the even ones:
def sum_even_numbers(numbers):

JavaScript
def sum_even_numbers(numbers):
    # Use a generator expression to filter and sum the even numbers
    return sum(num for num in numbers if num % 2 == 0)

# Example of using the function
numbers = [1, 2, 3, 4, 5, 6]
result = sum_even_numbers(numbers)
print("Сумма четных чисел:", result)

In this function, sum_even_numbers uses a generator expression to walk through every element of the list numbers, check whether the number is even (num % 2 == 0), and if it is, add it to the total. The sum function then computes the total of all such even numbers.

Claude 3.5 Sonnet:

Here is a Python function that takes a list of numbers and returns the sum of all the even ones:
def sum_even_numbers(numbers):

JavaScript
def sum_even_numbers(numbers):
    return sum(num for num in numbers if num % 2 == 0)

This function uses a list comprehension and the built-in sum() function. Here is how it works:

  1. The function takes a single argument numbers, which is a list of numbers.2. Inside the function it uses the generator (num for num in numbers if num % 2 == 0):
  2. It walks through every number in the list numbers3.Checks each number for evenness with the condition num % 2 == 04.If the number is even, it is included in the generated sequence5. The sum() function adds up all the numbers produced by the generator.6. The result of the summation is returned by the function.You can use this function as follows:
JavaScript
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = sum_even_numbers(numbers)
print(result)  # Outputs: 30 (2 + 4 + 6 + 8 + 10)

This function is efficient and concise, it processes the list in a single pass and returns the sum of all the even numbers.

Gemini 1.5 Pro:

JavaScript
  """
  This function takes a list of numbers and returns the sum of all the even numbers in the list.
  Args:
    nums: A list of numbers.

  Returns:
    The sum of all the even numbers in the list.
  """
 sum=0
  for num in nums:
    if num % 2 == 0:
      sum += num
  return sum

Here is how this function works:

  1. Defines a function named sum_of_evens that takes a single argument: nums. That argument is the list of numbers that will be passed to the function.
  2. Initializes a variable named sum and sets its value to 0. This variable will be used to hold the sum of the even numbers.
  3. Iterates over the list nums with a for loop. For each number in the list the function:**
    - Checks whether the number divides by 2 without a remainder, using the modulo operator (%).** If the remainder is 0, the number is even.**
    - If the number is even, it is added to the variable sum.**
  4. After iterating over all the numbers in the list, the function returns the value of sum.

Here is an example of using the function:

JavaScript
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_sum = sum_of_evens(numbers)
print(f"Сумма четных чисел: {even_sum})

This method will output:
Сумма четных чисел: 30

Comment:

The task was to generate a Python function that computes the sum of all even numbers in a list. All three models handled it, but the approaches and the explanations differ in style and detail.

  • ChatGPT 4o:
    The generated function uses a generator expression to filter and sum the even numbers in one step.
    ChatGPT gives a clear explanation of how the expression works and adds a usage example with a detailed walkthrough.
    This result is the most concise and carries no surplus information, which makes it a good fit for experienced users.
  • Claude 3.5 Sonnet:
    Claude generated identical code using a list comprehension, but its explanation puts more emphasis on how each expression works and what each part of the code does.
    It gives a detailed account of how each step works, which makes this result useful for beginner programmers.
    It includes an example with a longer set of numbers and a detailed description of the execution process.
  • Gemini 1.5 Pro:
    The function Gemini proposed uses a simpler for loop, where each element of the list is checked for evenness and the sum is built up gradually.
    The code was generated without modern Python constructs (generators, for instance), which makes it slightly less efficient but understandable for anyone just starting out in programming.

Generating a MySQL query

Task:

Create a query that selects all employees earning more than 50,000 from the 'employees' table.

Prompt:

Write a MySQL query to select all employees from the 'employees' table whose salary exceeds 50,000.

Result:

ChatGPT 4o:

Of course! Here is an example of a MySQL query that selects all employees from the employees table whose salary exceeds 50,000:

SQL
SELECT* 
FROM employees 
WHERE salary > 50000;

In this query:

  • SELECT * selects all columns from the table.

  • FROM employees specifies the table the data is selected from.

  • WHERE salary > 50000 filters the rows, keeping only the employees whose salary is above 50,000

Claude 3.5 Sonnet:

Of course! Here is a MySQL query to select all employees from the 'employees' table whose salary exceeds 50,000:

SQL
SELECT* 
FROM employees 
WHERE salary > 50000;

Let's break this query down:

  • SELECT *: this means we want to select all columns from the specified table.
  • FROM employees: specifies that we are selecting data from the table named 'employees'.
  • WHERE salary > 50000: this is the filter condition. It says we want to select only the rows where the value in the 'salary' column is greater than 50000.

This query will return all the data (all columns) for every employee whose salary exceeds 50000. The result will include every field of the 'employees' table for each employee that satisfies the condition.

Gemini 1.5 Pro

SQL
SELECT* 
FROM employees 
WHERE salary > 50000;

Here is the explanation:

  • SELECT means we want to select all columns from the table.
  • FROM employees specifies which table we are selecting the data from.
  • WHERE salary > 50000 - this is the condition that narrows the selection, keeping only the rows where the value in the `salary` column is greater than 50000.

This query will return all the rows from the `employees` table where the employee's salary exceeds 50

Comment:

ChatGPT 4o:

The query was generated correctly:

SQL
SELECT* 
FROM employees 
WHERE salary > 50000;

The model added a short, accurate explanation of how the query works, breaking it into its separate components (SELECT *, FROM, WHERE). This answer is convenient for users because it delivers the information they need briefly and to the point.

Claude 3.5 Sonnet:

The code is practically identical to ChatGPT's version:

SQL
SELECT* 
FROM employees 
WHERE salary > 50000

Claude added a more detailed explanation that covers the same components, but in a fuller form. That makes the answer a good fit for beginners who want more detail about each step of the query.

Gemini 1.5 Pro:

The code is identical too.

Creating a TiKZ diagram

Task:

Generate TiKZ code for a simple drawing in LaTeX.

Prompt:

Create TiKZ code that draws a circle and two intersecting lines.

Result:

ChatGPT4o:

Of course! Here is an example of TiKZ code that draws a circle and two intersecting lines:

Protobuf
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
    % Draw a circle centered at (0,0) with radius 2
    \draw[thick] (0,0) circle (2); 
    % Draw the first line, passing through the center of the circle 
    \draw[thick] (-3,-3) -- (3,3);  
    % Draw the second line, also passing through the center of the circle 
    \draw[thick] (-3,3) -- (3,-3);
\end{tikzpicture}
\end{document}

**This code creates:**‍

  • A circle centered at the point (0,0) with a radius of 2.
  • Two intersecting lines that pass through the center of the circle. One line runs from the point (-3,-3) to (3,3), the other from (-3,3) to (3,-3).

To use this code, copy it into a LaTeX document and compile it with a compiler that supports TikZ, PDFLaTeX for instance.

**Claude 3.5 Sonnet:**‍

**Here is an example of TikZ code that draws a circle and two intersecting lines:**‍

JavaScript
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    % Draw the circle
    \draw (0,0) circle (2cm);
    % Draw the first line
    \draw [-] (-2.5,1) -- (2.5,-1);
    % Draw the second line
    \draw [-] (-2.5,-1) -- (2.5,1);
\end{tikzpicture}
\end{document}

This code will create the following image:

  • A circle centered at the point (0,0) with a radius of 2 cm.
  • A first line running from the point (-2.5,1) to the point (2.5,-1).
  • A second line running from the point (-2.5,-1) to the point (2.5,1).

The lines will intersect at the center of the circle.

**To use this code:**‍

  • Make sure you have LaTeX and the TikZ package installed.
  • Copy this code into a new .tex file.
  • Compile it with pdfLaTeX or another LaTeX compiler.

You will get a PDF file with an image of a circle and two intersecting lines.

Gemini 1.5 Pro:

JavaScript
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) circle (1cm); 
  \draw (-1.5,0) -- (1.5,0); 
  \draw (0,-1.5) -- (0,1.5);
\end{tikzpicture}
\end{document}

This code will create a picture with a circle of radius 1 cm whose center sits at the origin. Two lines will also be drawn: one horizontal and one vertical, intersecting at the center of the circle.

You can change the following parameters:

  • Radius of the circle: change the value 1cm in the command \draw (0,0) circle (1cm); to the radius you need.
  • Position of the circle's center: change the coordinates (0,0) in the command \draw (0,0) circle (1cm); to the coordinates you need.
  • Position of the lines: change the start and end coordinates of each line in the commands \draw (-1.5,0) -- (1.5,0); and \draw (0,-1.5) -- (0,1.5); to the coordinates you need.

Comment:

ChatGPT 4o:

The TiKZ code was generated correctly, and a complete working example was provided:

JavaScript
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw[thick] (0,0) circle (2);
    \draw[thick] (-3,-3) -- (3,3);
    \draw[thick] (-3,3) -- (3,-3);
\end{tikzpicture}
\end{document}

The detailed explanation, describing every element, makes this answer clear and suitable for users with different levels of LaTeX knowledge.

Claude 3.5 Sonnet:

The TiKZ code was also generated correctly:

JavaScript
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
	\draw (0,0) circle (2cm);  
	\draw [-] (-2.5,1) -- (2.5,-1);  
	\draw [-] (-2.5,-1) -- (2.5,1);
\end{tikzpicture}
\end{document}

This answer takes a slightly different approach to positioning the lines, but it also solves the task correctly. The explanation includes a step-by-step description, which makes it useful for users unfamiliar with TiKZ.

Gemini 1.5 Pro:

  • The code is fully functional and correct for drawing a simple circle with intersecting lines.
  • The standard standalone document structure and the use of the tikz package make it easy to compile and render the image.
  • The code is optimized and concise, with no redundant commands or parameters. Every command maps directly onto the drawing tasks and carries nothing extra.
Try it in GPTunneL