All posts by Abhishek Nagekar

Net Neutrality

Do you know, here in India, at most of the public places like temples, we have two separate queues. One for the ordinary folks, and the other one for the rich folks. Sometimes the rich folks are charged, sometimes they are just judged from the car they get down from and are treated likewise. On the other hand, the ordinary people are treated like cattles, pulled and pushed by the authority, because we are not really benefiting them much.

This is the reason I would almost always choose Internet over any other public institution, at least in India. I know that whatever my social status be, on the Internet, I am equal. I have just as much right to put forth by thoughts as any rich businessman or a politician. I know that I have got people behind me who would not tolerate anyone who curtails my rights on the Internet. Yes, Internet is my home, and I am Internet’s child.

But like always, there are people who cannot stand that the ordinary are getting this extra power. They want to bring all of this under control. They want to turn the Internet into their own playground where they will control the lines as in our temples, giving early and quick access to the one who pays extra and slow to the one who does not. They want to make sure only the ones who can pay get heard, and all the others are suppressed.

Net Neutrality

You are out with your family, on a nice sunday picnic when on a toll booth, you are told that your Maruti car is no longer allowed on this flyover because Hyundai has paid the construction company to only allow their cars through the shortcut. What would be your reaction?

Tim Berners-Lee (‘Inventor’ or the world wide web, or more technically, original implementer of the HTTP protocol) defines ‘Net Neutrality’ as the principle that each packet of data should be treated equally, and no discrimination should be made based on economic motivations.

The exact opposite is what some telecom companies in India are trying to do. It started with Airtel. All of a sudden, it struck Airtel that if websites and applications can make revenue by creating services like messaging (like Whatsapp and Facebook) and calling (like Skype and Viber) which in turn decrease the usage of Airtel’s own services of SMS and calling, it is their right to start charging these Internet services separately. They even got out with their VOIP pack that a customer had to subscribe in addition to the existing ‘base’ data pack, to use this service. Eventually it was dropped on account of the resistance.

Now this time, telecom companies have convinced TRAI that they must be entitled with the authority to intercept the traffic, to decide what websites to let users access, and what not, and if yes, then at what speed. Ridiculous, right? Charging more for extra speed can be justified, but why on earth are those innocent data packets discriminated. TRAI has even released a consultation paper asking 20 questions here. If you take a look, you would immediately see that the paper is made extra complicated to not make sense to you, yes you, the customer. The C++ standard looks simpler. LOL.

So what does it change for you, for me?

As a customer, it will change almost everything for us that has any connection with the Internet. Firstly, we will have to pay for the usual data plan we currently do. So far so good. Now, you can’t use any messenger services from this pack, so no Whatsapp, Facebook, WeChat and all. Similarly, no Internet calling services would work, like Skype and Viber. Well, they would work if you pay an extra Rs100 or so a month for the ‘Special-messenger-pack’ and the ‘Special-VOIP-pack’. Now who the heck are these telecom companies to tell me how and what sites and services should I use.

Plus, it also gives the companies the power to control the speed of your connection on particular sites. For example, maybe Flipkart.com will load up instantly, but Amazon.in would take forever to load up just because Flipkart is affiliated with your service provider, and you are forced to buy from Flipkart. Disgusting, right?

So, if you don’t want to pay Rs250 for data, Rs50 for Whatsapp, Rs70 for Facebook and Rs80 for Youtube seperately, then you should instantly head to

https://www.savetheinternet.in/

and send an email to TRAI, which can be done with 2 clicks, yes literally.

As a publisher, you might own a startup or maybe a modest blog like the one you are reading this article on. Now, large sites and enterprises with deep pockets can just call up these telecom companies and be like “Make my site free on your network, I’ll pay you the incentive.” This will make all the traffic go on that particular site or use that particular service and not yours, which was equally good, or maybe better.

The Internet is the only place guys, where everyone is still equal. We need to keep it this way, otherwise, we would be helpless. We know exactly how strong our law is, and how well behaved our politicians are. We know exactly how seriously we are taken by the mainstream media and the police. We, Indians, are like puppets for the rich filthy and greedy companies that rule here. And now, they are trying to close the only open source of information for us. Don’t let that happen.

A humble request to all my fellow netizens. Log on to the site and send an email to TRAI. Educate your fellow netizens. Together we can stop this.

https://www.savetheinternet.in/

For further reading, here is a short list.

What programming isn’t

