Saturday, August 25, 2012

Colors in bash

Bash is capable of generating a huge spectrum of colors for text. Here is a small code in shell script which generates all the 256 different colors bash can render:
for i in {0..255}; do printf "\x1b[38;5;${i}mCLR-${i}\t"; done; echo; reset;

The last echo is for printing a blank like after the output, without that the command prompt would appear at the end of the output. More import is reset present at the end, without which your terminal might not go back to the original settings.

In case you are wondering how to print colored output in terminal, please read:
Now you have a number to color mapping which you can use to specify your favorite color in config files of various command like applications like vi and tmux.

Sunday, July 22, 2012

ROC Curve

My recent work related to Text Classification got me introduced to ROC Curve which is a very effective way to compare classifiers against each other and decide on the cutoff value for classes.
Its best defined with a simple example. Say you have a binary classification problem. You have generated 3 train-test datasets from the original data. Say you are using Support Vector Machine (SVM) as the classifier. After training SVM on all the three sets, you want to select the "best" of these three. How do you go about it?

Here's where ROC comes into rescue. Sort the output of the SVM and compute True Positive %tage and False Positive %tage so far for each data point and plot this in x-y graph. The area under the curve will give you a way to measure the effectiveness of each classifier. Moreover you can use the nature of the graph to establish your positive and negative class boundary.

Here are a few links to know more about ROC curve:

Thursday, May 24, 2012

tmux, a Better Terminal Multiplexer than screen

Recently I encountered a program called tmux which claims to be better at "terminal multiplexing" than screen. I did not have any idea about "terminal multiplexing", but I had been using screen for quite a few years now. tmux promises to be better that screen according to the tmux sourceforge page. This got me interested.

I installed tmux, and have been using it since last 2 weeks. My experience has been great except for a minor hiccup between vi and tmux. I thought of writing a small tutorial, but I feel there are better and more detailed ones that I would have the patience to write; not to mention we all "stand on the shoulders of giants". So I am going to point you to the online resources:
Finally, tmux is quite good and it gives some features not in GNU screen. Unfortunately, tmux is not as popular as screen, so you should be able to use both. Here is my contribution to the community, a cheat sheet with popular commands for both tmux and screen together. Please note that I use Ctrl+a as my tmux hotkey rather than the default Ctrl+b. Here is the thing you have to add to your ~/.tmux.conf to achieve this:
unbind C-b
set-option -g prefix C-a
Copy-pasting in tmux is also quite easy. You can copy from any part of the command window. Go to the copy mode by Ctrl+a PageUp. Move around the terminal using arrow keys. Ctrl+Space to start marking the text from where you want to copy. Use arrow key to select text. Alt+w to copy the text. Paste text using Ctrl+a ].

Thanks for reading.

Wednesday, May 23, 2012

Reload .inputrc at runtime

If you have been playing around with bash for some time you must have changed the file .inputrc sometime. I have changed it several times mostly for Del key and auto-completion trick. However every time I change it I have to logout of bash for it to take effect. Here are two different ways to do the same without logging out of the shell.
  1. Keyboard shortcut : Ctrl+x followed by Ctrl+r
  2. Script : bind -f ~/.inputrc
Here is an excerpt from my .inputrc which many others might find useful:
"\e[1~": beginning-of-line          # home
"\e[3~": delete-char                # delete
"\e[4~": end-of-line                # end
"\e[5~": history-search-backward    # page-up
"\e[6~": history-search-forward     # page-down
Hope this helped you.

Tuesday, April 17, 2012

Brilliant 8-phase multi-quine in ruby

Wikipedia defines quine as:
A quine is a computer program which takes no input and produces a copy of its own source code as its only output.
In layman terms, it is a program that when executed outputs itself. Like a C program outputting its own source code. Kleene's Recursion Theorem proves the existence of quines in any Turing Complete Language. Its a good exercise to write one in your favorite language. Its fascinating to see quines.

