Damien Sedgwick

Posted on

# ChatGPT - Complex Coding Challenges: 5 KYU

Good afternoon!

Let's get busy putting ChatGPT through it's AI acrobatics shall we!

The problem we are going to be looking at today is the following:

``````Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)

HH = hours, padded to 2 digits, range: 00 - 99
MM = minutes, padded to 2 digits, range: 00 - 59
SS = seconds, padded to 2 digits, range: 00 - 59
The maximum time never exceeds 359999 (99:59:59)

You can find some examples in the test fixtures.
``````

Now as the fixtures will likely be different for each language we are going to use today, I will not bother sharing them but for the purpose of this exercise, each time I ask ChatGPT to solve the puzzle, I will provide it the fixtures.

``````humanReadable :: Int -> String
let hours = seconds `div` 3600
minutes = (seconds `div` 60) `mod` 60
secs = seconds `mod` 60
where pad n = if n < 10 then "0" ++ show n else show n
``````

I am going to be honest, I wasn't expecting this to pass first time but it did! Solid work there from our pair programming AI.

Solving it in JavaScript

``````function humanReadable(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;

}

return (num < 10 ? '0' : '') + num;
}
``````

Another first time success for JavaScript in this instance, let's move on and see what we can C, C, C

Solving it in C

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

void do_test(unsigned seconds, const char *expected);

void human_readable(unsigned seconds, char* output) {
unsigned hours = seconds / 3600;
unsigned minutes = (seconds / 60) % 60;
unsigned secs = seconds % 60;

snprintf(output, 9, "%02u:%02u:%02u", hours, minutes, secs);
}

void do_test(unsigned seconds, const char *expected) {
char actual[9];
cr_assert_str_eq(actual, expected);
}
``````

Above is the first attempt, I wrote the question exactly the same as I had the previous two times but here we hit our first failure.

This code did not pass so I went back and asked ChatGPT to try again and provided it with the following function stub:

``````char *human_readable_time (unsigned seconds, char *time_string)
{
*time_string = '\0'; // write to time_string
return time_string; // return it
}
``````

A few seconds later I had a new solution to try:

``````#include <stdio.h>

char *human_readable_time(unsigned seconds, char *time_string) {
int hours = seconds / 3600;
int minutes = (seconds / 60) % 60;
int remaining_seconds = seconds % 60;
sprintf(time_string, "%02d:%02d:%02d", hours, minutes, remaining_seconds);
return time_string;
}
``````

This solution passed and all was well!

I appreciate that these may not be the most exciting of articles I am writing at the moment but we are doing the software community a great service in conducting some pretty trivial scientific research!

Catch you all on the next one!