There were days when I used to search the web for ‘best programming languages‘ and ‘most popular programming languages‘ in my free time. I was not able to decide myself what language to learn and invest my time upon, such that it would pay me back everything I put into it. This was really a hard task, given the fact that I have interest in a wide range of stuff related to computers, from algorithms to designing beautiful forms with CSS.
Given that I had no guidance from the people around me, since I had no one in my family from the technical field, and the college professors were not of much help either. Now that justifies my amateur-ish googling habits.
Leaving that aside, I have lately been asked a lot of times by my fellow mates, what is the ‘best language‘ to start with and what are the current ‘hot languages‘ in the market. To start with, there is no language that is ‘best’ in every sense. If there was, why would anyone bother creating additional languages in the first place, right? Languages are written and implemented with certain goals in mind, which it should satisfies better than what was present before creating that language, that’s the whole point about it. So stop searching for the best language and start looking for your own needs.
Secondly, I don’t think anybody should care about the ‘hot languages’ in the market, except for the tech magazines’ journalists. Why? Because if you haven’t heard of it yet, then you probably don’t actually need it yet. It is much better to know a single language as a whole, than to learn 5 languages just for the sake of them being ‘hot’.
If you are new to programming, then most probably you are taught programming using some or the other language (most probably C++ or Python). What most fail to teach is that programming is not equal to learning a language. Programming is the mindset that you develop when you are solving problem for a wide range of sub-problems by writing a common solution to all. Most of the times it means looking for a pattern in the chaos of that problem. You don’t need to know a language for finding this pattern, do you? You have to realize this thing at the earliest.
Programming language that you choose is the tool you choose for the task you have in hand. Now, depending upon the task, some tool might be better than the others, and help you in doing the task quicker. You can think of an analogy here, like if you want to cut tree trunks, you can use saw blade or maybe even a kitchen knife. But the saw will get the job done much faster. However, any sane human won’t cut vegetables using a saw. The same goes for programming languages. Different tasks demand different languages, and each has its own pros and cons. But the languages won’t help a bit if you cannot figure out the problem itself in the first place.
I had start to program in C, jumped to Python because ‘Google used it’. Then I came to PHP because I read somewhere that PHP gets you a job quickly. I started with JS and Node.js later due to their growing popularity. I did a lot of mistakes when I started it all. I wish I knew it then, that instead of wasting my time on learning the syntax of languages to do the exact same thing I did with the previous language, I could have been much better off learning some interesting algorithms that would’ve helped me in solving some tough hackathon problems.
So is the knowledge of language implementations not useful? Of course, I don’t mean that. A language is very useful once you know how to use it. You don’t have to know everything from a language. Just enough to get the problem solved. Nothing more. For example, you might have probably heard of C++. If you have ever tried to learn C++, you might have realized that it is a really large language. Originally designed for low level embedded programming and as a ‘C with Classes’, C++ has evolved to be a language that can be used in almost every domain. The ‘Standard Template Library’ is large enough for you to not miss anything in most of the cases, and even if you do, there is boost at your rescue. So do you think you most know all of the above to call yourself a C++ programmer? No. Many great programmers who use this language use just a subset of it and they are fine with it. I personally don’t use the object oriented facilities of the language much, but it doesn’t matter as long as it does what you want it to do. So don’t aim to know an entire language unless you want to get into compiler development or it’s standardization committee. Focus on the thing you are working on, and not the language you are using.
Have the enthusiasm to solve problems. Take pride in building things from the scratch, all by yourself. The love for programming comes in automatically. But most important of all, learn how to program and not how to write syntax of a particular language.
Thank you for reading.

Engineering

Engineer.
What comes to your mind when you hear that word? To me, the picture of a hard working individual, who is rational in his way of life, sees every aspect with the touch of technicality, given a job, he will do whatever it takes to get it done, selfless about his own personal pleasures, solving the problems of this world, one by one.

Well, at least that is what comes to my mind. The reality is far from it. We have more engineering colleges here in India than we have good hospitals. Why? Because you cannot charge Rs.400k from each patient that visits your hospital the way you can from the students that study engineering course. In the particular state that I live in, engineering colleges are a part time income source of the politicians here. See the problem in India is, everybody wants to get rich, before the other one. Now there is nothing wrong with it as long as you don’t steal people from their rights.

A student, passed out from junior college, is an empty vase eager to absorb knowledge. He is excited about his life ahead, his life in a professional institute where he will be taught the essentials to work in the industry for most of his life later. He has dreams, just like everyone. He has interests, and he tries very hard to get the stream of his choice. He had been working hard since the very day he got admitted in school for this particular moment.

Finally the moment of truth arrives. He is admitted into a good college. He even gets the stream of his choice. There is no bounds to his happiness. His life is all set to rock, in a sense. Isn’t it? His parents pay their hard earned money to the college, thinking that it will benefit their child to become a better man, an ‘educated’ person, a responsible citizen. Now the student’s joy know no bounds. He is excited as a child is on his first visit to an amusement park. The can’t sleep the day before his first day at college. Its time.

On the first day of college, he wakes up early. Gets ready and dashes his way to the college. He has 4-5 blank books in his bag, to make notes on what the professor teaches. He trips on the way multiple times due to excitement. On reaching the college, he takes the first bench in the class. “I would be able to pay more attention from here”, he whispers. The professor enters the classroom with a set of papers and a fat reference book. He speaks out his name, rather lifelessly, and gives a very brief intro on the topic he would be teaching that semester. The intro was rather too short.”This is it? Why doesn’t he speak more on it.” whispers the student. He was enjoying it. But the professor must hurry, a vast syllabus is waiting to get done.

The rest of the day goes similar. Nothing out of the ordinary. Nobody wants to talk anything out of the textbook. Probably it will waste a lot of time of the professor. Days pass by, and the only thing that changes is the number of books in the student’s bag. From 6 to 3 and then to one common rough book for all subjects. No, not because he was lazy to carry them all, but the pathetic quality of notes his teachers gave, he simply didn’t feel like they are worth of writing in separate books.

After a few boring days with no activity, which he later realizes were the best days of the semester, the professor starts to give them assignments as homework. Not the kind of assignments where you can research and write on your own, but the do-it-by-tomorrow-atleast-30-pages-or-get-a-C-grade kind of assignments. Alas, there isn’t enough time to find the solutions. He gets readymade solutions from a friend who had copied it from another friend who might have got the solutions from previous year’s journal of a senior, and copies all the answers into his assignment.

There is nothing in practicals too. Most of the time he is made to write journals so that he might submit them on time. So basically practicals are library hours where he gets to do nothing ‘practical’ but copy from other’s journals so that at least he can get an A grade, which eventually by the end of the semester gets him 5 additional marks, at the cost of wasting the precious practical hours where he could have maybe learnt how that particular IC works.

Days fly by quickly. Sitting for lectures where you don’t understand a word of what the professor says, but still have to, for attendance. Yes, the university enforces 75% compulsory attendance but fails to enforce benchmarks for the quality of lectures the professors give. Maybe enforcing the later will automatically make the attendance, not only 75% but also 90%, of most students.

Failing to comply with the attendance policy of the college results the student not being able to attend his exams and parents getting called up to meet the professors in college. What happens when a professor comes to teach a topic he has hardly any idea of, gives the worst lecture you can possibly imagine with all bits of information flying around, and then leaves? Nothing happens. Even if you do complain about it, the replacement is as good as the old one, so most of the student don’t even bother to do that.

Technical festivals. Yes, the thing our colleges are famous for. Technical festival, or tech fest as we call them, are something I personally waited for the entire semester. I could imagine all the amazing stuff that would be put to display, the workshops, the seminars and the enthusiasm. When it came, it was nothing as I had in my mind. It was all guys and girls hanging out enjoying their accidental holiday. It was foolish games like laser tag, devil_maze where people were made to run through dark corridors created with black curtains and UV lights. It was nothing technical, only games-which-can-attract-most-participants-and-earn-most-ticket-fees festival.

This concludes with the semester exams, or finals. Here all we learn is the questioning pattern of the university so that we can quite accurately guess what is going to be there on the exam the next day and how am I going to fool the paper checker into believing that I know something about the topic, not much but something to earn me passing marks, after which I will spend 15 days doing nothing but hoping the next semester is better than this one.

Finally, a student has covered 1/8th of his life in the engineering course, spending a fortune as time and money, achieving nothing, and now the only thing differs is that he knows how the rest 7/8ths of this life of his, which he highly anticipated, are going to be like. Welcome to the reality.

Time

Time is one of my favorite subjects. The reason being it’s highly mysterious and absolute nature. It give everything a reference frame. How else on earth would have you said when a thing actually occurred. It actually gives even the rest of the three dimensions a reference frame. Newton put forth the theory that nothing in the Universe is absolutely stationary, no not you standing still, because the earth is spinning, and not the earth since it is revolving around the sun. The sun goes round the Milky Way (our home galaxy) once in every 225 million years. Not surprising now, the Milky Way is not stationary itself, but moving towards Andromeda (our neighboring galaxy) at about 400,000 km/h. Point here is that, you cannot determine the absolute position of an object anywhere in the Universe by just giving the three physical dimensions. It simple doesn’t work where there is no frame of reference. Then how do you tell your position? You tell it in terms of space-time. The fourth dimension, that is of the time can be in any unit of time. We can actually convert the first three coordinates  in terms of light-seconds which is distance in terms of time taken by the light to travel the same distance.

