Wednesday, July 11, 2012

How random can people think?

    Since, I have been doing some research work at college these past 2 months. I have been getting several weird ideas of surveys and testing stuff
    I first tested my laptop's performance with 100% power usage. Will share the results in the next post. Now I want to know how random can people think.
   We may think that we are selecting some random number but always there is a certain number which comes to our minds first. For example, when giving math problems to my mom's students I often selected the numbers 23 and 32. Don't know why. Also, there will be a number which is a common favorite of the mass and is bound to come often in the survey. Lets see the result.
   Here is the link to the form. You just have to enter 5 randomly chosen numbers. Don't take too long to think the random number. Just type the first 5 that come to your mind!
   https://docs.google.com/spreadsheet/viewform?formkey=dEp0U2dfTV9VaHQxeDJQU0Zad3hqc1E6MQ

   Will share the results next week

  

Wednesday, June 20, 2012

In place swapping of n digits? Okay done!

    Yesterday in order to solve a coding question I had to swap 4 digits in place! Now I knew the formula to swap 2 digits in place, so I thought, the math cannot be that hard for n number of digits can it?
    Turns out I was right! I tried creating some formulas and then, ahha! I found it! Really simple one! Here it is:
   Say you have variables a,b,c,d and you want to assign values in this fashion:
   a->b b->c c->d d->a. This is how you'll do it:
   a = a+b+c+d
   b = a-b-c-d
   c = a-b-c-d
   d = a-b-c-d
   a = a-b-c-d
   This can simply be extended for n number of digits. Easy naa!

  

Sunday, May 20, 2012

Busy days going on

    Well believe it or not I have been busy! Hence was not able to post anything since long. Though, the good thing is, I have been busy working on things that I love and hence I am not getting bored! Here are 2 things that I am working on right now.

1. Getting Things GNOME!
         Getting things GNOME! is an organizer for Gnome Desktop environment. Needless to say that it is an open source project. It has a really cool team of contributors. I have solved bugs related to event handling and UI so far. Some updates are waiting approval. All in all, this is my first experience of working in a software team and the conversations on IRC and mailing lists has taught me a lot!

2. A project on Wireless Sensor Networks
         I am doing a summer internship under my college professor where I will be working on a secure data aggregation and intrusion detection system for Wireless sensor networks. It is not sure as to which part of the project will I be working on right now. Currently I am learning programming for TinyOS using Nesc. It is a really interesting project. I get to read several research papers and will be learning a lot about encryption and cryptography. It will be real fun!
 
   I have further projects in the pipeline but I have no clue when I will be able to work on them. But for the next 2 months these will be my primary focus.

  

Friday, March 30, 2012

Directi internship questions

    Hello world! Sorry didn't publish anything since long, I am lazy and you know it.
        Anyways, due to our performance at ICPC Asia regionals at Amritapuri, I got an internship offer from Directi (big deal :) ). As a part of the interview procedure I had to give a phone interview totally based on Algos. I was asked following 2 questions:
    1. Given a number, find the smallest number larger than the given number and consists of the same
        digits as the given number.
        i.e if input = 123 then the answer would be 132
       
        Soln - Start from the last digit, move till u find a digit smaller than the last digit and then place the last      
                  digit in front of the found digit and then sort the digits present on it's right hand side.

     2. Given an array ,for all members x find a number y such that:
             y<x and pos(y) < pos(x) and pos(y) is closest to pos(x) [pos(x)=position of x in the array]
         i.e given 1,2,5,3,4 answer will be 2:1, 5:2, 3:2, 4:3

         Soln: Stack comes in handy over here.
                   Place the first number in stack.
                   Now for every member check the top of the stack. If it is smaller, then the number is your    
                   answer, or else, pop the top and again check.
                   Once, you find your answer, push the current number also in the stack.

  

Monday, March 5, 2012

