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

Thursday, April 30, 2009

DATA-BASic

A DATABASE MODEL or DATABASE SCHEMA is the structure or format of a database(It is a structured collection of records or data that is stored on a server), described in a formal language supported by the DATABASE MANAGEMENT SYSTEM(Software to manage databases). Schemas are generally stored in a data dictionary(centralized repository of information about data such as meaning, relationships to other data, origin, usage, and format).A data model is not just a way of structuring data: it also defines a set of operations that can be performed on the data.

MODELS



FILE MODEL
The flat (or table) model consists of a single, two-dimensional array of data elements, where all members of a given column are assumed to be similar values, and all members of a row are assumed to be related to one another.

HIERARCHY MODEL

Data is organized into a tree-like structure, implying a single upward link in each record to describe the nesting, and a sort field to keep the records in a particular order in each same-level list. Hierarchical structures were widely used in the structure of XML documents. This structure allows one 1:N relationship between two types of data. This structure is very efficient to describe many relationships in the real world; recipes, table of contents.One limitation of the hierarchical model is its inability to efficiently represent redundancy in data.Parent–child relationship: Child may only have one parent but a parent can have multiple children. Parents and children are tied together by links called "pointers“.

NETWORK MODEL

It organizes data using two fundamental constructs, called records and sets.Records contain fields and Sets define one-to-many relationships between records.A record may be an owner in any number of sets, and a member in any number of sets.Parent–child relationship: Child can have more than one parent and a parent can also have multiple children.

Wednesday, March 25, 2009

GOOGLE I/O DEVELOPER SUMMIT

I am big freak to attend summit. Recently I attended ADOBE RIA SUMMIT at Bangalore and Before that FOSS.in all these places are great as people share there vast knowledge in small amount of time. This places actually tell you where to start and where the future is up to.I am waiting sum one would sponsor me




Above summit is god of all. I wish it happens in bangalore. This is the best summit i heared foe web site designing.Please leave the posts about how the conference was if anyone has attended.

Tuesday, August 5, 2008

MYSQL B!!! (Bug / Behaviour)


Well i got an error in mysql for which i banged my head for full 2 days and figured out it was a small change(I knw every bugs ends the ame way.. :)).Well , i didnt find any forum that explains about the bug and its solution.

Query: Select * from where tot = tot1
BUG: Unknown column "tot1" in where clause.

SOLUTION: Select * from where tot = 'tot1'

If the value of the tot1 is TOT it works fine if it is any thing other than that it gives an error. I could never figure why it acts so weird, Well if any1 knows the solution lets me know and simple image has been attached for the results.