But well, time is in fact, only absolute to a person at a particular position. Time is well affected by gravity, the same way light is. This is to simply say the time seems somewhat slower near a body of high mass, like the earth, or a black hole for some serious observations. We don’t actually see these differences in our routine life since they are too small in case of the Earth (Earth is not as dense as it should be for practical effects of delay in time to be observed by us). But there are applications on Earth that require precise measurement of time, and one such application is the Global Positioning System, the GPS in almost every phone these days. It relies on three geostationary satellites that measure the exact time taken for the receiver to reach them, creating a triangular plot, estimating the position of the receiver on the surface on the planet within a couple of meters accurately. These satellites are feed with the slightly wrong time after some unspecified interval, just so that it can adjust with the ‘slower’ time on the Earth’s surface (on account of the larger mass of Earth affecting the bodies near to it to a larger extent and also due to the relatively faster time on these satellites on account of their speeds due to special relativity).

There is another interesting thing to note here. How can you say if you are experiencing a slower version of time than you did some time in the past? You can’t. The reason is, ‘slower’ and ‘faster’ time are for the observers that are experiencing a relatively ‘faster’ or ‘slower’ time. So it is all relative. Talking about time in the context of speed of light, we come to an amazing theory by Einstein. Einstein proposed that the speed at which light travels is absolute and does not depend on the relative speed and position of the observer. To put it simply, imagine you are traveling on a highway at 60km/h. Another vehicle traveling at 70 overtakes you. For you, the relative speed of the other vehicle is 10km/h (which is, 70-60=10). This is how we expect things to behave right? Of course. But things change as one approaches the speed of light. See that light doesn’t behave the same way our highway example cars did. However fast you’re traveling, light will still be 300,000km/s faster than you, even if you are actually doing 290,000km/s (which is just about impossible for a body of large mass like us, but certain elementary particles can achieve those speeds). This can be considered the universal speed limits. So what happens when some particle tries to break this barrier?

For understanding this, let us consider the cone of light. When a particular event occurs, it sends out light (or radiations) in all possible directions. This is like throwing a pebble in a pond of water. It spreads in all possible directions and it’s size increases every instant. The outermost wave is the wave initially created by the pebble itself. Now think of this as light. An event occurs and light from that event spreads in all possible direction. Considering time on y axis and space on x, we can imagine a cone getting created.

https://upload.wikimedia.org/wikipedia/commons/9/9a/World_line.png

Now when something occurs, you don’t see it immediately, until the light from that event hits your retina. Consider the sun, for example. It is at a distance of about 149 million kilometers from the earth. Light takes about 8 minutes and 20 seconds to travel this distance. This is the reason the sunlight you’re seeing now is about 8 minutes and 20 seconds old. Which means that it takes the cone of light from sun, 8 minutes and 20 seconds to reach us, and make us aware of it. Still, considering the distances of other planetary objects, sun is quite close to us. Our nearest star, Alpha Centauri is at a distance of 4.3 light years from us, that is, the cone of light takes 4.3 years from Alpha Centauri to reach us. Since it is the cone of light, not surprisingly, it travels outwards at the speed of light.

Back to our original question, what if some particle tries to break the barrier of speed of light? If that happens, it will move from inside the cone of light of one event to outside of it, effectively seeing the time from the past, hence ‘traveling in the past’. But is that possible? No, according to the special theory of relativity. Since speed of light is absolute to the reference frame it is measured in, nothing can reach it (the mass of an object goes on increasing with increasing velocity. Close to the speed of light, the mass of the object is infinity and pushing an object of mass infinity is not possible). This was even proved by an experiment (Michelson-Morley experiment). This theory was consistent with the observations and most other theories, but Newton’s gravitation theory. If everything travels at speeds less than or equal to the speed of light, how do we explain the effects of gravitation on distant planets and stars instantaneously. Does gravity travel at infinite speeds, as opposed to relativity, which states that gravity travels at speed of light, which is again, not in accordance with solar system observations? Maybe.

Einstein spent a great deal of time trying to find a theory that would be consistent with both, the theory of relativity and the theory of gravitation. Finally he came up with something called the general theory of relativity. This theory suggested that the force of gravity doesn’t act like normal forces but it acts on the ‘space-time’ fabric, bending it in proportion with the relative energy and mass of the body. This suggested that the bodies revolving other bodies like the moon and earth are not traveling round the other body, but actually taking a straight path in the space time fabric which is 4 dimensional, and they only ‘appear’ to travel in elliptical orbits as we see them in 3 dimensions. This was a revolutionary suggestion. It implied that time, light all traveled curved path near objects of high mass or energy. That did, infact explained why time appeared slow near the surface of earth and faster from space, and also, how are we able to see stars that are just behind the sun even if a straight path to them would not be possible for light to take.

