Monday, April 28, 2014

The eggs queue

Rationale: I was a computer expert for over 10 years, but I was never been able to find and intelligent job, whereby I could apply what I know about computer science. I have always been ordered about by people much more stupid than me, who wanted me to use the wrong tool for the job, just like knocking a nail with a chainsaw or worse with your bare hands. Lately I become an independent organic farmer and, surprisingly, I can finally apply all I know about computers, although there aren't any in my farm!

One application is a circular queue to differentiate between fresh and old eggs. My chickens now lay about 5-6 eggs per day. I only eat two eggs a day and put the rest aside to make sweets or for possible guests.

I always want to eat eggs on a FIFO basis: First egg In, First egg Out, meaning I always use the oldest one, so to avoid some eggs are eaten too late and go bad. I reused an egg cardboard of an egg carton I took from the supermarket and numbered each of its eggcups, up-bottom and left-to-right.

Starting from 1, I keep adding eggs to the end of the queue and take ones to eat from the start. Initially, the queue head is the lowest number filled by an egg, while its tail is the highest number, but when you reach the end of the queue - 30 in my case - this is not true anymore. E.g. in this picture



the oldest egg is number 20 and egg number 1 comes just after egg number 30! This is why it is called a "circular" queue: although the cardboard shape is squared, you have to imagine its egg cups as arranged in a circle, so that 1 really comes after 30 physically. Queued eggs all make one single circular piece of "snake", which changes its length and goes around the circle clockwise or counterclockwise as eggs are added and taken, depending on your convention.

Now look at this picture:



Here I have 29 eggs and only one eggcup free, which happens to be number 26. Oldest egg is thus number 27 - this is the one I would eat first, while latest laid is 25. You can't add another egg in eggcup 26, since if you do it, you would not be able to tell fresh from old eggs, that is the beginning and end of the queue. If you put some markers, you could fill the queue completely, but for the sake of simplicity, I prefer to lower the queue capacity by 1 rather than having to use head and tail markers. This way queue head and tail is located visually, with no need for markers. Of course, your mind implements an algorithm to do it. If you are a programmer, can you formalize it in a programming language?

BTW If you want to store more than 30 eggs, of course you need another cardboard. You can just have two separate pieces or cardboard and remember which is the first one, or you can glue them together so that you have a 30*2=60-egg circular queue or even more if you have so many eggs to store - and chickens to feed!

The bottom line: chickens are more intelligent than IT managers. Oh well, do not take it personally if you are one of them.

No comments: