Pages

Monday, October 24, 2011

Hide right side ads in Gmail


A simple chrome extension to hide the ads showed in the right side of emails. The ads will still be there (someone will still search through emails to generate suitable ads), but will be hidden using CSS.
Nice tutorials on building extension for chrome at [5] and [4].
The source code and packed .crx extension are uploaded to google code.
Compilations of references (thanks):

  1.   Getting started
  2.   Content scripts
  3.   Extensions FAQ
  4.   Extensions dev guide
  5.   Creating a chrome extension tutorial
  6.   IconFinder 

Sunday, October 16, 2011

XML Formatter

Many times I've searched for tools/plugins/libraries to format/indent/... small XML files. A tool to do that can be easily done with classes from System.Xml namespace
try{      
 // Format XML file
 XmlDocument^ doc = gcnew XmlDocument;
 
 char tmpFileName[MAX_PATH] = "";
 GetTempFileName(".", "bak_1", 0, tmpFileName);
 
 // convert unmanaged -> managed
 String^ srcFile = gcnew String(szFileName);
 String^ tmpFile = gcnew String(tmpFileName); 
 
 File::Copy(srcFile, tmpFile, true);
 try{
  doc->Load(tmpFile);
 }
 catch(XmlException^ e1) {
  //convert Managed -> unmanaged
  TCHAR* errMsg = (TCHAR*)(void*)Marshal::StringToHGlobalAnsi(e1->Message);
  MessageBox(NULL, errMsg, "Error", 0);
  return 0;
 }

 XmlWriterSettings^ xws = gcnew XmlWriterSettings;     
 xws->Indent = true;
 xws->CheckCharacters = false;

 XmlWriter^ writer = XmlWriter::Create(srcFile, xws);      
 doc->Save(writer);
 writer->Close();

 File::Delete(tmpFile);
}
catch(Exception ^e) {
 MessageBox(NULL, szFileName, "Exception while converting following files:", 0);
}
MessageBox(NULL, "Converted", "ok", 0);


How nice would be to build your own small tools and adapt them when you need to.   A (very) small step towards being a self sustainable programmer is this :)
http://code.google.com/p/cool-xml/

Wednesday, October 12, 2011

Easy reviewing with Anki

Anki is a program that makes remembering things easier.
Here's  ~300 questions,  put in anki flash cards, extracted from great ceh exam prep guide.
...would have been very useful in reviewing the material.

Wednesday, June 8, 2011

Number of paths in square grid (Project Euler problem 15)

Description of the problem (from here ):
Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20×20 grid?


A: We can observe that:
  •  every path has exactly 2*n moves, every move being either right or down (backtracking isn't allowed)
  • in every 2*n path there are n moves down and n moves right. 
So, the number of all the distinct possibilities is the number of ways we can arrange n right moves from 2*n positions:

Tuesday, June 7, 2011

Number of digits in Fibonacci term (Project Euler problem 25)

Q: The question from the problem 25  is to find the first term in the Fibonacci sequence to contain 1000 digits. 


A: The answer comes from wiki


Since Fn is asymptotic to \varphi^n/\sqrt5, the number of digits in F_n\, is asymptotic to n\,\log_{10}\varphi\approx0.2090\,n. As a consequence, for every integer d > 1 there are either 4 or 5 Fibonacci numbers with d decimal digits.


So, 1000/0.2090 is approximately  4784, and the first Fibonacci term with 1000 digits is the 4782th term.

Monday, June 6, 2011

Maximal sum in a triangle (Project Euler problem 67)

The problem description is here. For a given triangle you have to find the path with the maximum path. Given an input triangle :
3
7 4
2 4 6
9 5 9 3



We can compute the maximum sum path using 2 approaches:

Solution 1 : bottom-up. Build maximum path from bottom.  From the next-to-last row, up to the first, add the maximum corresponding sub-triangle. So the initial triangle becomes iteratively:


3
7    4
11 13 15
9    5    9  3


3
20 19
11 13 15
9    5    9  3


23
20 19
11 13 15
9    5    9  3

So the maximum sum is 23, and the maximum path 
3
7 4
4 6
9 5 9 3

Solution 2: same result is obtained with a top-down approach. Starting from the second row, compute the maximum-sum path to every element. So the initial triangle becomes iteratively:
3
10  7
2 4 6
9 5 9 3

3
10  7
12 14 13
9 5 9 3

3
10  7
12 14 13
21 19 23 16

We get same result for the maximum sum.

Simple python Implementation for the bottom-up solution:
def get_numbers():
    #lines = open("tri_small.txt", "r")    
    lines = open("tri.txt", "r")    
    
    listOfRows = []
    
    for line in lines:
        currRow = [int(x) for x in line.split()]
        listOfRows.append(currRow)
    
    return listOfRows
    
def solve():
    row_list = get_numbers()
    length = len(row_list)
    
    for i in xrange(length-2, -1, -1) :
        len_row = len(row_list[i])                   
        for j in range(0, len_row) :
            row_list[i][j] += max(row_list[i+1][j], row_list[i+1][j+1])
        
    print row_list[0]
    return 0

Saturday, April 30, 2011

How to create an offline Ubuntu repository

Debmirror program  creates a local mirror of a Deban repository. A quick way to build an offline repository, save it to to an USB drive, and use it on a computer without internet access:

  1. Install the application on a computer with internet:
    $ sudo apt-get install debmirror
    
  2. Get the key for the repository and add it to the keyring. This can be done be importing it from a debian keyring:
    $ gpg --no-default-keyring --keyring /home/USER/.gnupg/trustedkeys.gpg --import /usr/share/keyrings/ubuntu-archive-keyring.gpg
    
  3. Download the repository (approximate sizes for different repositories can be found in the man page):
    $ sudo debmirror -a i386 --no-source -s main -h ro.archive.ubuntu.com -d natty,natty-updates,natty-security -r /ubuntu --progress -e http mirror
    
    This mirrors section main of Ubuntu 11.04 Natty, updates and security  All options are explained in the man page.
  4. Mount usb device containing the repository to the other computer:
    $ mount /dev/sdb1 /mnt/usb
    
  5. Create a backup copy of sources.list file and modify it:
    $ sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
    $ sudo vim /etc/apt/sources.list
    
    Delete all content and add just:
    deb file:///mnt/usb/mirror natty main
    deb file:///mnt/usb/mirror natty-updates main
    deb file:///mnt/usb/mirror natty-security main
    
  6. Retrieve the new list of packages from the offline repository:
    $ sudo apt-get update
    
  7. Upgrade the repository:
    $ sudo apt-get upgrade
    
  8. Start installing packages....