Now as we look at where this article started, yes, I was all wrong. I knew there was nothing as absolute space, but as it turned out, there is nothing as absolute time as well. Interesting though, isn’t it?

Time in the world of computers!

Computers happen to be my most favorite subject after astrophysics, so how can I let this special opportunity go without playing around with time functions in C++. To be honest, I had been on this time thing from about 2 weeks. I needed to calculate time precisely for looking at the efficiency of sorting algorithms. Not surprisingly, computers are way too fast for calculating time in seconds or milliseconds. To see the difference in time taken by two algorithms for sorting a set of 50 random numbers, you need to measure time in nanoseconds, at the least microseconds. Although I had trouble (a lot of trouble, in fact) in getting the time functions to work for me, I finally succeeded in getting nanosecond level precision without any extra props, just my PC and g++ compiler.

Starting from the first thought that came to my mind, get some C++ reference for built in functions. time() from <ctime> does a great job to fetch the number of seconds since 1st Jan, 1970,

but not good enough for me here. I knew that the bash command ‘date’ can be used for my purpose, but I couldn’t see a way to get the output of the command to my program back. I did some over smartness here. I executed the system(“date +%s%N > text”) which wrote the current timestamp upto the nanosecond to ‘text’ file. Then copy that time by reading the file into an unsigned long long int (since it is 19 digits long). Then after the calculations, repeat it again and subtract the initial time from the final one. A bit long cut, but I hoped it to work.

When I ran this code, it took, surprisingly 18+ milliseconds, or in particular 18649390 nanoseconds. And to be clear, I haven’t even included any code whose performance benchmarks I had to measure. This is the base time, LOL. Thinking about it for a second, algorithms store variables in cache, because RAM is not fast enough and I tried to write and read from the hard drive. That is so damn clever of me!

Pun aside, Now it was time to get help. Many stackoverflow posts and a good guy’s guidance later, I finally got the clock_gettime() from <sys/time.h> to work. Yes, now I was getting some pretty good numbers and I could see the difference when I added more stuff to the code.

Executing the raw code without any load gave some results I was hoping to see.

210 nanoseconds which is fine for a code that does nothing. This will serve all of my purposes, and should do most of anyone’s who is up with sorting algorithms.

PS – I tried hard to find good sources for information on the first part of this article, still if there is any mistake, don’t hesitate to notify me.

Handling large numbers with C/C++

I will tell you what this is about. I saw a simple looking problem on HackerEarth about finding factorial of given numbers. It looks easy, but another look at the constraints (1<=N<=100) changes everything. Well, not everything, especially if you are going to write it in languages like Python (“dynamically typed”) which has built-in capabilities to handle big numbers, but it is really a trouble to do it in C or any such statically typed language.

So first of all, what exactly is the difference between statically and dynamically typed languages. Dynamically typed languages require their interpreters to detect the type of the variable from the value that is assigned to it. On the other hand, in statically typed languages, the type of the variables must be known at compile time.

Some believe the latter has advantages over the former. As we explicitly state the type, run time errors are reduced and run time performance increases. We won’t get into that discussion here.

So our problem

Before getting into it, I will first write the Python code that worked flawlessly and gave answers to factorials of over 100.

As you can see, it doesn’t get any easier for anybody, no matter how novice he is with programming. But how does one solve the same problem with languages with no native support for big (of order >10^100) numbers? Simple. We make use of algorithms. The first thing that pops in one’s mind when dealing with numbers this large is the use of arrays. Yes, that is the right way to go, or at least the one that worked for me.

So here is the plan. We create an array of integers which will hold a digit in each index, starting with the least significant bit. For example, if we were to store the number 12345 in the array, we would do it like this:

54321

That is, array[0] stores ‘5’, array[1] stores ‘4’ and so on. We have reversed the number for a specific reason. For knowing that reason, you have to go back to your 2nd grade class where you were taught to multiply two two-digit numbers. How did you do that?

4
2
37
x63
111
2220
2331

Got the memory back? Although it may seem a trivial thing now, notice that you never do a multiplication whose result is more than 81, that is, 9×9 which is the product of largest two single digit numbers. So can you somehow make the computer follow the same method to calculate the factorial, such that it never does a >81 digit addition in the entire process, which is well inside the size of the shortest numeric data type in C (unsigned short: 65,535)? Yes, of course. We are coders, right? 😉

