Code in English — Part 1

Panna Lal Patodia
7 min readSep 8, 2022

--

Generate Code from Plain English in Multiple Languages

“Code in English” is the new paradigm of software development

https://www.fastdevai.com

AI Code Generator from English Description

Introduction:

Generating code from plain English description is the holy grail of programming and computer scientists are working on this since many years. However, most projects failed completely. English or any human language for that matter is quite vague and computer requires fully accurate and succinct instructions to perform the task. However, today we can achieve this goal to a large extent. The only requirement is to provide clear and concrete descriptions in plain English.

We are introducing a revolutionary product “Code in English” that can generate code from plain English in multiple languages (Please visit https://www.fastdevai.com for more details). Currently, we support Python, JavaScript, Java, C, C++, C#, PHP, Go, Perl, Ruby, Typescript, Haskell, Kotlin, Scala, Swift and Dart languages. This improves the productivity of the software developers by 2 to 3 times. It can help immensely by reducing the cost, improving profitability, and delivering the product much faster. It will also help software developers of your company to learn new programming languages in a very innovative way.

Apart from generating code, it can explain the code, translate code from one language to another language, generate SQL and much more.

Let us take a simple example. Three arrays are given. We are required to merge these three arrays into a single array and then compute the minimum, maximum, mean, median and standard deviation from the merged array.

Let us specify the problem in clear terms:

Given three arrays:

a = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99]

b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68]

c = [9.24, 8.87, 2.72, -4.08, -3.7]

Merge array a, b and c into array d. Then sort array d. Now, compute minimum, maximum, mean, median and standard deviation from array d.

Why is the additional statement given to sort the array d?

First, for computing median, it is mandatory to sort the array. Secondly, once the array is sorted in ascending order, it becomes amazingly easy to compute minimum and maximum. The first element of the sorted array is the minimum value, and the last element of the sorted array is the maximum value.

We have generated the code in the following programming languages to limit the length of the article:

  1. Python
  2. PHP
  3. C++
  4. Java, and
  5. JavaScript

Let us generate Python program from the above description and the following code is generated:

import numpy as npa = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99]
b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68]
c = [9.24, 8.87, 2.72, -4.08, -3.7]
d = np.concatenate((a,b,c))
d.sort()
print("Minimum: ", d.min())
print("Maximum: ", d.max())
print("Mean: ", d.mean())
print("Median: ", np.median(d))
print("Standard Deviation: ", d.std())

On running the above code, we get the following output:

Output:
Minimum: -4.08
Maximum: 9.24
Mean: 2.465652173913043
Median: 2.23
Standard Deviation: 3.9781346142949703

Next, we generated the code in PHP using the same description and the following code was generated:

<?php$a = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99];
$b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68];
$c = [9.24, 8.87, 2.72, -4.08, -3.7];
$d = array_merge($a, $b, $c);
sort($d);
$min = min($d);
$max = max($d);
$mean = array_sum($d) / count($d);
$median = $d[count($d) / 2];
$sum = 0;
foreach ($d as $value) {
$sum += pow($value - $mean, 2);
}
$std = sqrt($sum / count($d));
echo "Minimum: $min\n";
echo "Maximum: $max\n";
echo "Mean: $mean\n";
echo "Median: $median\n";
echo "Standard Deviation: $std\n";
?>

On running the above code, we got the following result:

Min: -4.08
Max: 9.24
Mean: 2.465652173913
Median: 2.23
Standard Deviation: 3.978134614295

C++ Code: The code was generated from the same description

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;int main()
{
vector<double> a = {3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99};
vector<double> b = {-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68};
vector<double> c = {9.24, 8.87, 2.72, -4.08, -3.7};
vector<double> d;
d.reserve(a.size() + b.size() + c.size());
d.insert(d.end(), a.begin(), a.end());
d.insert(d.end(), b.begin(), b.end());
d.insert(d.end(), c.begin(), c.end());
sort(d.begin(), d.end());
double min = d[0];
double max = d[d.size() - 1];
double mean = 0;
for (int i = 0; i < d.size(); i++)
{
mean += d[i];
}
mean /= d.size();
double median = 0;
if (d.size() % 2 == 0)
{
median = (d[d.size() / 2] + d[d.size() / 2 - 1]) / 2;
}
else
{
median = d[d.size() / 2];
}
double sd = 0;
for (int i = 0; i < d.size(); i++)
{
sd += pow(d[i] - mean, 2);
}
sd = sqrt(sd / d.size());
cout << "min: " << min << endl;
cout << "max: " << max << endl;
cout << "mean: " << mean << endl;
cout << "median: " << median << endl;
cout << "sd: " << sd << endl;
return 0;
}

We have compiled the above code and then executed it. Below, we display the output:

Output:
min: -4.08
max: 9.24
mean: 2.46565
median: 2.23
sd: 3.97813

Java Code: Next we generated the Java code

