Tuesday, April 17, 2012

Solve Hannoi problem using recursion or iteration

Problem:
Solve the Tower of Hanoi problem first using recursion and then using iteration.

Analysis:
The Hanoi problem is a favourite example when teaching recursion. The idea is, to move dishes 1..N from source to target, we can first move dishes 1..(N-1) from source to interm, then move the dish N from source to target, and finally we move dishes 1..(N-1) from interm to target. Thus the recursion implementation is very straightforward. To implement using iteration is a little trickier, and can be done using a stack.

Code (recursion):

Code (iteration):

Color Balls

Non-Coding Problem:
Given a bag of n balls each with a different color. Each time you pick up a pair of balls and paint both to one of their colors. What's the expected number of steps before all balls become the same color?

Analysis:
This is a problem from the "Green Cover Book". It's easy to see that we have the following transition probability.
Thus if we let \(\mu_i\) be the expected number of steps getting to one color when the bag has i distinct colors, then we have \[ \mu_i = 1 + \frac{i(i-1)}{n(n-1)} \mu_{i-1}+\left(1-\frac{i(i-1)}{n(n-1)}\right) \mu_i. \] From this we can use induction to prove that \[ \mu_i = \frac{i-1}{i} n(n-1). \] Therefore we have \[ \mu_n = (n-1)^2. \]

Thursday, April 5, 2012

How to toggle on/off touchpad in Ubuntu

Somehow touchpad inteferes a lot with typing in Ubuntu. After some experimenting, I found the following script can reliably toggle on/off the touchpad:

For thinkpad X220, we can use Fn+F8 to toggle on/off the touchpad.

I found that the best experience so far can be achieved by disabling touchpad and using keyboard+USB mouse instead.

Saturday, March 3, 2012

Install gettc on macbook

gettc is a very convenient tool for downloading TopCoder problems (description, test cases etc.) and practice offline. This post summarizes how I installed gettc on my macbook air.
  1. Mac OS X Lion ships with ruby 1.8.7, and gettc requires ruby>=1.9. So our first step is to install a newer ruby
    1. Install rvm, and use it to install new ruby. Follow the steps here
    2. Set the new ruby as default. Follow the steps here
  2. Next install prerequisites of gettc.
  3. Next install gettc. Follow the instructions here. After it's done, change the first line (location of ruby) in `which gettc`. The location of ruby can be found by `which ruby`.
  4. Set up the PATH by adding the following line to ~/.bash_profile:
  5. Modify "~/.gettc/template/solve/cpp/Makefile" to remove the option "-std=gnu++0x". Modify "~/.gettc/template/bin/runner.sh" to remove the option "-o $ostream".


Remarks:
If your machine is Ubuntu or other unix variants, and you don't have root privilege, you can use the above steps as well.

Friday, March 2, 2012

Getting used to Mac from a Windows/Linux background

Here are some tips for getting used to Mac from a Windows/Linux background. The following tips apply for the Mac OS X Lion on a Macbook Air.
  • Right click: use "control"+click instead.
  • Cut/Copy/Paste/Find: use "command"+X/C/V/F instead.
  • Home/End button
    • "control"+A for Home, and "control"+E for End works in both browsers and Terminal.
    • "fn"+Shift+left/right works in Terminal but not in browsers.
    • "command"+left/right works in browsers but not in Terminal.
    • In the Terminal, if you are using vim, you can use "^" for Home and "$" for End.
  • Select two files and all the files in between (multiple file selection): first switch from icon view to list view, and then do it in the Windows way, i.e. select the first file, press and hold "Shift" and select the last file.
  • Install the command-line-only gcc C/C++ compiler: download "Command Line Tools for Xcode" from Apple Developer Site, which is only 170M (or 280M after install).
  • Install latex: I used the basic version of MacTex and TeXShop.
    • To find a missing package, you can use ctan search.
    • To download the missing package, you can do "sudo tlmgr install [package name]".
    • To install a package manually, you can extract it into /usr/local/texlive/2011basic/texmf-local/ and then run "sudo texhash".
    • To do inverse search in TeXShop, you can use "command"+click on the pdf viewer.

Monday, February 20, 2012

How to set default screen brightness in Ubuntu 11.10

For some unknown reason, the setting in "System Settings->Screen->Brightness" is not saved in Ubuntu 11.10 of my HP laptop, and I had to adjust it manually each time when my computer starts. This has been really frustrating. Today I've finally googled a solution that works perfectly. The solution can be found here.

Thursday, February 16, 2012

How to run Mathematica in batch mode

  1. Let's say your code is saved in myscript.nb. First you have to convert it to plain text by copy and paste from Mathematica. Assume the plain text version is saved in myscript.m.
  2. Next, run the script in the terminal as follows:
    math -noprompt -script myscript.m
    

Visitors