To start off, we will need variables. We will use num to accept the input number whose factorial is to be found out. cur stores the result of the calculation i * arr[j] + temp. The least significant digit from cur (for example 3, in case of 123) goes into arr[j] while the remaining digits get stored in the temp variable. Follow the above step till the end of the array which we initially denote by pos variable. pos is initialized and assigned 1, as we initialize our arr[] with arr[0]=1 (since we will be using this value to multiply subsequent 2,3,4…num, we don’t want our answer to evaluate to 0).

After this loop, we will need to empty out the carry forward integer in our temp variable. It will be done in the reverse order as well, but here, we will increment pos to make it always point to the number of digits in arr[].

Finally you can print our the arr[] in the reverse order to get the expected answer and this should not surprise you since we have been doing storing of the numbers in reverse order in our arr[]. Here is the C++ code that I wrote. I didn’t cross check the results for larger values of num, so take care with that.

So that was it for this short article. I am reading more on GMP (GNU Multiple Precision Arithmetic Library) that is written exactly for this purpose. Nevertheless, it is always good to know how to do it by hand. Thank you.

What is GNU/Linux

The following is a piece from my previous blog. I had written it in one of my diaries and since that blog of mine is no more, I would like to publish it here. My views on Linux/GNU and why it is one of the most amazing thing you will ever learn. Here it goes…

What is Linux?

 

In the early 1990s, Finnish computer science student Linus Torvalds began hacking on Minix, a small Unix-like operating system for PC then used in college OS courses. He decided to improve the main software component underlying Minix, called the kernel, by writing his own.
In late 1991, Torvalds published the first version of this kernel on the Internet and called it Linux, a mix of his own name and Minix.
When Torvalds published Linux, he used the GNU’s General Public License which made the software free to use, copy and modify by anyone provided that the copies and any variations are kept equally free. Torvalds also invited contributions from other programmers. Though these contributions came slowly at first, as Internet evolved, thousands of hackers and programmers from around the globe contributed to his free software project.
The Linux software developed so quickly that today, Linux is a complete modern OS, which can be used by programmers and non-programmers alike.

What makes Linux so special?

 

The building blocks of Linux OS are the ‘tools’. If you ask for a rough definition, ‘A tool is a small piece of code that is designed to perform one and only one task with great precision’.That’s it.
The entire Linux concept is based on thiese little pieces of code. Most operating systems (like Microsoft Windows) have large utilities called applications. These applications can perform a large number of functions or tasks, for example word processors, presentation designers or a web browser. Along with their main tasks, this applications also perform some side tasks like search, replace, spelling checks often found in all applications. The source code for these applications is stored separately (or each binary has a separate set of these instructions) for each application, hence taking up more space on disk as well in memory.
These applications are often closed source, meaning it will do your job like magic, but you will never understand what is happening in the background (like what methods are implemented to search, can it be revised to make it more efficient and such). Hence programmers can never learn, use or build anything from it. The end result of this approach is that the same functions inside all these different applications must be built by programmers from scratch, separately and independently each time – a set back to the progress of the society as a whole and waste of countless man hours and energy that programmers use to code the same thing again and again.
This is where Linux is special. Most of the tools (or all, for that matter) are open sourced, programmers can integrate them straight away without much effort to build something that takes the community as a whole ahead at a faster pace. Also, you don’t have to spend any time debugging the tools most of the time because it is almost always that someone has used it in production before and rectified the bugs, as they are always there. Saves a lot of time for us, as developers.
There is also this interesting functionality called ‘pipes’. Pipes behave as one would expect from the name. It ‘pipes’ the output of one tool to the input of another. If you didn’t think about it already, you can create powerful tool chains that do multiple tasks in co-ordination giving the expected result, and using individual tools in sequence would have. Just as the tensile strength of steel is greater than the added strengths of its components nickel, cadmium and iron – multiple tools could be combined to some unpredictable results than that of the individual tools, called as the concept of synergy, basic philosophy of all GNU/Linux tools.
These basic tools which have been improved over these decades are crafted to do a particular task to perfection, and even if it doesn’t fit your requirement, you can always grab the source, edit it accordingly and use it. If you want to keep up with the open source spirit, post it online so it will save some time of someone with the same problem. These small tools can be a power weapon in the hands of a Linux expert, or Wizard as we call them.
Note that when I use the term ‘Linux, I mean GNU/Linux. The entire thing wouldn’t be possible with either one. Linux is the kernel and rest of the OS is GNU. More information here: https://www.gnu.org/gnu/linux-and-gnu.html

