Saturday, September 18, 2010

Pairing algorithm

I cam across a very good question, I posted my anwser in java, If any one interested can post it in any other languages also.

Write a function, Brackets(n), where n is an integer, which will print all valid pairings of parenthesis. For example, Brackets(3) should print
()()()
(())()
()(())
(()())
((()))

Try not to use brute force and generate 2^n combinations.

My solution to the above question in JAVA is

package solved200;

import java.util.Stack;

public class Brackets {

private final int n;

String open = "(";
String close = ")";
String[] tokens = new String[]{open, close};

Stack mainStack;
int count;

public Brackets(int n) {
this.n = 2*n;
mainStack = new Stack
();
}

public void printBrackets(){
printBracketsHelper(this.n, 0, 0);
}

private void printBracketsHelper(int k, int numCloseBrackets, int numOpenBrackets){

if(k == 0){
if( numOpenBrackets == numCloseBrackets) {
count++;
printStack();
}
return;
}
for(int i = 0; i < tokens.length; i++){ boolean addToStack = false; String curToken = tokens[i]; if(mainStack.size() == 0) { if( curToken.equals(close)) { push(curToken); numCloseBrackets++; addToStack = true; } } else { if( curToken.equals(open)){ if( numOpenBrackets + 1 <= numCloseBrackets) { push(curToken); numOpenBrackets++; addToStack = true; } } else if( curToken.equals(close)){ if( numOpenBrackets <= numCloseBrackets + 1) { push(curToken); numCloseBrackets++; addToStack = true; } } } if( addToStack ) { printBracketsHelper(k-1, numCloseBrackets, numOpenBrackets); String popStr = mainStack.pop(); if( popStr.equals(close)) numCloseBrackets--; else if (popStr.equals(open)) numOpenBrackets--; } } } private String push(String curToken) { // System.out.println("PUSH " + curToken); return mainStack.push(curToken); } private void printStack() { System.out.print(count + ": "); for(int i = mainStack.size() - 1; i>=0; i--){
System.out.print(mainStack.get(i));
}
System.out.println();
}

public static void main(String[] args) {
Brackets b = new Brackets(15);
b.printBrackets();
System.out.println("Total : " + b.count);
}

}

Monday, September 13, 2010

Worst Journey's

I know I am diverting from my topic of giving only technology content in this blog but I really need a space to express my anger and need a solution to this.

sep12 hyd -> bang
Small suggestion to all those people who travel by bus frequently for outstations. then read this blog. Yesterday due to heavy festive season rush I got a private bus ticket(OMER travels) before a month. My travel starts like this: I was taken to afzal gunj(charminar area) in a van, pushed me in a small compact room, above all it was raining. I waited there from 9-11:30 for my bus to arrive, few ppl got furious, there was a fight n every person in that room was pushed outside the room. Everyone got drenched, there was no conclusion for the fight at all. I paid Rs 1550/- for the ticket to travel which is a double the cost of the whole journey. There were people who paid more than 2K for the same travel. 2 buses arrived after a long wait which didn't even seem like a volvo. I went and sat in my seat, I saw a guy in my next seat. I just asked him, Sir this is ladies seat. He said this fellow gave me the ticket an hour back.When I went down of the bus to ask him about my seat, I could see he had lot of ppl shouting at him as he sold one seat to 2 ppl. I went and asked him that my next seat is not given to ladies. His answer was so wat? I really want to thank the guy next to me for his co-operation and sensibility he asked few ladies to exchange with his seat. They didn't give blankets, bus was freezing.Topping to all, bus stopped in the middle of a forest. A guy comes shouting sab log utaro, Yeah bus abhi mumbai jayegi. For a while I thought bus got hijacked... :) But the guy said u need to go n sit in the other bus. All of the passengers started a fight with the driver and his answer was , Ur luggage is already kept in other bus, How early you go n sit there v ll start early. There was no way out so we started moving to other bus. I saw few people were coming from other bus and moving to our old bus. I asked one girl where r u going/destination. She said she need to go to Hyderabad. Then it clicked me that these people hired a bus from repective location, exchanged the passengers from the buses and brought back to where they started. I just felt some person feels he is too intelligent, and I want to teach that intelligent jerk who played with 120 people in the whole journey. Please suggest what can I do. I took the ticket from my fellow passenger, thinking i could do sumthin