Thursday, October 14, 2010
Looping (count/2*(count%2==2?1:-1)) from middle to start and end.
One of the following would do:
1.
for(int i = 0 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}
2.
for(int i =1 ; i <10;i++){
System.out.println(i/2*(i%2==0?1:-1));
}
of course, first one will print 0 two times. So,
1.1.
System.out.println(0);
for(int i = 1 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}
Let me change these suitable for real situation:
1.1.
doSomethingForIndex(0)
for(int i = 1 ; i < 5;i++){
doSomethingForIndex(i);
doSomethingForIndex(0);
}
2.
for(int i =1 ; i <10;i++){
doSomethingForIndex(i/2*(i%2==0?1:-1));
}
If you look at these you will see no different in execution, because all statements run one after other.
But, say if we want to run doSomethingForIndex(i); in different threads one after other, then sample 1.1 will run two statements in one thread. So in that case it is better to go for option 2. (count/2*(count%2==2?1:-1))
Sunday, April 11, 2010
Counting boxes
I used to count boxes from any surface wherever I saw. Say, If we have a rectangle surface with boxes as shown here.
1. We can count all boxes one by one like this.
2. But, Yes, simply we can count rows and columns and multiply to get the total.
Yes that is 4x6=24, In this case we have to count two times(1-4,1-6)
3. What if we count like one of the following way.
We have to count only one time (1-6) but we have to remember the number once we reach edge.
I see counting diagonally means, counting both rows and columns at once. This different type of counting is possible(or we may use) in different situations in our life. May be sizing a problem etc. I am looking forward to see any real world examples/applications to match this. I just thought to write so you also can think.
Tuesday, September 15, 2009
Part 5: Thoughts of “How we do?” - Process
The question is, if I am waiting to cross, should I look at the other side vehicles as just as vehicle or should I check for my bus as well. Say if I want to catch bus numbered 811, If I sow a bus moving towards the bus stop with the same number, then should I consider that and harry up myself? Is that safer? , faster ? Or correct approach?. Actually once I cross the road and reached the bus stop only I can get the bus. So before getting to that stage, do I need to worry about that? Will it be safer?. Of cause, If I missed that bus then I want to wait for next bus which may come after another 10-20 min or some reasonable time.
For that, a simple advice may be: you shouldn't think about your bus number until you reach the bus stop. That always safer. Because if you think about your bus number then your mind will say to cross the road without looking for other vehicles, that will cause dangerous accidents. You may have to loss your self even.
Normally, the process will be defined for every project with a set of rules and procedures to ensure the safety or quality of the project. But that may slow down the project and we may loose a business because of the delay also.
Say If I have a meeting today at office and I should be on time. While waiting at the start of the crossing, If I sow a bus which I want to catch to go to office and if there is no vehicle then way I shouldn't cross the road? Of cause I shouldn't practice this in main junction or in a traffic area.
On the whole, If we follow all the process regulations in our project and if we missed our dead line we can only say that we have follow this and that process and our project is a better project etc. But we will loose the business at that time. Mostly business needs Good project rather Better project same as real life.
So, It is important to define and follow a process for our project, but at the same time we should identify and change or leave the process when ever its need. Again this is a skill of switch between following and leaving the process, we should be eye open for this always.
Monday, June 8, 2009
Tuesday, January 27, 2009
Part 4: Thoughts of “How we do?” - A Story
A family arranged a trip. They were going by a van. Van was driven by a young driver, next to him a young family member. In the next row, father, mother and their child around 10 years old boy were seated. In the last row grandfather and grandmother were there.
On the way, a traffic signal, it is turned to red. So driver stopped the van. Now the little boy prompt a question “Why you stopped the van?” “The traffic signal is red” driver replied. He asked more “Why we have to stop if signal is red?” for that the young family member replied “If not police man will charge us”
The boy was not satisfied with that answer; He turned his head to back. Then grandfather tried to answer “We have to give way to others before we take”, “There should be a common rule to control”, “you have to wait for your chance”. While grandfather was continuing with his answer, boy had turned his head to his father, because he couldn’t understand those answer. Then father explained, in a junction, if all people try to go, then there will be no coordination. So we have to divide time between us and move one by one. For that there should a person or signal to indicate the time change. And finally if someone violating those rules, there should be police man or someone to check and punish.
After father’s explanation boy was fine. Let stop the story there, in the story, you may have noticed three different answers for one question “Why we have to stop?”
All three answers are right and related to same question. First answer which was from the young man, is talking about immediate input/output of the problem. This is a kind of technician mode of thinking. It is without thinking about others, just thinking how to kindle the present situation according to present observation. Or they may have trained to understand the situation and react according to that.
Second answer, the grandfather’s answer which is more conceptual, no worries about how to design. Scientists used to tell thing like this. More like theories, speaking in an ideal situation.
Final answer, that took care of design aspect, and explained how these are made into a practical working solution. This is an engineer mode of thinking, explained and analyzed the design.
Even though I have put different modes into different people, it doesn’t mean those are owned by only them. All we have those three modes of thinking with us. For each problem in our life, we react or understand things in one or some of the above modes. For every problem, the time to find a solution, the understanding or knowledge we have about the area of the problem and the role we play to solve the problem are decide the way in which we going to handle. In case, if we couldn’t find a solution with one mode, we may have to switch to another different mode as well.
So, it is important of understand these different mode of thinking, choose one or more and switch between them to solve our problems. It is nothing new, we all know this, but just to get to know and organize into you mind.
Monday, January 19, 2009
Bits – Eclipse, Java.
Tuesday, December 2, 2008
Part 3: Thoughts of “How we do?” - In Programming
We have a problem, we want to solve with computer. The computer is dump and we have to write a program, but it will run that for us any number of times without tired. Today lot of programming languages has invented, we are in high level language era. That is, we write programs in a language more like English/Natural language. Then have to convert those programs, mainly those will contains instructions to execute to perform some tasks, into machine code which can be understood by the machine/computer. These programming languages will contain rules or syntax to follow, any body can learn it.
Even though, if we know the syntax, we can't write a program for a given problem. We have to think what are the instructions to be run and in which order. We called this as algorithm. Some how, we have to identify the algorithm and build up them as a full program. Writing a program is not that simple as writing an essay. Our mind has to tell us what should be written.
From a simple problem to a complex problem, we should be able to think and write a program. It may be difficult, but our way of writing program definitely lead our brain think faster and find the solution for us.
In programming languages we have lot of opening and closing, for example in Java, a class body will be opened with “{“and closed with “}”. Improper closing makes compile errors and lead to spend more time to find out them and close currently.
Say we want to write the following class:
class MyClass{
void method(){
}
}
we can write this first line (class MyClass{), then second line (void method(){), after that 3rd line only } and finally } to close the class.
Or
Firstly we can write “class MyClass{}” then move the curser inside the “{}” and press enter key two times and move the curser to the second line and write the “void method(){}” etc.
In the second method, we may have to do more work/press more keys, but in the first method we have to keep lot in our mind, such “have to close the class body”, the logic etc. In the second method, once we closed the class body, our mind is free to think the actual solution, no need to think any think about those unwanted language syntax etc. In programming we have lot of like this, () - for method parameter, “” - strings, [] array etc. So, if we closed all of them immediately we opened and then think only in side, after that we should be able to see good improvement in our performance.
You may wonder, this is a simple thing, and we don't need to even think about this because our favorite smart integrated development environment (IDE) (Eclipse, Visual Studio, etc) would do for us. Of cause, yes! Now a days, all this IDEs are doing this. Even if I happened to evaluate a new IDE, I used to check this first. But, I wanted to check ourselves more in the following two cases.
First one, in case if we wanted write a program without IDE or in a new programming language witch doesn't have a smart IDE so far, then are we going to write in which way from above? If we are not going to follow the second way, then we don’t learn that our IDE teach for me.
Secondly, I could see lot more opening and closing in our programs, but even a smart IDE doesn't do for us. For example say we want to write a method, which suppose return a list of objects according to the logic. So I will start with method.
List doSomthing(){
}
Fine, what is next, I have to create a List and fill that with objects according to the logic, then return. But here I could see another opening and closing, that is, creating a list and returning it.
So, I prefer to add following lines to the method first before start other coding.
List doSomthing(){
List result = new SomeList();
//Think and write the logic here
return result;
}
Now we have to think only about the logic, and that also simplified like as, check what ever you want, but finally add into the result list. So now, we got a direction also for our thinking.
Next, if we want to write some thing into a file, first we have to open and write something according to our logic and then have to close, and then only the content will be written into the actual physical file. This is also another example for this kind of open/close scenario. It is great, if we get used to write the open/close statement first before writing the logic in between that. This is applicable for most of the input/output stream programming.
It is obvious that we should be able to find more places where we can find the same open/close pattern in our programming life. If we could capture those and bring into our practice definitely it will change our way of thinking and programming. And not only this pattern, we have more patterns/practices which to be looked into. I hope those will lead us to not worry much about “What we do” and to will make us focus on “How we do”.