Amazon

Saturday, February 2, 2008

Algorithm interview questions

[1] Find a linear (O(n) in time) algorithm which does in-place
riffle shuffle of an array of size 2n.

[2] Write a program to check for balanced brackets (and braces, and parentheses)
without using a stack.
"{[]()" false.
"{[]()}" true.
")))(((" false
"((()))" true
"{}([]())" true
"[(]){}" false
Use O(n) time and O(1) space.

Wednesday, January 30, 2008

A probabalility question

You have N balls with N different colors. Randomly you draw two at a time,
then painting the first ball to match the second. What is the expected
number of drawings before all balls are the same color?

Tuesday, January 15, 2008

Python tips and tricks

Q: How to add current path into system?
A: sys.path.append(os.path.abspath('./'))

Saturday, December 8, 2007

MATLAB Mex File for Const Time Median Filtering

Step1:
Download the C code from http://nomis80.org/ctmf.html
Step2:
Write the Mex function below, for example, with
name ctmf_mex.c
Step3:
In Matlab, run "mex ctmf_mex.c ctmf.c -output ctmf"

#include "ctmf.h"
/*
void ctmf(
const unsigned char* const src, unsigned char* const dst,
const int width, const int height,
const int src_step, const int dst_step,
const int r, const int cn, const long unsigned int memsize
);
*/
#include "mex.h"
#include "matrix.h"

void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{

unsigned char *I0, *I;
int height, width, r;
mwSize dims[2];
/* Check for proper number of arguments */
if (nrhs != 2) {
mexErrMsgTxt("Two input arguments required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
I0 = (unsigned char*)mxGetPr(prhs[0]);
/* Check the dimensions of Y. Y can be 4 X 1 or 1 X 4. */
height = mxGetM(prhs[0]);
width = mxGetN(prhs[0]);
r = (int)mxGetScalar(prhs[1]);

if (!mxIsClass(prhs[0],"uint8")) {
mexErrMsgTxt("I0 has be a matrix of type uint8.");
}
dims[0] = height;
dims[1] = width;
/* Create a matrix for the return argument */
plhs[0] = mxCreateNumericArray(2, dims, mxUINT8_CLASS, 0);
I = (unsigned char*)mxGetPr(plhs[0]);
ctmf(I0, I, height, width,
height, height,
r, 1, 512*1024
);
}

magic commands in iPython

from IPython.iplib import InteractiveShell
#magic function about
def magic_about(self,parameter_s=''):
"""Equivalent to ... """
print "hello world"
InteractiveShell.magic_about = magic_about
del magic_about


http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/ipython.html

http://ipython.scipy.org/moin/IpythonExtensionApi