Saturday, May 25, 2013

Sublime Configuring CodeIntel Auto Completion

Unarguably Sublime is one of the best text editors and with its ability to extend with python scripts, gifts itself a lot of power. Here lets see one of those extensions which allow code completion. Normally, sublime provides code completion for the words in the current file being edited. Codeintel allows the user to get code completion feature of the respective languages.

Installation

Lets install CodeIntel, through Package Control plugin. To install Package Control plugin, do the following steps. Here is the author's page. Quoting from the source website

Installation is through the Sublime Text 2 console. This is accessed via the ctrl+` shortcut. Once open, paste the following command into the console.

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print('Please restart Sublime Text to finish installation')

After installing it, Press Shift+Ctrl+P or click on Tools->Command Palette. In the window that opens up, type Install Package. In the text box which you see after that, type SublimeCodeIntel and click on it to install it. You are done with the installation of CodeIntel.

Configuration

The following information is taken from the README.rst file. Open ~/.codeintel/config file in your favourite text editor, most likely Sublime. Pick your language from the list below and copy paste only the blocks for the languages of your choice.

    {
        "PHP": {
            "php": '/usr/bin/php',
            "phpExtraPaths": [],
            "phpConfigFile": 'php.ini'
        },
        "JavaScript": {
            "javascriptExtraPaths": []
        },
        "Perl": {
            "perl": "/usr/bin/perl",
            "perlExtraPaths": []
        },
        "Ruby": {
            "ruby": "/usr/bin/ruby",
            "rubyExtraPaths": []
        },
        "Python": {
            "python": '/usr/bin/python',
            "pythonExtraPaths": []
        },
        "Python3": {
            "python": '/usr/bin/python3',
            "pythonExtraPaths": []
        }
    }

For example, I like to use python and my config file looks like this
{
 "Python": {
     "python": '/usr/bin/python',
     "pythonExtraPaths": []
 },
}

Thats it. Restart your sublime, type code and press Alt + / to see the Auto completion at work.

Sunday, May 19, 2013

Ubuntu scaling the display (Zooming)


Today I came across this useful trick, which allows me to have lot of space on my screen, even though my screen supports only very low resolution.

First, lets see how to get to know the list of displays attached to Ubuntu

~$ xrandr
Screen 0: minimum 8 x 8, current 2881 x 1280, maximum 16384 x 16384
VGA-0 connected 1600x1280+1281+0 (normal left inverted right x axis y axis) 376mm x 301mm
   1280x1024      60.0*+   75.0  
   1152x864       75.0  
   1024x768       75.0     60.0  
   800x600        75.0     60.3  
   640x480        75.0     59.9  
LVDS-0 connected (normal left inverted right x axis y axis)
   1600x900       60.0 +   40.0  
DP-0 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 376mm x 301mm
   1280x1024      60.0*+   75.0  
   1152x864       75.0  
   1024x768       75.0     60.0  
   800x600        75.0     60.3  
   640x480        75.0     59.9  
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-0 disconnected (normal left inverted right x axis y axis)
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 disconnected (normal left inverted right x axis y axis)
~$ 

This is what I got on my machine. And following are the display names as per Ubuntu.
VGA-0 
LVDS-0
DP-0 

This command has listed out other useful information as well. I could only understand the resolutions supported. Following are the resolutions supported by VGA-0 and DP-0.
1280x1024
1152x864
1024x768
 800x600
 640x480
So the maximum resolution allowed is 1280x1024. But that doesnot allow me to have more space on screen. After long searching, I got this.
xrandr --output VGA-0 --mode 1280x1024 --scale 1.25x1.25
This command sets the resolution of VGA-0, to 1280x1024 but it scales it by the factor of 1.25x1.25. Voila :) Now I have much bigger screen.


Wednesday, May 8, 2013

Google Code Jam 2013 - Round 1B - Osmos

Today, I would like to share my solution to the Google Code Jam 2013's Round 1B's Osmos problem. These are the statistics for that problem
Small Input : 4668/7250 users correct (64%)
Large Input : 3537/4578 users correct (77%)
I believe it was tricky enough for an easy problem. Many people have got atleast one wrong try. Here is how I solved it (I couldn't solve it during the contest).

# include <iostream>
# include <cstdio>
# include <algorithm>
 
using namespace std;
int Sizes[101];
int Total, A, N, Temp, Counter;
int getDiff (int A, int B, int & C)
{
    while (A <= B)
    {
        C++;
        A += (A-1);
    }
    return A;
}
int Solve (int idx, int CurrentScore, int Changes)
{
    if (idx == N) return Changes;
    if (CurrentScore == 1)
        return Solve (idx + 1, CurrentScore, Changes + 1);
    else if (Sizes[idx] < CurrentScore)
        return Solve (idx + 1, CurrentScore + Sizes[idx], Changes);
    else
    {
        int tChanges = 0;
        int Diff = getDiff (CurrentScore, Sizes[idx], tChanges) + Sizes[idx];
        int Min1 = Solve (idx + 1, Diff, Changes + tChanges);
        if (Sizes[idx] > CurrentScore)
           return min (Solve (idx + 1, CurrentScore, Changes+1), Min1);
        else return Min1;
    }
}
int main()
{
    //freopen ("Input.txt", "r", stdin);
    //freopen ("Scratch.txt", "w", stdout);
    scanf ("%d", &Total);
    for (int i = 1; i <= Total; i++)
    {
        scanf ("%d%d", &A, &N);
        Temp = A;
        Counter = 0;
        for (int j = 0; j < N; j++) scanf ("%d", &Sizes[j]);
        sort (Sizes, Sizes + N);
        printf ("Case #%d: %d\n", i, Solve (0, A, 0));
    }
}
Here is the ideone URL http://ideone.com/g5qxYd where I tested the program with Google's inputs.

Saturday, May 4, 2013

Fixing Ubuntu apt-get sources

This script makes use of the mirror protocol.
#!/bin/bash

UbuntuCodeName=`lsb_release -c | cut -f 2`
if [ ! -f /etc/apt/sources.list.Original ];
then
    mv /etc/apt/sources.list /etc/apt/sources.list.Original
 echo "deb     mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName main restricted universe multiverse

deb     mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-updates main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-updates main restricted universe multiverse

deb     mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-backports main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-backports main restricted universe multiverse

deb     http://security.ubuntu.com/ubuntu $UbuntuCodeName-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu $UbuntuCodeName-security main restricted universe multiverse

deb     http://archive.canonical.com/ubuntu $UbuntuCodeName partner
deb-src http://archive.canonical.com/ubuntu $UbuntuCodeName partner

deb     http://extras.ubuntu.com/ubuntu $UbuntuCodeName main
deb-src http://extras.ubuntu.com/ubuntu $UbuntuCodeName main" >> /etc/apt/sources.list
else
 echo "This script has been run already. Exiting"
fi

Alternatively, you can run the following commands to update the sources automatically
wget -O UpdateAptSources.sh http://ideone.com/plain/UgsBel
chmod 555 UpdateAptSources.sh
sudo ./UpdateAptSources.sh

Python equivalent of C's freopen

All I wanted to do is, to make python treat a file as stdin and another file as stdout. This is possible in C and C++ like this

freopen ("Input.txt", "r", stdin);
freopen ("Output.txt","w", stdout);

Including these two lines at the beginning of main function, makes sure that all the read calls read from Input.txt and all the write calls write to Output.txt. We don't have to use fprintf and fscanf to read and write to a file anymore. printf and scanf itself are enough. This would be very useful, if we want to automate an interactive program. Almost all of Sport Programming solutions have these two lines. Because, I don't like to type the input each and every time. So I store it once in a file and then write the results to another file.

Now, I wanted to do the same thing in Python, for Google Code Jam 2013. So started looking for way and I got this. I saw Greg Hewgill's answer which is like this

import sys
# parse command line
if file_name_given:
    inf = open(file_name_given)
else:
    inf = sys.stdin

This assigns stdin or a file to the variable inf. But then, I wanted to use standard library functions of Python like raw_input and input as well. So, I managed to do this

import sys

sys.stdin  = open("Input.txt")
sys.stdout = open("Output.txt")

That's it. I just replaced the stdin and stdout with two different files. Now I am even able to use raw_input and input functions, without any trouble. Hope this helps... :)



Thursday, May 2, 2013

Ubuntu Nvidia Driver Installation

Following are the steps to install Ubuntu Nvidia drivers and activating it.

sudo apt-get install linux-headers-$(uname -r)
sudo apt-get install nvidia-310
sudo apt-get install nvidia-313-updates
sudo apt-get install nvidia-settings
Once the instllation of the above mentioned packages are successful, run
sudo nvidia-xconfig
This will create the /etc/X11/xorg.conf file

Now, do
sudo software-properties-gtk

Which will open up a window like this


Select the latest driver name from the list and then do

sudo reboot

That's it :)