Linux Cron Jobs

  I have always envied Iron man for Jarvis and all other cool toys that he has. I used to envy all those people who had some or the other type of automation in their life.
  Even I wanted some automation through my computer. First I had thought of making a twitter bot that would tweet the news of the day from Wikipedia. I had also a php script to scrap the necessary data . But, then I got bored of the idea and hence dropped it in middle (nothing new for me).
  I come to my room everyday at 12:20 from my college. So, I thought, how about getting my media player ready by the time I reach my room? (Cool Idea huh!)
  I already knew that I had to simply create a cron-job for this. The question was "how?". Well, what is Google for? A simple Google search told me that I just had to enter the command
     crontab -e
  It will ask you for your favorite editor. Select the one of your choice. It opens a user cron file. Now, for people who don't know what cron file is, well in simple words, you enter the time and command and then your computer runs that command on that time. The format of a cron file is:


   m h dom mon dow command
   m-minute
   h-hour
  dom-date of month
  mon-month
  dow- date of week
  command - command to be run

  you can enter * at any field (except command ofcourse!) to run that command every minute,hour etc.
  you can also input range in the form [1-5]
  also, you can give an interval like */2 i.e occur every 2 minutes etc.
  Enter the proper value in each field and enter the correct command. And then save the file. Thats it! You have now a command that will run automatically!
  One thing though, the output of the command will not be shown on the screen but will be mailed to you.
   Also, an important thing: If you are starting any GUI application you need to give command in this format:
   env DISPLAY=:0 command
   e.g env DISPLAY=:0 firefox

   Now, who is going to keep his/her morning alarm using a cron-job? :)


Saturday, February 18, 2012

Scraping Data: Getting only what you need

     It has happened several times that I have visited Wikipedia home page just to look into today's history. But, then there is so much of information, that you simply get distracted, don't you? So I decided how about, getting just what I needed? I had once stumbled upon a CS 50 video on Scraping data from internet, hence I had a basic idea as to what to do.
    But I googled a bit, coz I wanted to get something which was more recent and something that made my work very easy. Thankfully I stumbled upon, simplehtmldom parser . It made scraping of data so much easier. You don't even need to worry about completeness of the code! I just had to go through the HTML of the page and identify where the information was placed. Well, here is the code that I used to get my information.


<?php
  include_once('simple_html_dom.php');
  $html = file_get_html("http://en.wikipedia.org/wiki/Main_Page");
 echo $html->find('p', 4)->innertext; // Event happening today e.g Independence day
$ret['All'] = $html->find('ul', 5)->innertext; // Event happened in the past
echo $ret['All'];
   $html->clear();
   unset($html);
?>


I have used $ret['All'] just to show you that the output can be stored in another array.
      There is one fault in this code. If there is no event happening today, then the fourth 'p' tag would contain data of some other section of the page. Hence, in order to scrape the data in this manner, you need to hope that the page's html does not change. I have used only some of the features of the simplehtmldom parser, this manual will show you more applications of the library. Happy Hacking!
      If I get a chance I will make a twitter bot, that gets this info and tweets it everyday. Will need to setup some cron jobs for that and will need to learn several other things, but that will be done later.
      


  

Tuesday, February 7, 2012

TT (newer version of timetable)

  Last year I had created a shell script which showed me my academic timetable for the day. I was a newbie in shell scripting at that time and hence the script was not so good. Hence, here is tt or I should say timetable2.0 ?
  This time I have stored my timetable in a timetable.txt file so that whenever I need to modify the timetable, I don't need to modify the script. Also, the code is much shorter this time with not so many if statements. Here is the new code:

#!/bin/bash
days=( Monday Tuesday Wednesday Thursday Friday )

if [ "$#" == 0 ]
then
  echo -e "\nToday's Timetable:"
  set `date +%u`
else
  echo -e "\n"${days[$1-1]}"'s timetable:"
fi
  sed -n $1p < timetable.txt
  echo -e "\n\n"


  Still to be added: Feature to change the timetable. But that's all for now. The new modification will come when I feel the mood of it :)