Saturday, May 14, 2016

A Regular Expression Engine in Python


Regular expressions are quiet useful in programming tasks. They are a useful method to search for patterns in long strings of text. For example the pattern "(c|b)at" allows us to search for either 'cat' or 'bat' in any string that this pattern is run over. This example was a simple one and regular expressions in practice are far more powerful and complex.

One must take care however in remembering that regular expressions are a loaded mathematical term. They are not the same as regex and regexp since those two do not make regular languages.

Here, for the sake of brevity we shall call regular expressions regex. Regexes are usually made using Deterministic Finite Automatons. They take a string of input symbols and tell us if the DFA accepted the symbol string or not. Such an activity is called the run of the DFA.

Steps
  1. We define that our regex engine will only provide the basic elements of concatenation, union and kleen star. Thus '|*' are the only special characters allowed besides the other things in the alphabet. 
  2. We first build a Non-Deterministic Finite Automaton with epsilon transitions using Thompson's construction algorithm from the given pattern.
  3. Then we convert this e-NFA to a regular NFA without epsilon transitions by the epsilon closure method.
  4. The new NFA is converted to an equivalent DFA by the power set construction method.
  5. The DFA is finally run on a string. The Union (|) and the Kleen closure (*). Concatenation is assumed for consecutive symbols.
  6. Upon completing the run, the DFA either returns True or False depicting that it has accepted the string or rejected it.
Example

For the regex '0|10*' (which means either a zero or one followed by any number of zeros) on the alphabet '01' the following strings return results as shown:
  • '1' : True
  • '0' : True
  • '01': False
  • '10': True
  • '11': False 
The entire code is available as a gist.

Wednesday, April 27, 2016

A Better Picture of Graduation



Just thought that since everyone goes about quoting graduation marks without realizing how misrepresented they are, here is my graduation with all the tests on record.

Practicals + Theory + Internals

Sunday, April 24, 2016

Why You Should Code

Every day, every second now, we are immersed in a world where dumb machines work for us. To list the obvious, your phones, TV, desktop, watch and, laptop are all computers. In the not so obvious list come power plants, rockets, cars, water supply, GPS, and, the post office. Despite living in this obviously computer driven world very few people know how to code. So I'm going to tell you why I code, in an effort to get you to pick up yourself and get going.

Programming at it's heart is communication. You instruct the machine. You tell it what to do, your desires, the things you don't like, the things you want more of, the things you want done quickly. You tell the machine what it is that you want done. In those moments of history, you are God. You can instruct the machine to anything and it will obey. It will obey in a manner you never thought possible.

There will be no complaint, no groans. There will be no raised eyebrows. You have a powerful ally at your command. You can designate repetitive tasks to it and it will do them till the end of time itself. You can assign some intelligent tasks to it and it will do them till the end of time. What the machine does is up to you.

The first problem I solved was downloading YouTube videos. I knew I wanted this entire play list and I did not want to go on clicking anymore. So I wrote a program for it and the machine obediently downloaded the entire play list for me. That is all there is to computing. Nothing fancy, nothing mysterious. Simple communication.

I now solve a lot of problems using my laptop. I have to apply for a PhD. So I write a program to suggest names of PhD guides to me.

There are lots of ways to communicate with the computer. Sure you can use something like Excel to do calculations for you. But in that you are limited to the options that the makers of Excel provide to you. Imagine this like talking to a person with cards. You can only say what is on the cards. Of course you will be limited! On the other hand, the way for unlimited communication is programming languages. Pick any one!

All programming languages are designed with one thing in mind. How can we maximize the things you can communicate to a computer. With a programming language at hand, you can communicate with your computer in an almost unlimited fashion. So learn one.

Try out Python! Try out Matlab( or Octave if you prefer free software like I do)! Try Java or even C!

All these languages allow you to communicate with the computer. All of them are powerful! With an untiring assistant at your beck and call, your life will be a lot simpler.