import java.util.Arrays;
import java.util.Scanner;
public class ArrayMerge {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] a = {3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99};
double[] b = {-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68};
double[] c = {9.24, 8.87, 2.72, -4.08, -3.7};
double[] d = new double[a.length + b.length + c.length];
int i = 0;
for (double x : a) {
d[i] = x;
i++;
}
for (double x : b) {
d[i] = x;
i++;
}
for (double x : c) {
d[i] = x;
i++;
}
Arrays.sort(d);
double min = d[0];
double max = d[d.length - 1];
double mean = 0;
for (double x : d) {
mean += x;
}
mean /= d.length;
double median = 0;
if (d.length % 2 == 0) {
median = (d[d.length / 2] + d[d.length / 2 - 1]) / 2;
} else {
median = d[d.length / 2];
}
double sd = 0;
for (double x : d) {
sd += Math.pow(x - mean, 2);
}
sd = Math.sqrt(sd / d.length);
System.out.println("Minimum: " + min);
System.out.println("Maximum: " + max);
System.out.println("Mean: " + mean);
System.out.println("Median: " + median);
System.out.println("Standard Deviation: " + sd);
}
}

We have compiled and executed the code. The output is given below:

Output:
Minimum: -4.08
Maximum: 9.24
Mean: 2.465652173913043
Median: 2.23
Standard Deviation: 3.9781346142949703

JavaScript Code: Next, we have generated the JavaScript code. Below, we display the code:

var a = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99];
var b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68];
var c = [9.24, 8.87, 2.72, -4.08, -3.7];
var d = a.concat(b, c);
d.sort(function(a, b) {
return a - b;
});
var min = d[0];
var max = d[d.length - 1];
var mean = d.reduce(function(a, b) {
return a + b;
}) / d.length;
var median = d[Math.floor(d.length / 2)];
var std = Math.sqrt(d.map(function(x) {
return Math.pow(x - mean, 2);
}).reduce(function(a, b) {
return a + b;
}) / (d.length)); // Changed from (d.length - 1) to (d.length)
console.log("min: " + min);
console.log("max: " + max);
console.log("mean: " + mean);
console.log("median: " + median);
console.log("std: " + std);

It should be noted that instead of computing standard deviation, the code was computing population standard deviation, and this resulted in a slightly different result.

Output:
min: -4.08
max: 9.24
mean: 2.465652173913043
median: 2.23
std: 4.067542061119016

When we changed the code from (d.length — 1) to (d.length). This means we have computed standard deviation using the formula: sqrt((x-mean)²/n) where n is the number of elements in the array. After this change, the result obtained was

Output:
Minimum: -4.08
Maximum: 9.24
Mean: 2.465652173913043
Median: 2.23
Standard Deviation: 3.9781346142949703

We observe that the result obtained is similar except for the number of decimals in Mean and Standard Deviation. We see that C++ program has generated the value of Mean and Standard Deviation correct to five decimal places. So, we added the following statement:

Compute mean and standard deviation correct to ten decimal places.

The code was remarkably similar to the original C++ code except for the following two lines:

cout << "mean: " << setprecision(10) << mean << endl;
cout << "std: " << setprecision(10) << std << endl;

On compilation and execution of the above code, we got the following output:

Output:
min: -4.08
max: 9.24
mean: 2.465652174
median: 2.23
std: 3.978134614

From the above, we still got the result that is correct to nine decimal places.

If we go through the Python code carefully, we observe that numpy library is used. Can we generate the Python code wherein it computes minimum, maximum, mean etc. without using this library. We have given the following English description to generate the new Python code:

Given three arrays:
a = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99]
b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68]
c = [9.24, 8.87, 2.72, -4.08, -3.7]
Merge array a, b and c into array d. Then sort array d. Now, compute minimum, maximum, mean, median and standard deviation from array d without using numpy.

Python code without using the library numpy:

import matha = [3.69, 0.81, 7.01, 3.49, -1.64, 6.29, 0.26, 6.07, 6.99]
b = [-1.23, 1.03, 8.94, -1.71, 0.98, -1.16, 2.23, 2.29, -0.68]
c = [9.24, 8.87, 2.72, -4.08, -3.7]
d = a + b + c
d.sort()
print("Minimum: ", d[0])
print("Maximum: ", d[-1])
print("Mean: ", sum(d)/len(d))
print("Median: ", d[len(d)//2])
# Standard Deviation
sum_of_squares = 0
for i in d:
sum_of_squares += (i - sum(d)/len(d))**2
print("Standard Deviation: ", math.sqrt(sum_of_squares/len(d)))

On running the above code, we got the same result:

Output:
Minimum: -4.08
Maximum: 9.24
Mean: 2.465652173913043
Median: 2.23
Standard Deviation: 3.9781346142949703

Conclusion:

We could generate the code in different languages from plain English description, this shows the power of the tool. However, let us also understand its limitation. The code is generated by AI, and it is not perfect. So, it is advisable to verify the output or check the code.

This will be series of articles and in the forthcoming articles, we will discuss the more complex problems and how we are able to generate the code just by providing English description. We presume in the next five years most of the programmers will generate code from English description rather than writing code in programming languages.

Thanks so much for reading my article!!! For further details, please visit the website https://www.fastdevai.com

--

--