Today morning my friend Umesh showed me this brilliant piece of ruby code that behaves as a Multi-quine. Link to original post by its author. Here's a listing of the same:



Save it as "test.rb" and run the following shell script:
while true;
do
    clear
    ruby test.rb | tee test2.rb
    mv test2.rb test.rb
    sleep 1
done

You will be able to see the picture changing after every 1 second and returning back to the original after 8 seconds. Note that when you run test.rb, it produces the output which is used for the next iteration. The shell script code lines 4 and 5 runs the code and uses the output of the code to replace the original code.

After being amazed by the beauty of this piece of code, I am not trying to figure out how exactly does it achieve this result. Someday I will post the same code with indentations and explanations in this blog.

Tuesday, April 03, 2012

Make Your Own Command-line Calculator with bc

I do not know about you, but I am bothered by the lack of a proper command line calculator for doing small single line calculations in Linux (or any other UNIX or Mac system). Sure bc is there but it does not support floating point by default, so one ends up writing things like:
echo "scale=3; 22/7" | bc
which is quite cumbersome for day to day work.

To subvert this trouble, I wrote the following two-liner and saved it as "calc" in a directory in PATH:
#! /bin/bash
echo "scale=3; $1" | bc; exit
Then do a "chmod +x" on the file to give it execute permission. This does the same thing as the above script, but is way simpler to use:
calc 22/7
The command "scale=3" was to instruct bc to print upto 3 decimal digits. You can change it as per your requirements.

The bc utility can do way more than this simple calculations. For example it can handle different bases and load functions from files. To know more about its capabilities try the good old "man bc" in terminal or read an online reference like this one on about.com.  You can read more about its advanced capabilities from an old but quite relevant article in LinuxJournal "bc: a handy utility".

Disclaimer : Although the script was by me, the idea is not new. In fact a lot of my colleagues use similar scripts for their own daily work.


Monday, March 26, 2012

Do-while loop in Ruby

As you know, I'm trying out Ruby. While solving one of the exercise problems I felt the need for a do-while loop. Unfortunately ruby has no documented do-while loop. Thankfully, somebody has already asked this same question in stackoverflow : Is there a do-while loop in ruby?

Here is a demonstration of a do-while code in ruby:


Remember that using undocumented features is always ill-advised !!

Saturday, March 24, 2012

I'm trying out Ruby

I have always been interested in programming languages. After coding in a plethora of mainstream procedural languages C, C++ and Java, last year I tried my hands on Python. Apart from being an enjoyable experience, learning Python has paid off quite well while building prototypes in my work. This prompted me to try learning some other languages.

While searching for a cool language to try out, I came across the book, "Seven Languages in Seven Weeks" which promises to go beyond the simple tutorials and "introduction to" and introduce some non-trivial aspects of the language. I have started from page 1 and am exploring Ruby. So far the author has kept up his promise.

Ruby has got very friendly constructs and syntactic sugars like:
  • the keyword "until", which is simply a negation of the popular "while"
  • instead of simple "for" loops, constructs like "10.times"
which gives a warm and friendly feeling to the language. Besides the book mentioned above I am using "Programming Ruby 1.9" from the same publishers for a reference.

Friday, March 02, 2012

Demystifying bulk SMS sender name

With the high penetration of mobile phones, companies have started sending out automated sms. I have always wondered about the cryptic sender names. Careful observations about the sender name reveals:
  • all letters are in capitals
  • name format is **-******
    • the first two letters seem random
    • followed by an hyphen
    • followed by a six letter
  • last six letters seem to identify the sender name
    • for example presently, "SBIINB" represents SBI bank
  • sometimes the same sender can send some sms with different combinations of first two letters
    • SBI sends sms as "BX-SBIINB", "TD-SBIINB" etc.
Some researching in internet points out that my observations about the last six letters are correct. The first two letters before the hyphen represents service provider and the region from which it is sent. For the examples above:
  • "BX" = BSNL from Karnataka
  • "TD" = Tata from Delhi
