I see, learn and rediscover… everyday!
Posts tagged coding
Forgot the match again?
Jul 10th
Do you play Topcoder? If yes, then there might be something for you in this post. If you don’t know what topcoder is, check http://topcoder.com/tc and then come back here.
If you play topcoder, then keeping track of when the next match is a big headache. There are many many people who login to the arena one day before the contest and ask why there is not many playing today. Come on, it is not funny to get up at 5.30 in the morning to know that the match is not today and it is tomorrow. The rest think the match is one day later, who curse themselves, bang their head for missing a match. If you want to forget forgetting :P about topcoder matches, follow the simple steps given below.
All you need is a Google Account.
- Login to google calendars at http://calendar.google.com/
- Search for “Topcoder” in public calendars. ( By clicking “Search Public Calendars” button). I got some 50 results when I wrote this. This may increase or decrease later. Choose anything from the list (anything from the first 5 will be good enough).
- Click on the “Add to Calendar” button.
- Go back to your calendar page. You can see the Topcoder calendar in the “Other calendars”.
- You need to activate mobile setup by activating your phone number. This takes less than a minute. Give your country and phone number. Google will send you a verification code in 1 or 2 minutes. Type that validation code there and voila, you are done with the mobile setup. :-)
- Now click on the “Calendars” tab in the settings page.
- Click the “Notification” of your new Topcoder calendar.
- Click the “Add a reminder” to add new remainders.
- You can have upto 5 remainders for this calendar.
Google Maps
Jan 15th
I have been using Google Maps for a long time (a nice way to kill time) and now I am using the Google Map API to create my own site (not a nice way to kill time). Was able to start with my work using the sample examples given in the Google Maps site and by trial and error method.
But came to a dead end when i had to manage many events at the same time. Say for the example, when the user clicks in the map, first an infowindow opens up with a form in it. When the user clicks elsewhere when already an infowindow is open, then an prompt asking for whether he should save the old place or not. If he says no, the show the new infowindow. Else retain the old infowindow. Save the details using an Ajax request so that the page is not reloaded again and again. Mummy….
But now, after exploring so much about Google Maps, I was able to manage things now. A simple tutorial about how to handle events in Google Maps. The reason why i choose events is that this is more difficult to grasp AFAIK. :)
First let us start with a simple Google Map as shown in the following url
http://sp2hari.com/gmap/event-simple.html
The main code which adds the event listener is
GEvent.addListener(map,"click", function(overlay,latlng) {
alert ("Map Clicked");
});
GEvent.addListener(map,"click", displayMessage);
Note that for the same event “click”, I have added two event listeners. In the first listener, the code to be executed when the event occurs is inline. This is fine if the code is very small. Even then i advice you to follow the second method where the function name to be called is given as argument. Then you can mention all the actions to be done when the event occurs in that function. For example, my displayMessage will look like
function displayMessage(overlay, latlng) {
document.getElementById("message").innerHTML = "Clicked at latitude "+latlng.lat()+ " and longitude "+latlng.lng();
}
Yup. That’s all it needs to add a simple event listener for Google Maps.
Now let us try something more ;)
How about trying to add a marker at the place where it is clicked and a button to clear all markers?
Check this url
http://sp2hari.com/gmap/event-addmarker.html
In the above file, I just have one event listener for click (you can have as many as you want), and the function I’m calling after click is “addMarker”. The following code adds the event listener.
GEvent.addListener(map,"click", addMarker);
Now the function addMarker has the code to add a marker as shown below
function addMarker(overlay, latlng) {
var marker = new GMarker(latlng);
map.addOverlay(marker);
document.getElementById("message").innerHTML = "Marker added at latitude "+latlng.lat() + " longitude "+latlng.lng() ;
}
Another function clearAllMarkers is used to clear all the markers used in the map. Check the code for that function.
function clearAllMarkers() {
map.clearOverlays();
document.getElementById("message").innerHTML = "All markers cleared";
}
Let’s play more with markers :)
How about having a infowindow open when you click a marker?
Check the following url
http://sp2hari.com/gmap/event-showinfowindow.html
In the above example, we can add markers and we also get an infowindow when we click on the marker. For this the code is very similar to the previous example with a small modification in the addMarker function.
function addMarker(overlay, latlng) {
marker[markerCount] = new GMarker(latlng);
marker[markerCount].bindInfoWindowHtml("This is Marker number "+markerCount);
map.addOverlay(marker[markerCount]);
markerCount = markerCount+1;
document.getElementById("message").innerHTML = "Marker added at latitude "+latlng.lat() + " longitude "+latlng.lng() ;
}
As you can see, we have a global array of markers and whenever we create a new marker, we bind an infowindow to the marker using bindInfoWindowHtml method. Simple right? :)
We’ll see more complex event handling sometime later. :) Feeling sleep right now :)
Coding Style …
May 23rd
Some tips/tricks for nice coding skills are,
A few examples for C is posted here .
Sample Code 1 :
if (a == 5) {
}
else {
b = 20;
}
Things to be noticed in the above snippet are.
1. A space between if and “(” .
2. Space in both the sides of the comparison operator.
3. Space between “)” and “{“
4. Space between both the sides of assignment operator (line 2 & 5) . This is true for almost all the operators.
5. Proper indentation of lines 2 & 5. If you are using emacs or vi, check here for your .emacs or .vimrc file .
Sample Code 2
Let us have a function which takes two integers and returns their sum .
The code should be like
int add_numbers (int num1, int num2) {
return (num1 + num2);
}
The function call will be something like,
int sum;
sum = add_numbers (10, 20);
Things to be noticed in the above snippet is
In the first line in the function declaration,
1. The function name should be as clear as possible.
2. A space between the end of function name and “(” .
3. Spaces are given after every “,” in the function argument list.
4. A space is given between “)” and “{“.
In the second line in the function declaration,
1. A space before “(“. [ This rule is almost global. Apply it everywhere whenever you use "(" ] .
3. The indentation about which was mentioned earlier.
But yes, if your girl friend is a geek or a nerd or a psycho or a fundoo, then you better go for this. ;-)
#define MAGIC “eilouvy43605321″
#define _(p,o,q) (t o#p[0])?(q)
#define __(p,o,q) _(p,o,t-q)
int main(){int t, i; for(i=8;i>0;i–)printf(“%c”, MAGIC[(((t=(MAGIC+7)[i-1])==’_')?62:_(.,==,63):_(@,==,64):__(a,>=,’a'+36):__(A,>=,’A'+10):(t-’0′))]);}
Note :: I wont say the coding style i use is the perfect one. It always depends upon what your team was using till now and how easy it is to read, debug and maintain the code.
Useful Links :

Recent Comments