Installing extensions for Mercurial

Detailed instructions are here.
Create forlder ~/.hgextensions
Copy extesion files to this folder.

Open ~/.hgrc Mercurial’s config file and add [extensions] section. It might look something like:

[extensions]
kilnauth=~/.hgextensions/kiln_extensions/kilnauth.py

At the moment of this post Kiln did not support SSH connection. Kilnauth extension stores cookies and keeps you authenticated so that you don’t have to to type username and password again and again when connecting via https.

Advertisements
Posted in Mercurial, Productivity, Programming | Leave a comment

Running unit tests in Python

Install nose:
sudo easy_install nose
or:
sudo pip install nose

By default nose finds all directories and files that match the following regular expression:
(?:^|[b_./-])[Tt]est [NOSE_TESTMATCH]

I usually create a file test_something.py and place my unit tests there as regular functions, e.g.:
def test_function_1():
...

To run tests in specific file do:
nosetests test_file.py

To run all tests in files satisfying regex above:
nosetests

By default, nose will capture all output sent to stdout. To be able to see print statements inside test functions use –nocapture option.

I usually run all tests by:
nosetests --nocapture -v

or:
nosetests --nocapture -v -w path_to_folder

Posted in Productivity, Programming, Python, UNIX/Linux | Leave a comment

Great unix one liner to join two tables keys in the first columns

Suppose file column_ids.csv contains the first column with some IDs. You have a table big_table.csv with the first column also containing IDs. We can join the two files in unix shell:

join -t "," <(sort file_1.csv)  <(sort file_2.csv) > result.csv

-t option specifies a delimiter.
Both files must be sorted in the same order on the key value.

Posted in Bash, Productivity, Programming, UNIX/Linux | Leave a comment

Add a new host to known_hosts

This is from serverfault:
ssh-keygen -R [hostname]
ssh-keygen -R [ip_address]
ssh-keygen -R [hostname],[ip_address]
ssh-keyscan -H [hostname],[ip_address] >> ~/.ssh/known_hosts
ssh-keyscan -H [ip_address] >> ~/.ssh/known_hosts
ssh-keyscan -H [hostname] >> ~/.ssh/known_hosts

Posted in EC2, Programming, Uncategorized, UNIX/Linux | Leave a comment

Python things

How to remove a package using easy_install?
sudo easy_install -m [package_name]

How to start ipython notebook with interactive pylab capabilities?
ipython notebook --pylab inline

Posted in Programming, Python, Uncategorized, UNIX/Linux, visualization | Leave a comment

Interview brainteaser asked at Google

Problem.
You are in the room with n computers. Some of them are good, and some of them are bad. You can query any computer about the status of any computer in the room. Good computers will always tell you the truth. Bad computers can answer anything. You also know that there are more good computers in the room than bad ones. How will you find a good computer in the room, and how many queries will you need?

Solution.
The idea is to construct a sequence of queries that produce the sequence of answers of the form “good -> good -> good … -> good”. We should be able to do so because we know that there are more good computers in the room than bad ones. After we queried all computers in the room and are left with this chain, we know that there is some good computer in the chain. Therefore, the last computer will be good. To be concrete, we can go as follows:
Pick any computer and query it about the status of some other computer. If it answers “good” – add it to the back of the chain; if it answers “bad” – discard both and don’t query them any more. Pick another computer and query it about the status of the last computer in the chain (if the chain is empty pick any computer that has not been discarded before). If it answers “good” – add it to the back of the chain; if it answers “bad” – discard it along with the last computer in the chain (so that chain shrinks by one in this case). Keep doing it. Your chain is guaranteed to be non-empty after all computers are queried because you always discard as many bad computers as good ones.
The number of queries required is n – 1.

Posted in Interview, Math | Tagged , | Leave a comment

Popular computer science interview question: thieves locking treasure in cryptographic way :)

This problem is quite common in interviews with people coming from computer science background. The problem is from some famous paper on cryptography.
Problem.
7 thieves stole treasure. They would like to place N locks on it. Each lock can have several keys that can open it; a single key can open only one lock. Thieves would like to achieve the following 2 goals:
1) No 3 thieves should be able to open all locks
2) Any 4 of them should be able to open all of the locks
What is the minimum number of locks and the minimum number of keys that will allow designing such a locking system?

Solution.
To each lock there corresponds no more than one triple of thieves that can not open it. This is true because if there are more than one such triples, then we can group them and get a group of at least 4 thieves that can not open that lock. This will violate the second requirement. Therefore, the number of locks must be \ge7 \choose 3 with the minimum at 7 \choose 3 =35 locks. Now focus on any lock and the corresponding triple of thieves that can not open it. Each of the remaining 4 thieves must have a key to that lock in order for the second requirement to hold. Therefore the minimum number of keys is 4 7 \choose 3 = 140.

Posted in Interview, Math | Tagged , , | Leave a comment