Jumping over multiple programming languages

I just happened to take a look at my own blog. Starting with some posts about python, I went to C, then PHP-MySQL and then Javascript. All of this in around 3 months. Now, it is great to know multiple languages, but the thing I feel is, you must ‘know’ them. Knowing in the sense, the underlying philosophy, best practices and such. I have started with at least 10 languages this year. I can code little bits in all of them, namely C, Java, Python, PHP, Javascript and some other like database, HTML/CSS which are not-so-much-programming languages. Problem is, I cannot code fluently in any of the above listed languages. I just tend to get bored by doing a language after some time and then jump to something else. Something needs to be done, right?

Actually, I didn’t notice this myself. Friends, with whom I hangout, told me that I needed to concentrate on one thing, until I master it, before moving to something else. I couldn’t agree more, but also couldn’t decide what needs to be done. So kept trying different things till I get something that would keep me distracted for long enough.

I started with web development. I hate PHP, although I also tend to code in it the most because it is efficient for quick dirty works. Python happens to be one of my favorite languages, because of the neatness and power. But at the same time, coming from C, I don’t feel the depth in other languages that I got in C.

Web programming is great for some quick compliments. LOL. Just after a week, I could write great pieces of code that would amaze everyone. But deep down somewhere, I didn’t feel good about myself. Its like, choosing the easy path rather than the challenging one. So I thought again, about some other stream. Yes, software programming is pretty good.

So I started with Java. I did learn all the basics and some intermediate stuff. Java is great, but still, I missed C. I needed something close to the OS. I needed a language that I could use to interact with my Arduino. I needed something like C. So why not C again, perfecting it?

Yes, I felt, getting back to C seemed to be the best way to go.

Then, out of nowhere, I got to attend Bjarne Stroustrup‘s lecture at IIT-Bombay. Although I never got to get into the actual seminar, his mere presence was enough to push me into C++. I read about it, and even watch some online videos about C++ and opinion from experts about it. Seemed perfect. The control and power of C, the flexibility of Java and the usability of (almost) Python.

I have now been doing C++ for about 2 weeks. Not much really, basic syntax and stuff. Important thing is, I am enjoying it. I really hope I stick to it for some time now till I master it. I don’t want to be an example of

jack of all trades and master of none

Let’s hope my next post here has some serious C++ in it. 

AJAX registration form using jQuery

I have created this simple registration form which makes user registration using asynchronous requests in the background, checking if a duplicate username or email exists, and presenting the user with status of registration on the same page without have to reload the page even once.

It is simple, but with some prerequisites for my particular example. They are

  1. MySQL database for saving registrations
  2. PHP registration page for connecting to database
  3. Some basic javascript knowledge

So having set up the above stuff, we shall get started. If you don’t want to code all yourself, use my code from Github.

First of all, create for yourself a nice form. It doesn’t take much right? Just make sure you attach an ‘id’ attribute to each input field for convenience later. Here is my version.

If you see, after the form I have left an empty div. It is for displaying the response we get from our ajax request. You can, of course, add nice styling to this div. Having our form set, we can concentrate on the jQuery part.

Start with making sure our DOM is loaded with $(document).ready() function. Grab hold of the click event of the form submit button. Then we get the values in the username, email and password fields.

Here, we can have some basic checks to make sure form is filled up. We could have used the required attribute to make sure all fields are filled, but that does not give us full control, so use Javascript or jQuery and display some custom messages. I clubbed them together in a single if.

Next, using the simple $.ajax() function, make a request and get the response code. My action page is register.php, and I have make simple error codes to detect what response the backend gave. Here are the codes, for some reference.

// Code returns 101 for "username already exists"
// Code returns 102 for "email already exists"
// Code returns 104 for "internal server error"
// Code returns 202 for "user created successfully"

Refer register.php for complete code.

If the query succeeds, we capture the response in responseText variable. We can then do any response based manipulation we need for our specific needs. Just make sure to return false at the end to prevent default submit behavior. Here is my final jQuery snippet.

Finally, the entire HTML code has nothing more, just the standard doctype and stuff. Here is what to expect, or similar. LOL I was trying some CSS so ignore all that 😉

Also, I wanted to tell you, I switched from the classy syntax highlighter to this new (for me!) gist. It is cool, only a little more effort. Loved it. If you own a blog and post code frequently, give it a try. Thank you for reading.

On why PHP removed magic quotes – Nice explanation

