Search This Blog

Tuesday, July 30, 2013

SRM 585 DIV 2 250 Point Problem Solution

We need to calculate the LISNumber of a sequence which the smallest of the number of strictly increasing sequences needed.
So we need to compare number with its neighbor, if the current is less than neighbor we need to continue. 
If the current number is greater than or equal to the neighbor it is a separate sequence increment the count.
Solution to SRM 585 DIV2 250 Point Problem.

public int calculate(int[] seq)
       {
              int res=0;
             
              for(int i=0;i<seq.length-1;i++)
              {
                     if(seq[i]<seq[i+1])
                     {
                           continue;
                     }
                     else if(seq[i]==seq[i+1])
                     {
                           res++;
                     }
                     else
                     {
                           res++;
                     }            
              }     
              return (res+1);
       }


Snakes and Ladders Game in Java


Snakes and Ladders game is an interesting and a fun game to play with friends. I have built a basic snakes and ladders game using the SWT (Standard Widget Toolkit) when I learned it long back, using just the SWT labels and MS Paint to create a board game.
You can download the source code by clicking the link Download Source Code


Instructions:
The following are the instructions to set up the source code in Eclipse IDE.
i)                    Import the project into Eclipse’s Workspace by clicking on File -> Import
ii)                   Select the root directory and check on the “Copy Projects into workspace” and click Finish.
iii)                 Once the project is loaded , it shows that it loaded with Errors
iv)                 Right Click on the project and click on Properties and select Build Path. The libraries are missing that is the cause for the error.  We  need to provide the correct path for the libraries

v)                  All the jar files will be present in the Eclipse folder, we just need to find it and give the name For e.g. we will take the first one “org.eclipse.osgi_3.8.2.v20130124-134944.jar
vi)                 Click on the Add External jar button

vii)               Navigate to the file “org.eclipse.osgi_3.8.2.v20130124-134944.jar” and add it, also remove the entry that previously showed error.
a.       Add the correct external jar
b.      Remove the jar that already showed error
viii)              Repeat the steps 4 to 7 for the remaining 11 build path errors.
ix)                 Once the build paths are set correct, we can run the Java Application. Right Click on the Dice.java  and select run as “Java Application”

x)                  Now we can select players to Play, it supports maximum of 3 players. Click on include to include the players and give a name
xi)                 Click on Roll, to roll your dice. Each ones turn will be displayed in the Roll box. Have Fun :)

You can download the source code by clicking the link DownloadSource Code

Saturday, July 27, 2013

Network Speedometer

Network Speedometer
Network Speedometer is a small application which can run in your notification area and shows/ display’s your Network’s current upload speed and download speed.



You can also view the Up load speed and Download speed on placing your mouse over the application.

Requirements:
.NET Framework 4 Client profile

Instructions:
1)      Download the RAR file and extract it.
2)      Click on the Setup.exe  [This will download and install the .NET Framework 4 Client profile]
 Please be patient as this may take some time
3)      Once completed if asked for restart, restart your computer
4)      Click on NetWorkSpeedOMeter.application file
5)      If prompted Click on Install, enjoy

You can download the application by clicking on the link below:

Sunday, July 21, 2013

Coding Bat Java String 1 solutions 1 to 18



This post contains the solutions to Java String-1 section of codingbat.com. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners. Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.
1.       Problem Name : helloName
Solution:
Hint: The code
Return "xyz" + name;

public String helloName(String name)
{
    return("Hello "+name+"!");
}


2.       Problem Name : makeAbba 
Solution:
Hint: e.g. "Hi" and "Bye" returns "HiByeByeHi".  Return (a+b+b+a)
      
public String makeAbba(String a, String b)
{
     return (a+b+b+a);
}     


3.       Problem Name : makeTags 
Solution:
Hint: Given tag and word strings, create the HTML string with tags around the word, e.g. "<i>Yay</i>".  ‘i’ should be replaced by whatever is given in tag string and Yay should be replaced by whatever is given in word string.

public String makeTags(String tag, String word)
{
       String starttag="<"+tag+">";
       String endtag="</"+tag+">";

       return(starttag+word+endtag);
}

  
4.       Problem Name : makeOutWord 
Solution:
Hint:  Need to enclose the words in the given parenthesis and return the string.  The out word contains e.g. “<<>>” the first two characters represents our start tag and last two characters of out represents end tag. Return Start

public String makeOutWord(String out, String word)
{
       int len=out.length();// length of out string

       String starttag=out.substring(0,2); // to take << from out string
       String endtag=out.substring(len-2,len); // to take >> from out string
      
       return(starttag+word+endtag);
}


