Created with Raphaël 2.0.0

Wednesday, March 26, 2025

AI Coding Challenge Major Fail - Why AI coding still has a long way to go

I asked Microsoft's AI Chat 'Copilot' to generate C# code to get the innermost string between strings. This is generalized version of getting a string between braces. Copilot generated a very plausible result, and even gave an example input and output. But the output, was very wrong. It was very misleading, given the very convincing Explanation below.

Conveniently, we can test this code for accuracy. 

The output describe is
 "Innermost", which is the ideal output. 

The actual output if you run the code is "Innermost]Middle".

TL;DR

When you go for a credit check, or cancer treatment, or battle policy cost increase, can you so easily argue the 'computer is wrong'? 

Live AI Code


AI Code generated

using System;

public class Program
{
    public static void Main()
    {
        string input = "Outer[Middle[Innermost]Middle]Outer";
        string start = "[";
        string end = "]";

        string result = GetInnermostString(input, start, end);
        Console.WriteLine("Innermost string: " + result);
    }

    public static string GetInnermostString(string input, string start, string end)
    {
        int endIndex = input.LastIndexOf(end);
        if (endIndex == -1) return ""; // End marker not found

        int startIndex = input.LastIndexOf(start, endIndex - 1);
        if (startIndex == -1) return ""; // Start marker not found

        startIndex += start.Length;

        return input.Substring(startIndex, endIndex - startIndex);
    }
}


Corrected AI Code

Monday, March 17, 2025

C# Round Datetime Extension To Nearest Minute, Round Up, Round Down



C# D
ateTime extensions that rounds a datetime Up, Down or Nearest minutes and smaller units are rounded to zero.

As used in my Solo SCRUM Sprinter App - book work effort in Outlook calendar directly every 30mins.




AI CoPilot Improved Code or is it? Added March 17, 2025

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
     
public static class Program
{
 /// <summary>
 /// Rounds datetime up, down or to nearest minutes and all smaller units to zero
 /// </summary>
 /// <param name="dt">ignore, implicitly passed</param>
 /// <param name="rndmin">mins to round to</param>
 /// <param name="directn">Up,Down,Nearest</param>
 /// <returns>rounded datetime with all smaller units than mins rounded off</returns>
 public static DateTime RoundToNearestMinuteProper(this DateTime dt, int rndmin, RoundingDirection directn)
 {
   if (rndmin == 0) //can be > 60 mins
   return dt;
  
   TimeSpan d = TimeSpan.FromMinutes(rndmin); //this can be passed as a parameter, or use any timespan unit FromDays, FromHours, etc.  
  
   long delta = 0;
   Int64 modTicks = dt.Ticks % d.Ticks;
  
   switch (directn)
   {
    case RoundingDirection.Up:
       delta = (modTicks != 0) ? d.Ticks - modTicks : 0;
    break;
    case RoundingDirection.Down:
       delta = -modTicks; 
    break;
    case RoundingDirection.Nearest:
    {
       bool roundUp = modTicks > (d.Ticks / 2);
       var offset = roundUp ? d.Ticks : 0;
       delta = offset - modTicks; 
       break;
    }
   
   }
   return new DateTime(dt.Ticks + delta, dt.Kind); 
 }
 
 public enum RoundingDirection
 {
  Up,
  Down,
  Nearest
 }
 
 public static void Main()
 {
  //var dt = new DateTime(2001, 1, 1, 0, 0, 0, 000);
  //var dt = new DateTime(2018, 08, 03, 10, 7, 30, 000); //8/3/2018 10:00:00 AM Nearest, re 15/2 = 7.5 mins or 7 mins and 30 secs
  var dt = new DateTime(2018, 08, 03, 10, 7, 31, 000); //8/3/2018 10:15:00 AM Nearest
  
  Console.WriteLine(dt.RoundToNearestMinuteProper(15, RoundingDirection.Up)); 
  Console.WriteLine(dt.RoundToNearestMinuteProper(15, RoundingDirection.Down)); 
  Console.WriteLine(dt.RoundToNearestMinuteProper(15, RoundingDirection.Nearest)); 
 }
}

Optimizations: 1/2 of a number use right-shift

int half = number >> 1; // Right-shift by 1 to divide by 2

Wednesday, March 12, 2025

Google Search Console takes design cue from my metadata consulting blog

Nice to see new Google Search Console layout and design took a bit out of my design for this blog. 😉



Buy Me A Coffee