Wednesday, October 28, 2015

LinkedList In java Example

import java.util.LinkedList;

public class MyFirstLinkedList{

public static void main(String args[]){
System.out.println("I am at the starting point Yuppie");
System.out.println("---------------------------------");

LinkedList<String> ll = new LinkedList<String>();
ll.add("Nepal");
ll.add("Canada");

System.out.println("New LinkedList : " + ll);
ll.addFirst("Usa");
ll.add(1, "Iran");
ll.addLast("Japan");

System.out.println("New LinkedList afer adding items : " + ll);

ll.addFirst("Nepal");
ll.set(4, "Pakistan");
ll.addLast("Turkey");
ll.addLast("Turkey");
ll.addLast("Egypt");
ll.addLast("Egypt");
ll.addLast("Qatar");

System.out.println("New LinkedList updated [0] : " + ll);

if(ll.contains("Nepal")){
System.out.print("Yuppie there is Nepal");
}

Object name = ll.get(0);
System.out.println("Taken Object Name is : " + name);
System.out.println("Directly taken name is : " + ll.get(3));

System.out.println("Index of Egypt: " + ll.indexOf("moti"));
System.out.println("LastIndex of Egypt : " + ll.lastIndexOf("moti"));

ll.removeFirstOccurrence("Turkey");
System.out.println("List updated [1] : " + ll);

ll.poll();
System.out.println("List updated [2] : " + ll);

ll.pollFirst();
System.out.println("List updated [3] : " + ll);

ll.removeLastOccurrence("Turkey");
System.out.println("List updated [4]: " + ll);

}

}

Output ::

I am at the starting point Yuppie
---------------------------------
New LinkedList : [Nepal, Canada]
New LinkedList afer adding items : [Usa, Iran, Nepal, Canada, Japan]
New LinkedList updated [0] : [Nepal, Usa, Iran, Nepal, Pakistan, Japan, Turkey, Turkey, Egypt, Egypt, Qatar]
Yuppie there is NepalTaken Object Name is : Nepal
Directly taken name is : Nepal
Index of Egypt: 8
LastIndex of Egypt : 9
List updated [1] : [Nepal, Usa, Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [2] : [Usa, Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [3] : [Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [4]: [Iran, Nepal, Pakistan, Japan, Egypt, Egypt, Qatar]


Tuesday, May 26, 2015

Stanton measure

1. The Stanton measure of an array is computed as follows. Count the number of 1s in the array. Let this count be n. The Stanton measure is the number of times that n appears in the array. For example, the Stanton measure of {1, 4, 3, 2, 1, 2, 3, 2} is 3 because 1 occurs 2 times in the array and 2 occurs 3 times.

Write a function named stantonMeasure that returns the Stanton measure of its array argument.

If you are  programming in Java or C#, the function prototype is
int stantonMeasure(int[ ] a)

If you are programming in C++ or C, the function prototype is
int stantonMeasure(int a[ ], int len) where len is the number of elements in the array.

Examples

if a is
return
reason
{1}
1
1 occurs 1 time, 1 occurs 1 time
{0}
1
1 occurs 0 times, 0 occurs 1 time
{3, 1, 1, 4}
0
1 occurs 2 times, 2 occurs 0 times
{1, 3, 1, 1, 3, 3, 2, 3, 3, 3, 4}
6
1 occurs 3 times, 3 occurs 6 times
{}
0
1 occurs 0 times, 0 occurs 0 times
Solution:

public static int stantonMeasure(int[] a){
int one_counter = 0, stat_counter = 0;

for(int i=0; i<a.length;i++){
if(a[i] == 1) one_counter++;
}

for(int j=0; j<a.length; j++){
if(a[j] == one_counter) stat_counter++;
}

return stat_counter;

}
3. Consider the following algorithm
Start with a positive number n
if n is even then divide by 2
if n is odd then multiply by 3 and add 1
continue this until n becomes 1


The Guthrie sequence of a positive number n is defined to be the numbers generated by the above algorithm.


For example, the Guthrie sequence of the number 7 is
7,  22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1


It is easy to see that this sequence was generated from the number 7 by the above algorithm. Since 7 is odd multiply by 3 and add 1 to get 22 which is the second number of the sequence. Since 22 is even, divide by 2 to get 11 which is the third number of the sequence. 11 is odd so multiply by 3 and add 1 to get 34 which is the fourth number of the sequence and so on.


Note: the first number of a Guthrie sequence is always the number that generated the sequence and the last number is always 1.


Write a function named isGuthrieSequence which returns 1 if the elements of the array form a Guthrie sequence. Otherwise, it returns 0.


If you are programming in Java or C#, the function signature is
int isGuthrieSequence(int[ ] a)


If you are programming in C++ or C, the function signature is
int isGuthrieSequence(int a[ ], int len) when len is the number of elements in the array.

Solution::

public static int isGuthrieSequence (int[] a){
        int lgh = a.length;
        System.out.println("Array Length : "+ lgh);
        boolean flag = true;
        System.out.println("last : "+ a[lgh -1]);

        if(a[lgh-1] != 1){
         return 0;
         }else{
            for(int i=0; i<lgh-1; i++){
                int first = a[i];
                int laterr = a[i+1];


                if(first % 2 == 0){  //if the number is even
                    int firstTemp = a[i]/2;
                    if(firstTemp == laterr){
                        flag = true;
                    }else{
                        flag = false;
                        break;
                    }
                }else{ //if number is odd
                    int firstTemp = (a[i] * 3)+1;
                    if(firstTemp == laterr){
                        flag = true;
                    }else{
                        flag = false;
                        break;
                    }
                }
            }
        }

        if(flag) return 1;
        else return 0;
}

Monday, May 25, 2015

Porcupine Number (MUM Sample Questions)

A Prime number is an integer that is divisible only by 1 and itself. A porcupine number is a prime number whose last digit is 9 and the next prime number that follows it also ends with the digit 9. For example 139 is a porcupine number because:

a. it is prime
b. it ends in a 9
c. The next prime number after it is 149 which also ends in 9. Note that 140, 141, 142, 143, 144, 145, 146, 147 and 148 are not prime so 149 is the next prime number after 139.

Write a method named findPorcupineNumber which takes an integer argument n and returns the first porcupine number that is greater than n. So findPorcupineNumber(0) would return 139 (because 139 happens to be the first porcupine number) and so would findPorcupineNumber(138). But findPorcupineNumber(139) would return 409 which is the second porcupine number.

Solution::

public static int findPorcupineNumber(int n){
        int sycoNum = n;
        boolean yuppie = true;

        while(yuppie){
            sycoNum++; 
            
            if(isPrime(sycoNum)){
                int temp1 = sycoNum; 
                boolean nxtPrimeFound = true;
                
                while(nxtPrimeFound){
                    temp1++;
                    if(isPrime(temp1)) nxtPrimeFound = false;
                }

                if((sycoNum % 10) == 9 && (temp1 % 10)== 9){
                    System.out.println("Porcupine Number = " + sycoNum);
                    yuppie = false;
                }
            }
        }
     }
}

-by Yubraj Pokharel :)