Tuesday, August 7, 2012

3 different way Tree Traversal

=========================================================================
Node class for tree
class Node{
    int data;
    Node left;
    Node right;
}
=========================================================================
Case 1: pre-order 

Recursive
public static void preOrderRecursive(Node root){
    if(root == null)
        return;
		
    System.out.print(root.data + " ");		
    preOrderRecursive(root.left);
    preOrderRecursive(root.right);
}
NON-Recursive
public static void preOrderIterative(Node root){
    if(root == null)
        return;
		
    Stack<Node> unvisited = new Stack<Node>();
    unvisited.push(root);
	
    while(!unvisited.isEmpty()){
        Node node = unvisited.pop();
			
        System.out.print(node.data + " ");
        if(node.right != null)
            unvisited.push(node.right);
        if(node.left != null)
            unvisited.push(node.left);
    }	
}
=========================================================================
Case 2: in-order 

Recursive
public static void inOrderRecursive(Node root){
    if(root == null)
        return;	
	
    inOrderRecursive(root.left);
    System.out.print(root.data + " ");
    inOrderRecursive(root.right);
}
NON-Recursive
public static void inOrderIterative(Node root){
    if(root == null)
        return;
		
    Stack<Node> unvisited = new Stack<Node>();
		
    while(!unvisited.isEmpty() || root != null){
        if(root != null){
            unvisited.push(root);
            root = root.left;
        }else{
            root = unvisited.pop();
            System.out.print(root.data + " ");
            root = root.right;
        }
    }
}
========================================================================= 
Case 3: post-order 

Recursive
public static void postOrderRecursive(Node root){
    if(root == null)
        return;
		
    postOrderRecursive(root.left);
    postOrderRecursive(root.right);
    System.out.print(root.data + " ");		
}
NON-Recursive
public static void postOrderIterative(Node root){
    if(root == null)
        return;
		
    Stack<Node> finalStack = new Stack<Node>();
    Stack<Node> tempStack = new Stack<Node>();
	
    tempStack.push(root);
    while(!tempStack.isEmpty()){
        Node node = tempStack.pop();
        finalStack.push(node);
        if(node.left != null)
            tempStack.push(node.left);
				
        if(node.right != null)
            tempStack.push(node.right);
    }
	
    while(!finalStack.isEmpty()){
        Node node = finalStack.pop();
        System.out.print(node.data + " ");	
    }
}

Find a missing integer in an array of number

Find a missing integer in an array of number

Case 1: Sorted
Use two pointers to store previous and current value, and difference between two number should equal to 1.
public static int findMissing_Sorted(int[] input){
    if(input.length == 0)
        return 0;

    int prev = input[0];
    for(int i = 1 ; i < input.length ; i++){
        int curr = input[i];
        if((curr - prev) != 1)
            return (prev + 1);
        else
            prev = curr;
    }
    return 0;
}



Case 2: Unsorted
Get the sum of 1 to N, use formula sum = (N+1)*(N)/2
Minus the element in the array, and the remaining value is the missing number.
public static int findMissing_Unsorted(int[] input){
    if(input.length == 0)
        return 0;
   
    int N = 10;
    int max = (N + 1) * N / 2;
  
    for(int i = 0 ; i < input.length ; i++)
        max -= input[i];
   
    return max;
}

Monday, August 6, 2012

How to use *args and **kwargs in Python

from
http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

http://docs.python.org/tutorial/controlflow.html#keyword-arguments


def test_var_args(farg, *args):
    print "formal arg:",farg
    for arg in args:
        print  "another arg:",arg
  


def test_var_kwargs(farg, **kwargs):
    print "formal arg:",farg
    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

test_var_args(1, "two", 3, "four")
print '-'*40
test_var_kwargs(farg = 1, myarg2 = "two", myarg3 = 3, myarg4 = "four")


def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

args = ("two", 3)
test_var_args_call(1, *args)


def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)

Tuesday, July 31, 2012

Codeforce - 158A - Next Round

n, k = map(int, raw_input().strip().split() )
seq = map(int, raw_input().strip().split() )

score = seq[k - 1]

if score == 0:
    counter = 0
    for i in range(0, k):
        if seq[i] > score:
            counter += 1
    print counter
 
else:
    counter = k
    for i in range(k, n):
        if seq[i] >= score:
            counter += 1
        else:
            break
    print counter

ACM105 -- The Skyline Problem

#include <iostream>

using namespace std;

int main()
{
    int L,H,R;
    int counter = 0;
    int temp_pre,temp_curr;
    int min = 20000,max = 0;
    int building[10001] = {0};


    while(cin>>L>>H>>R)
    {
        if(min>L)
        min = L;

        if(max<R)
        max = R;

        for(int i = L ; i < R ; i++)
        {
            if(building[i] < H)
            building[i] = H;
        }
        counter++;
    }



    bool check = true;
    temp_pre = building[min];
    cout<<min<<" "<<temp_pre<<" ";

    for(int j = min+1 ; j <= max ; j++)
    {
        temp_curr = building[j];
        if(check) //check == true
        {
            if(temp_curr == 0)
            {
                check = false;
                cout<<j<<" "<<temp_curr;
            }else
            {
                if(temp_curr != temp_pre)
                {
                    cout<<j<<" "<<temp_curr<<" ";
                    temp_pre = temp_curr;
                }
            }
        }
        else
        {
            if(temp_curr == 0)
            {
                //do nothing
            }else
            {
                check = true;
                temp_pre = temp_curr;
                cout<<" "<<j<<" "<<temp_curr<<" ";
            }
        }
    }

    cout<<endl;
    return 0;
}

Codeforces -- 1A -- Theater Square

#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char* argv[]){
    long double n,m,a;

    cin>>n>>m>>a;

    long long res = (long long)(ceil(n/a) * ceil(m/a));

    cout<<res;
 return 0;
}

ACM 10035 -- Primary Arithmetic

#include <iostream>

using namespace std;


int main(int argc, char* argv[]){
    unsigned long long n1;
    unsigned long long n2;
    int carry = 0;
    int sum = 0;
    int count = 0;

    while(cin >> n1 >> n2) {
        if(n1 == 0 & n2 == 0)
            break;

        carry = 0;
        count = 0;
        sum = 0;

        while ((n1 > 0) || (n2 > 0)) {

            sum = carry + (n1 % 10) + (n2 % 10);

            if (sum >= 10) {
                count++;
            }

            carry = sum / 10;

            n1 /= 10;
            n2 /= 10;
        }

        if (count == 0) {
            cout << "No carry operation." << endl;
        } else if (count == 1) {
            cout << "1 carry operation." << endl;
        } else {
            cout << count << " carry operations." << endl;
        }
    }
 return 0;
}