View the official list of bulk sms codes in the NDNC website.

Saturday, February 18, 2012

GATE 2012 CS solution

I had appeared in GATE 2008 and 2009. After that I had not been interested much in GATE. However this time (GATE 2012) a close friend of mine appeared for the same, which got me interested again. Here is a solution set to the GATE 2012 Computer Science paper. I have solved question from Algorithms, Automata, C programming, Probability ... in short, the topics that I enjoyed enough to remember them after 2 years.

As I mentioned earlier, I have not appeared in for the examination. I got the question GATE 2012 CS and IT question set C from a fellow netizen. Please download the paper first as I will be using that as the reference for objective type answers.

2 years earlier I had posted solutions to GATE 2009 CS question paper. Comments and suggestions from that post made me realize the importance of explaining every answer. Feel free to ask questions if you need further clarifications.

GATE 2012 Computer Science and IT solutions to question set C
  1. b
  2. -
  3. -
  4. b
    • Draw the sine graph for the interval. Only one minima at 3*pi/2 where sin(x) = -1
  5. c
    • fork() will spawn off a new child process every call. Three calls to fork() results in 8 processes, out of which one is original, other 7 are children.
  6. b
  7. a
    • Obviously. Look at f(X,Y) and compare it with X
  8. c
    • Balanced binary search trees require log (number of elements) time to search
    • log (n*2^n) = log (n) + log(2^n) = O(n)
  9. -
  10. c
    • Standard switch case behaviour called "fall through", when the break statement is not there
  11. c
    • Look into any DB book, I studied Navathe.
  12. -
  13. c
    • Do the checking manually
  14. c or d
    • 1 is false, 3 and 4 are true
    • not sue about 2
  15. -
  16. -
  17. c
    • follows from definition of CDF
  18. -
  19. d
    • 4 bit multiplier requires 2x4 = 8 bit input, i.e. 2^8 set of inputs
    • multiplication of two 4-bit numbers will result in at most 8-bit result, so 8 bits for every multiplication
    • 2^8 * 8 = 2^11 bits = 2 Kbits
  20. c
    • Average case is at lease as good as worst case
      • A(n) better than W(n) :: Quicksort
      • A(n) same as W(n) :: Heapsort
  21. c
    • Draw it and find out. They key thing is "any embedding of G on the plane"
  22. d
    • T(n-1) for moving n-1 disk to aux from source
    • 1 for moving the n-th disk to destination
    • T(n-1) for moving n-1 disk back to source from aux
  23. b
  24. -
  25. c
  26. -
  27. --
  28. -
  29. b
    • examine the cases:
      • P(6 in first throw) = 1/6
      • P(1 in first throw followed by 5 or 6 in second throw) = (1/6)*(2/6)
      • P(2 in first throw followed by 4, 5 or 6 in second throw) = (1/6)*(3/6)
      • P(3 in first throw followed by 3,4, 5 or 6 in second throw) = (1/6)*(4/6)
    •  summing up = (1/6)*(1+2/6+3/6+4/6) = 5/12
  30. -
  31. -
  32. b
    • b'd' -- group the 4 corners
    • b'c' -- group the a'b' columns to 2 cells with the top 2 cells in ab' column
  33. a
    • MST algorithm does not depend on the value of the weights, it depends only on the relative ordering of the edge weights.
    • Ranking remains unchanged as all the weights are >= 1
  34. b
    • I did it by hand, don't know if there is a shortcut
  35. b
    • Observe the graphs
  36. -
  37. d
    • observe that q is a dead state, so anything beginning with 000 should go to q -- A and B gets eliminated by this logic
    • after 10 if there is a 0 the state should move to 00 state, so D is correct
  38. a
    • look into any Data Structure book
  39. -
  40. -
  41. -
  42. -
  43. -
  44. d
    • Dijkstra's algorithm probes the best solution so far
    • Thanks to Shivam for pointing this out
  45. b
    • merge-sort takes O(n log n) comparisons
    • here to compare two strings you need O(n) time, so O(n^2 log n)
  46. c
    • represent a cycle by a permutation of 4 labeled vertices
    • cycles are identical if one is a rotation of another
    • same cycle of length 4 can have 4 different representations each starting with a unique letter
    • (6 P 4) / 4 = 90
  47. -
  48. -
  49. -
  50. c
    • line 2 has a static variable, which retains the old value when he function is called again
  51. d
    • "auto int" is same as normal "int"
    • "register int" is same as normal "int" as far as value retention is considered
  52. -
  53. -
  54. -
  55. -
  56. -
  57. a
  58. -
  59. a
    • maxima-minima problem
    • maximize the profit where, profit = 50q-5q^2
  60. b
  61. b
    • Given:
      • P(X) = 0.6
      • P(Y) = 0.4
      • P(R | X) = 0.96
      • P(R | Y) = 0.72
    • P(R) = P(X) * P(R | X) + P(Y) * P(R | Y) = 0.864
    • Applying Bayes Rule:
      • P(Y | R) = [ P(R | Y) * P(Y) ] / P(R) = 0.333
  62. c
    •  follows from fundamental definitions of mean and standard deviation
  63. -
  64. -
  65. b
    •  another maxima-minima problem and the function is already given, just maximize y