I am a newbie in PHP, or at least not as good to call myself mature. Recently, PHP announced deprecation for magic_quotes in version 5.3 and complete removal in version 5.4. So there are official docs on why it was removed, but this is what I found written by a contributor and it was perfectly explained. Do check out.
Here goes the comment: –>
The very reason magic quotes are deprecated is that a one-size-fits-all approach to escaping/quoting is wrongheaded and downright dangerous.  Different types of content have different special chars and different ways of escaping them, and what works in one tends to have side effects elsewhere.  Any sample code, here or anywhere else, that pretends to work like magic quotes –or does a similar conversion for HTML, SQL, or anything else for that matter — is similarly wrongheaded and similarly dangerous.
Magic quotes are not for security.  They never have been.  It’s a convenience thing — they exist so a PHP noob can fumble along and eventually write some mysql queries that kinda work, without having to learn about escaping/quoting data properly.  They prevent a few accidental syntax errors, as is their job.  But they won’t stop a malicious and semi-knowledgeable attacker from trashing the PHP noob’s database.  And that poor noob may never even know how or why his database is now gone, because magic quotes (or his spiffy “i’m gonna escape everything” function) gave him a false sense of security.  He never had to learn how to really handle untrusted input.
Data should be escaped where you need it escaped, and for the domain in which it will be used.  (mysql_real_escape_string — NOT addslashes! — for MySQL (and that’s only unless you have a clue and use prepared statements), htmlentities or htmlspecialchars for HTML, etc.)  Anything else is doomed to failure.

Creating an autocomplete field | Setting up frontend | Part 2

In the first part of this article, we had set up the backend MySQL database. Now we can write the code necessary to get information from the server and convert it to a format we can parse, JSON in my case here. So lets begin.

Step 4 – Writing script to get data from MySQL database

As always, we can write it in any language we are comfortable with. PHP happens to be mine. The script is straight forward, accept the pincode via a GET request, ‘pincode’ parameter. Query the database for its existence, add it to an array and using json_encode() function in PHP, display the output. If value does not exist, it will return null, and it is not our concern here about what happens after that, as it will be taken care by the application using this API.
Code: retrieve.php
<?php
$pincode = $_GET['pincode'];
$conn = mysql_connect("localhost", "root", "password");
mysql_select_db("turnouts") or die();

$pincode = (string)mysql_real_escape_string($pincode);

$sql = "SELECT taluka, district, state FROM pincode WHERE pincode = '$pincode'";
$data = mysql_query($sql);
if(!$data) {
die(mysql_error());
}
$row = mysql_fetch_row($data);
$taluka = $row[0];
$district = $row[1];
$state = $row[2];
$state = str_replace("\r\n", "", $state);
$taluka = strtolower($taluka);
$district = strtolower($district);
$state = strtolower($state);
header('Content-type: application/json');
$array = array('taluka'=>$taluka, 'district'=>$district, 'state'=>$state);
echo json_encode($array);
?> 

The URL will follow the pattern, /retrieve.php?pincode=000000 and return data. As an example, the following image,

If you have come successfully to this step, then bingo! We now have a working API.

Step 5 – Writing an AJAX form using jQuery

 We will now write a simple form with 4 fields in HTML. Include jQuery in that page, and then write the script at the bottom which will query our API asynchronously.
Code: pincodes.html
<html>
<head>
<title>Pincodes</title>
<script src="js/jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="form">
<label for="pincode">Pincode:</label>
<input type="text" name="pincode" id="pincode" required /><br />

<label for="taluka">Taluka:</label>
<input type="text" name="taluka" id="taluka" /><br />
<label for="district">District: </label>
<input type="text" name="district" id="district"><br />
<label for="state">State:</label>
<input type="text" name="state" id="state"><br />
</div>
<script>
$(document).ready(function() {
$('#pincode').keyup(function(e) {
var pincode = $(this).val();

if(pincode.length == 6 && $.isNumeric(pincode)) {
var req = 'retrive.php?pincode=' + pincode;
$.getJSON(req, null, function(data) {
                 
$('#taluka').val(data.taluka);
$('#district').val(data.district);
$('#state').val(data.state);
});
};
});
</script>
</body>
</html>

Some explanation goes here.
The .keyup() event fires when a key is pressed in that field. We could have use onFocus() or onBlur() in javascript but that only fires when a field is focused or blurred, so this is much better. Since we have pincodes of length 6 digits here in India, to prevent script from querying for the first five digits, we have added a if(pincode.length == 6) to our code.

As I said, the even fires as soon as one enters the 6th digit and populates the remaining three fields with the data it retrieves.

It is hard to show it in a still image, but having just entered the pin, rest of the fields get populated.

Finally

So this was it. I hope you enjoyed it. Any corrections or suggesting, mail them to me or just comment down below. Thank you for reading.