iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔖

Playing around with AI Programmer

に公開

Introduction

The comments within the code were not written by AI, but entered by me.
The code itself is pasted exactly as the AI wrote it.

Main Topic

I found an interesting service on Twitter, so here is a report on it.
To those thinking, "Another overseas service? I can't speak English," don't worry!
It's made in Japan!
Japanese is perfectly fine!

https://aiprogrammer.hashlab.jp/

Trying it out right away

JavaScript

// Function to calculate the average of an array

function average(arr) {
  var sum = 0;

  for(var i = 0; i < arr.length; i++) {
    sum += arr[i];
  }

  return sum / arr.length;
}

Hmm, it ended up using var.
Since var is absolute garbage, I'll try adding "ES6".

// Function to calculate the average of an array in ES6 or later

const average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;

It produced a nice result.
When specifying JavaScript, it might be better to include phrases like "ES6 or later."

Python

# Function to calculate the average of an array

def average(arr):
sum = 0
for i in range(0, len(arr)):
sum += arr[i]
return sum / len(arr)

It's long, and the indentation is off.

Ruby

# Function to calculate the average of an array

def average(array)
sum = 0
array.each do |element|
sum += element
end
sum / array.length
end

It's long!

Go

// Function to calculate the average of an array

func average(arr []int) int {
sum := 0
for _, v := range arr {
sum += v
}
return sum / len(arr)
}

The code for Python, Ruby, and Go is the same.
Even though any of these languages could do it in 2 to 3 lines, the code has become redundant.

PHP

// Function to calculate the average of an array

<?php

function get_average($arr) {
return array_sum($arr) / count($arr);
}

?>

The PHP code got shorter.

Giving complex instructions

Natural number check

JavaScript
// Function to check if a number is a natural number

function isNatural(num) {
return num >= 0;
}

It also allows decimal points.

Deviation value

JavaScript
// Function to calculate the deviation value

function getDeviation(arr) {
var mean = getMean(arr);
var deviations = arr.map(function(num) {
return num - mean;
});
return getMean(deviations);
}

A mysterious getMean appeared.

Calculator

JavaScript
// Create a calculator that can parse a string and perform addition, subtraction, multiplication, and division

var calculator = {
add: function(x, y) {
return x + y;
},
subtract: function(x, y) {
return x - y;
},
multiply: function(x, y) {
return x * y;
},
divide: function(x, y) {
return x / y;
}
};

It's useless.

Summary

AI Programmer is a very interesting service.
The biggest attraction is its support for Japanese.
I'm looking forward to future updates.

Extra

I also had GitHub Copilot write some code.

JavaScript
// Function to calculate the average of an array

const average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;
Python
# Function to calculate the average of an array

def average(arr):
    return sum(arr) / len(arr)
Ruby
# Function to calculate the average of an array

def average(array)
    array.sum / array.length
end
Go
// Function to calculate the average of an array

func average(arr []int) int {
    return sum(arr) / len(arr)
}
PHP
// Function to calculate the average of an array

<?php
function average($arr) {
    return array_sum($arr) / count($arr);
}
?>
JavaScript
// Function to calculate the deviation value

const calc = (score) => {
    return (score - 50) / 10;
}
JavaScript
// Function to check if a number is a natural number
const isNaturalNumber = (num) => {
    return Number.isInteger(num) && num > 0;
}
JavaScript
// Create a calculator that can parse a string and perform addition, subtraction, multiplication, and division

function calculator(str) {
    let result = 0;
    let operator = '+';
    let num = 0;
    for (let i = 0; i < str.length; i++) {
        if (str[i] === '+' || str[i] === '-' || str[i] === '*' || str[i] === '/') {
            if (operator === '+') {
                result += num;
            } else if (operator === '-') {
                result -= num;
            } else if (operator === '*') {
                result *= num;
            } else if (operator === '/') {
                result /= num;
            }
            operator = str[i];
            num = 0;
        } else {
            num = num * 10 + parseInt(str[i]);
        }
    }
    if (operator === '+') {
        result += num;
    } else if (operator === '-') {
        result -= num;
    } else if (operator === '*') {
        result *= num;
    } else if (operator === '/') {
        result /= num;
    }
    return result;
}

Copilot's version can't handle multiplication and division. It's close, though.

Extra 2

Apparently, you can write in a different language by using the "not X but Y" syntax.

Java
// Create a function to find the average of an array in Java instead of C++

public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
int average = sum / array.length;
System.out.println(average);
}
}

At the time of writing, Java was not yet supported, but it still managed to write it.
(I wrote the code up to "public class Main" myself)
I was simply amazed.

Extra 3

Apparently, it can explain code in Japanese, so I'll give it a try.
I've commented it out for styling reasons.

TypeScript
const getDecimalLength = (value: number) => {
let x = (value + '').split('.')[1];
if (typeof x !== 'undefined' && x.length > 0) {
return x.length;
}
return 0;
};

// This function gets the number of decimal places from a numeric value.
Assembly
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 12, 0 sdk_version 12, 3
.globl __Z3addii ## -- Begin function _Z3addii
.p2align 4, 0x90
__Z3addii: ## @_Z3addii
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
popq %rbp
retq
.cfi_endproc
## -- End function
.subsections_via_symbols

# This code defines a function that adds two integers.

It's assembly generated by gcc, but it explains it quite well.

GitHubで編集を提案

Discussion