5.       Problem Name : extraEnd 
Solution:
Hint:   return a new string made of 3 copies of the last 2 chars of the original string.

public String extraEnd(String str)
{
       String end = str.substring(str.length()-2);
               
       return end + end + end;
}


6.       Problem Name : firstTwo 
Solution:
Hint:  return the string made of its first two chars, so the String "Hello" yields "He".
Constraint: If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string ""


public String firstTwo(String str)
{
        if (str.length() >= 2)
        {
            return str.substring(0, 2);
        }
        else
        {
            return str;
        }
}


7.       Problem Name : firstHalf
Solution:
Hint: Given a string of even length, return the first half. So the string "WooHoo" yields "Woo"

public String firstHalf(String str)
{
       int len=str.length();
               
       if(len%2==0)
       {
             int half=len/2;
             return(str.substring(0,half));
       }
       else
       {
              return " ";
       }
}

8.       Problem Name : withoutEnd 
Solution:
Hint:   Given a string, return a version without the first and last char, so "Hello" yields "ell".
Constraint:  The string length will be at least 2. 

public String withoutEnd(String str)
{
       return (str.substring(1,str.length()-1));
}


9.       Problem Name : comboString 
Solution:
Hint:   Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside.
Constraint:  The strings will not be the same length, but they may be empty (length 0)


public String comboString(String a, String b)
{
       if(a.length()>b.length())
               return(b+a+b);
       else
               return(a+b+a);
}


10.   Problem Name : nonStart 
Solution:
Hint:   Given 2 strings, return their concatenation, except omit the first char of each
Constraint: The strings will be at least length 1. 

public String nonStart(String a, String b)
{
       return(a.substring(1,a.length())+b.substring(1,b.length()));
}


11.   Problem Name : left2 
Solution:
Hint: Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end
Constraint: The string length will be at least 2. 

public String left2(String str)
{
       return str.substring(2) + str.substring(0, 2);
              //substring(2) extracts the string from index 2 through the end. So we put that first, followed by substring(0, 2)
}

12.   Problem Name : right2
Solution:
Hint:  Given a string, return a "rotated right 2" version where the last 2 chars are moved to the start.
Constraint: The string length will be at least 2.

public String right2(String str)
{
       int len=str.length();
       if(len>2)
       {
               return(str.substring(len-2,len)+str.substring(0,len-2));
       }
       else
       {
               return str;
       }
}


13.   Problem Name : theEnd
Solution:
Hint:  Given a string, return a string length 1 from its front, unless front is false, in which case return a string length 1 from its back.
Constraint: The string will be non-empty. 

public String theEnd(String str, boolean front)
{
       if(front)
              return(str.substring(0,1));
       else
              return (str.substring(str.length()-1,str.length()));
}


14.   Problem Name : withouEnd2 
Solution:
Hint: Given a string, return a version without both the first and last char of the string. 
Constraint: The string may be any length, including 0. 

public String withouEnd2(String str)
{
       if(str.length()>2)
       {
              return(str.substring(1,str.length()-1));
       }
       else
              return "";
}


15.   Problem Name : middleTwo 
Solution:
Hint: Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri".
Constraint: The string length will be at least 2.

public String middleTwo(String str)
{
       if(str.length()>2)
       {
               int middle=str.length()/2;
               return(str.substring(middle-1,middle+1));
       }
       else
       {
               return str;
       }              
return result;
}


16.   Problem Name : endsLy 
Solution:
Hint: Given a string, return true if it ends in "ly".

public boolean endsLy(String str)
{
       int len=str.length();
       if(len>=2)     
       {
              if(str.substring(len-2,len).equals("ly"))
                return true;
              else
                return false;
       }
       else
       {
                return false;
       }
}


17.   Problem Name : nTwice 
Solution:
Hint: return a string made of the first and last n chars from the string.

public String nTwice(String str, int n)
       {
                return (str.substring(0,n)+str.substring(str.length()-n,str.length()));
       }

18.   Problem Name : twoChar
Solution:
Hint: return a string length 2 starting at the given index. If the index is too big or too small to define a string length 2, use the first 2 chars.
Constraint: The string length will be at least 2.


public String twoChar(String str, int index)
       {
                int len=str.length();
                if(index>=0 && index+2<=len)
                return(str.substring(index,index+2));
                else
                return (str.substring(0,2));
       }

Please check the my posts for solutions to the remaining problems of Java String 1 section.

Labels