Friday, June 28, 2013

Solution to Store Credit

This is a probable solution to CODE JAM's Store Credit.


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class StoreCredit {
   
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(ReverseWords.class.getClassLoader().getResourceAsStream("A-large-practice.in"));
            int problemCount = Integer.parseInt(scanner.nextLine());
            int caseCounter = 1;
            for(int iProblemCounter = 0; iProblemCounter < problemCount; iProblemCounter++) {
                Set completedIndexes = new HashSet();
               
                int creditAmount = Integer.parseInt(scanner.nextLine());
                int numberOfItems = Integer.parseInt(scanner.nextLine());
                String itemsLine = scanner.nextLine();
                String[] intArray = itemsLine.split(" ");
                String output = "";
                itemLoop:{
                    for (int iArrayCounter = 0; iArrayCounter < numberOfItems; iArrayCounter++) {
                        completedIndexes.add(iArrayCounter);
                        for (int iArrayInnerCounter = 0; iArrayInnerCounter < numberOfItems; iArrayInnerCounter++) {
                            if(completedIndexes.contains(iArrayInnerCounter)) {
                                continue;
                            }
                            int sum = (Integer.parseInt(intArray[iArrayCounter]) + Integer.parseInt(intArray[iArrayInnerCounter]));
                            output = (iArrayCounter + 1) + " " + (iArrayInnerCounter + 1);
                            if(sum == creditAmount) {
                                break itemLoop;
                            }
                        }
                    }
                }
                System.out.println("Case #" + (caseCounter++) + ": " + output);
            }
        } catch (Throwable a_th) {
            a_th.printStackTrace();
        }
    }
}
See you later friends.

Thursday, March 14, 2013

Windows Service Monitor and starting

Hi friends,

It's been long since my last post. Again I am here with some solution. Specifically for windows. The problem statement was, few windows service was getting stopped all of a sudden due to some error. What we needed is one small utility which monitors the specific service and start that if that is stopped.

There was many suggestion and solution, many monitoring softwares. But I liked the solution mentioned below. Took quite some time to collect all the information and build it.

First copy paste the below code in a file named as "serviceMonitor.vbs" or you can choose any other name as well.


Set sh = CreateObject("Shell.Application")
set shellLogger = CreateObject("WScript.Shell")

If sh.IsServiceRunning("SERVICE_TO_MONITOR") Then
    shellLogger.LogEvent 4, "
SERVICE_TO_MONITOR is running"
Else
    shellLogger.LogEvent 4, "
SERVICE_TO_MONITOR is not running"
    if sh.ServiceStart("
SERVICE_TO_MONITOR", true) Then
        shellLogger.LogEvent 4, "
SERVICE_TO_MONITOR automatically started"
    Else
        shellLogger.LogEvent 4, "
SERVICE_TO_MONITOR could not be started"
    End If
End If   

Replace the word SERVICE_TO_MONITOR with name of your service.
Then open scheduler and create a task. Choose the trigger you want, I kept it at every 1 min.

wolla...  your windows service will never be down. If it is down, It will be up within next one min. :)