Please leave a comment if you were helped by my effort. Feel free to ask questions if you need further clarifications or have found some error in my solution.

Wednesday, February 15, 2012

Check who logged in to your Linux system

Linux (and all other unix clone OSs) have a handy way to let you know who are the users who have logged into your system. The command is "last". This command uses the file "/var/log/wtmp" and displays a list of all users logged in since the file was created.

I use it as:
last | grep -v -e bigyan -e reboot | less
Grep -v gives the complement of the results that it returns normally. The output of this command contains:
  • username of the user who logged in
  • terminal used by him/her ... pts/number
  • IP address from which logged in
  • Time of loggin in
  • Time of loggin out
  • duration logged in to the system
Read more usages of "last" command here. be sure to check out its cousin command "lastb" too.

Tuesday, January 31, 2012

TED Talks Download

I am a big fan of TED talks. Usually I like to listen to them while traveling. So far I have been downloading using the "download" button in their website. Today I found a very interesting website which gives links to all TED talks in Metalink standard file format. This can be used directly to search and download videos in high quality using any downloader that supports metalink.

The Metalink file format is an XML file which can be parsed by any xml parser. I use a Python library for parsing the xml and then use good old wget to get the file directly from TED.

Here is a link to the website for getting the metalink file: http://metated.petarmaric.com/

Wednesday, January 18, 2012

Opening vi and command line in same window

One thing I have always liked about emacs is its ability to open a terminal within it. This makes writing programs a lot easier as you can both compile/debug and write a program without leaving your editor. There exists a few plugins to achieve the same in vi, but none of them are stable and platform independent.

Yesterday while going through the gnu screen man page, I found that it supported splitting the screen. Using that functionality, its easy to open vi and terminal in the same window. Here is a screenshot of what I managed to get:


Here is the step by step guide on how to get this:
  1. Open vi in a screen : screen vi
  2. Split the screen by : ctrl+a S remember that the S is in caps, so you might have to press shift+s
  3. Go to the bottom screen by ctrl+a Tab
  4. Create a new screen by ctrl+a ctrl+c
  5. To switch between the split screens, use ctrl+a Tab
Look up the man page on screen in your system for further cool things that you can do. Here is a stort list of commands which I use everyday (other than the ones in top):
  • list all screens : ctrl+a "
  • disconnect a screen : ctrl+a d 
  • reconnect a screen : screen -r screenNumber 
  • list all the running screens from terminal : screen -ls
  • split screen vertically :  ctrl+a |