OK, after seeing an example where someone tried to ask the OpenAI “ChatGPT” to generate the Code of the first challenge in this year’s Advent of Code (AoC), I thought I’d try the same, but asking for Xojo code.
This was my query to the ChatGPT bot:
i have a text file with one column of integers, and each group of integers is separated from the next one by a blank line. i want to load the file, and sum the values of each group of integers. then i want to find the group with the largest sum of values and print it. can you write that in Xojo?
And the resulting code was:
dim file as FolderItem = GetOpenFolderItem("Select a text file")
if file <> nil then
dim reader as TextInputStream = TextInputStream.Open(file)
dim currentGroup as new List of Integer
dim groups as new List of List of Integer
dim currentLine as String
while Not reader.EOF
currentLine = reader.ReadLine
if currentLine = "" then
groups.Add(currentGroup)
currentGroup = new List of Integer
else
currentGroup.Add(currentLine.ToInteger)
end if
end while
reader.Close
dim maxSum as Integer
dim maxGroup as Integer
dim i as Integer
for i = 0 to groups.Count - 1
dim sum as Integer
dim j as Integer
for j = 0 to groups(i).Count - 1
sum = sum + groups(i)(j)
next
if sum > maxSum then
maxSum = sum
maxGroup = i
end if
next
MsgBox("The group with the largest sum is group " + maxGroup.ToString + " with a sum of " + maxSum.ToString)
end if
There are many things wrong here:
- “new list of” is not supported by Xojo (but something like that is understood by VB, it seems)
- It does not inline the declaration of loop variables (but I could teach him to correct that, though).
- it uses “end while” instead of “wend”.
And more.
So I tried again (click “Reset Thread” at the top left) and asked to make it for REALbasic instead:
i have a text file with one column of integers, and each group of integers is separated from the next one by a blank line. i want to load the file, and sum the values of each group of integers. then i want to determine the group with the largest sum of values. The result shall be the sum of that group. can you write that in realbasic?
This produced a better result:
Still got the array decl wrong, so I told it:
In realbasic, there is no “new list of” term. instead, replace that with “()”. Also, realbasic does not have an “Add” method for appending to arrays. Use “Append” instead.
in realbasic, to close a “while” loop, you must use “wend” instead of “end while”.
Result:
' Open the file for reading
dim file as FolderItem = GetOpenFolderItem("Text Files (*.txt)|*.txt")
if file <> nil then
dim reader as TextInputStream = TextInputStream.Open(file)
' Create a variable to store the largest sum
dim largestSum as Integer = 0
' Create a variable to store the current sum
dim currentSum as Integer = 0
' Read each line of the file
dim line as String
while not reader.EOF
line = reader.ReadLine
' If the line is blank, then we're at the end of a group
' so we need to check if the current sum is the largest sum
' so far, and reset the current sum to 0.
if line = "" then
if currentSum > largestSum then
largestSum = currentSum
end if
currentSum = 0
else
' Otherwise, add the value of the line to the current sum
currentSum = currentSum + line.ToInteger
end if
wend
' If the file doesn't end with a blank line, then we need
' to check if the final group has the largest sum
if currentSum > largestSum then
largestSum = currentSum
end if
' Close the file
reader.Close
' Print the largest sum
MsgBox "The largest sum is: " + largestSum.ToString
end if
Also it appears to have learned not to use “end while” any more (I had asked it to remember this for future queries by anyone).
I still had to teach him about the unavailable “ToInteger” function:
realbasic has no “ToInteger” member function for string values. Use the “Val” function instead, but keep in mind that it returns a 64 bit double value, which could lose precision if the integer value in the string has more than 48 significant digits. If it’s likely that the values are larger than 2^47 bits, use a helper variable of type “variant” in between, by assigning the string to a variable of type variant, then getting its integer value with the “IntegerValue” member function. Please remember this for future queries by anyone.
And it produced (only showing the relevant change):
dim value as Variant
value = line
currentSum = currentSum + value.IntegerValue
Still had to fix “ToString”:
realbasic does not know a “ToString” function for the integer type. Instead of “intvalue.ToString” use: "Str(intValue, “-#”). Please remember this for future queries
And now I got a working program:
' Open the file for reading
dim file as FolderItem = GetOpenFolderItem("Text Files (*.txt)|*.txt")
if file <> nil then
dim reader as TextInputStream = TextInputStream.Open(file)
' Create a variable to store the largest sum
dim largestSum as Integer = 0
' Create a variable to store the current sum
dim currentSum as Integer = 0
' Read each line of the file
dim line as String
while not reader.EOF
line = reader.ReadLine
' If the line is blank, then we're at the end of a group
' so we need to check if the current sum is the largest sum
' so far, and reset the current sum to 0.
if line = "" then
if currentSum > largestSum then
largestSum = currentSum
end if
currentSum = 0
else
' Otherwise, convert the line to an integer using a
' helper variable of type Variant, and add it to the
' current sum.
dim value as Variant
value = line
currentSum = currentSum + value.IntegerValue
end if
wend
' If the file doesn't end with a blank line, then we need
' to check if the final group has the largest sum
if currentSum > largestSum then
largestSum = currentSum
end if
' Close the file
reader.Close
' Print the largest sum
MsgBox "The largest sum is: " + Str(largestSum, "-#")
end if
That was fun!
Later
Sadly, my teachings only stick during one browser session. When I used a different browser and asked again for the realbasic code, it generated some very different code was closer to original basic than to REALbasic or Xojo, unfortunately.