Wednesday, July 19, 2023

OR & NOR in Chess

Chess game has fascinated me since my childhood, there are lots of interesting facts about it. My favorite piece on the board is Knight. It can hop and do deceiving attacks with its mysterious capabilities. Basically, the square it can land on is not obvious and makes the opponent overlook or miss the attack. I have been taught that Knight moves in L or 7- shape. The steps were not intuitive to me and I was not able to get a convincing explanation for the same until recently. When I found the reason, it only increased my admiration for the inventor of the game.

Before going to Knight, let's look at other pieces on the board.

The Rook: Rooks move in straight lines along ranks (horizontally) and files (vertically). They can move an unlimited number of squares in any of these directions, making them powerful long-range pieces.

The Bishop: Bishops, on the other hand, move diagonally across the chessboard. Like rooks, they can also move an unlimited number of squares along their respective diagonals, granting them extensive influence over the board.

The Queen: - The queen can move to all squares which a rook can move or a bishop can move. Most powerful in the chess world! 

Now, the Knight: The knight can move to a square which neither Bishop NOR Rook can go, or simply a queen cannot go. This makes the player to hard to foresee any fork attacks, which knights are good at. 

Knight is a real-world use case of NOR operation, that's common in the Computer Science field.

Monday, July 17, 2023

Hungry Vacuum cleaner

Yesterday, my niece visited our house. I get excited playing with kids, especially toddlers. I was running the Vacuum cleaner and she was curious looking at it. I let her push the button that triggers the suction. She started collecting objects from the floor and kept them near the head of the vacuum. The cleaner sucked them and they started appearing in the transparent bin, and she was excited to see them piling up there. I said it's a hungry vacuum cleaner and it eats them and puts them in its tummy! She cheered this idea of a hungry vacuum cleaner!

Usually, I don't extend such ideas to stories since I am not good at it! But not so now. I took the help of ChatGPT to translate this into a good story.

I asked ChatGPT to write a story of a hungry vacuum cleaner catered to toddlers. It gave a big one, which only a middle schooler can read. Then I said, it's too verbose, then it refined the same to an elementary schooler :-). I gave the feedback again, but this time I asked more precisely to have the story format similar to the famous Hungry Caterpillar. It gave a good format now but was still using complex words for toddlers. I took it and modified it. Here it is, for your eyes!

Once upon a time, there was a hungry vacuum cleaner, Dustie.

On Monday, Dustie ate a pile of crumbs like rolling drums!

On Tuesday, Dustie gobbled up spilled juice! Papa and Mommy for a truce!

On Wednesday, Dustie sucked up brother's toy with joy!

On Thursday, Dustie zoomed around the room, and finds no doom!

On Friday, Dustie munched a bowl of popcorn with its long horn!

On Saturday, Dustie speeded like a tornado and wipes out all the play dough!

On Sunday, Dustie needs a recharge to meet you next week!

Saturday, June 24, 2023

Thought process blackholing - ChatGPT to rescue

During course of Software development, our thought process will get stuck many times, and we need to take a break or need help from other engineers to get out of it. This state is common, particularly, while troubleshooting a bug or a failure scenario. In this blog, I am taking such an example, and how ChatGPT can help in faster resolution.

This was sometime last year, where there is a software failure occurring on a rare condition. We were not able to reproduce the issue at will nor able to find any issue in the code. The Senior developer, my co-worker, was looking at the code for more than 2 days, and he didn't find any issues in the related APIs. Then we jumped on a call, and I pointed out the code where there is bug, and was able to solve it in 10 minutes. The software bug looks something like the following.

wg := new(sync.WaitGroup)

go func() {

     wg.Add(1)

     fmt.Println("Hello world")

     wg.Done()

}

wg.Wait()

fmt.Println("Execute after func is done")

In the above code, func go-routine can be scheduled at any time and wg.Add may not be run, so the parent code can print "Execute after func is done" before "Hello world". The issue is obvious here, but hard to find it in a blob of code. The fix is simple, move wg.Add(1) before the go func(){} body or after it.

This thought-stuck state happened to me as well, many times in the past. I had to take a break from looking at the code or need some external trigger to clear the state. 

A very-smart static analysis tool can help here, but static analysis is always on the back burner when we have high-priority tasks. 

ChatGPT for rescue. I quizzed ChatGPT to find out what the above code is doing. Here are my interactions with it.

ChatGPT explains what the code is trying to do




With my next level of probe, it finds the bug. I think the probe can be automatic so that ChatGPT can find it while explaining.




With ChatGPT integrated in developers' IDE, we can fix this issue while coding, or alternatively hook up the ChatGPT in the code review process which can do better review and find the bug before merge!

I believe tools like ChatGPT can only make developers' life easier, and make us more productive!