Cats and a Mouse

In the past few weeks I have been reading several articles about how great Visual Studio Code is and how software developers are starting to use as their main IDE. I have been using VS Code on and off but have made up my mind that from now on for Java development I will only use Visual Studio Code. I have installed Eclipse and IntelliJ in my machine, both of which I have used for Java development. I will leave both of them installed for compatibility purpose only. Will let you know my findings towards the end of the year.

Have been working on software that manipulates a picker inside a stacker to move DICOM media from the input bin to a drive, take a photo of the media, read and transfer the contents of the DICOM disc to a storage server, generate a JPEG image of the media making sure the labels are properly displayed (i.e., the image is up side up) and send the properly generated image to the storage server.

I started with a sample program provided by the OEM capable of sending individual commands to the stacker. When I received the stacker it would not initialize. After taking it for repair, I decided to use a software library that I had written in C years ago. I just had to add support for the new stacker. Once that was done it took a morning to develop code to move the DICOM media and operate the drives.

The next step was to take a photo and crop it. For that I used C++ and the C++/WinRT framework for Windows. At this time I am able to take a picture and extract the area holding the disc sitting on the extended drive.

Since the media spins in the drive it made sense to take the photo before the drive is closed. The problem is that operators will miss to align all input discs so the pictures can be taken when discs are place in the extended trays of the drives. I decided to take the picture before the data is read. That way the software has plenty of time to crop, extract and orient the image of the disc.

I am in the process of rotating the image. Hope to be done in the next couple days. After that I will make a short video with my phone and make it available to my boss while I continue to test the implementation of the different applications I wrote for this system. I wanted to take the microservice approach. As usual I like to use the essence of what I know and not limit myself to work on a monolith system that needs to be ported to the cloud.

To write some simple code I looked at the Cats and a Mouse HackerRank challenge. The solution appears to be quite simple but I was more interested in Visual Studio Code. Once I am done with this post, I have found a tutorial in VS Code which I will do to learn whatever is offered by the IDE besides Java.

If interested, the challenge can be found here.

Apparently we have a line. In the line we have two cats and a single mouse. The position of the cats and mouse on the line is represented by integer values. The assumption is that the animals will run on the line. If one cat is closer than the other to the mouse, then that cat would have a mouse burger. If both cats are at the same distance from the mouse, the mouse will escape while the cats argue which one will have lunch.

HackerRank has provided the test scaffolding. They also provided the skeleton for the function to develop.

2
1 2 3
1 3 2
Cat B
Mouse C

In this example we have two independent situations indicated by the 2 in the first line. In the first case the cats are at positions 1 and 2 and the mouse is at position 3. It seems that the second cat (B) should be able to have a meal today.

In the second case, the cats are position in locations 1 and 3 with the mouse in position 2. Since both cats are one unit away from the mouse the mouse should be able to escape.

  // Complete the catAndMouse function below.
  static String catAndMouse(int x, int y, int z) {

    String result = "";

    // ???? ????
    // System.out.println("catAndMouse <<< cat x: " + x + " cat y: " + y + " mouse
    // z: " + z);

    // **** compute the distance from the first cat (A) and the second cat (B) to
    // the mouse ****
    int catAToMouse = Math.abs(x - z);
    int catBToMouse = Math.abs(y - z);

    // ???? ????
    // System.out.println("catAndMouse <<< d1: " + d1 + " d2: " + d2); // **** determine what happens based on the distances **** if (catAToMouse > catBToMouse)
      result = "Cat B";
    else if (catAToMouse < catBToMouse)
      result = "Cat A";
    else
      result = "Mouse C";

    // **** return the proper message ****
    return result;
  }

The catAndMouse function gets three arguments. In my humble opinion they should be labeled catA, catB and mouse or something like that.

A string is initialized to return the outcome.

The next statement which I commented out was used to verify that the function was properly called. Of course when using an IDE we can just single step and check the values of the arguments. I like to put messages because in practice I like software that is able to log messages to files. Some messages are always enabled (e.g., warnings and errors) but some should be enabled just to debug an issue in the field. Of course the messages would not be generated by a System.out.println statement.

Next we will compute the distance from the first cat (A) to the mouse. Note that we get a positive instance in case the mouse is closer to the line origin than the cat. The same holds for the second cat (B).

We now check if cat A is further than cat B. In this case cat B would catch the mouse. If cat A is closer than cat B then cat A will have a meal. Finally if both cats are at the same distance our friendly mouse will live another day.

I was not sure about posting the code for this project, but it can be found in my GitHub repository.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a message using the following address:  john.canessa@gmail.com. All messages will remain private.

Keep on reading and experimenting. It is the best way to learn, refresh your knowledge and increase your development toolset!

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.