Monday, December 29, 2014

Friday, December 26, 2014

Python Regex

Problem:
    To list all the files in a directory in format with: ama.xxxx or ama.xxxx.gz (x is digital) and print them in the chronological order.

import os
import re

def listdir_fullpath(d):
    return [os.path.join(d, f) for f in os.listdir(d)]

AMA_FILE_NAMING = ".*ama.\d{4}(.gz|)$"
amaFilePattern = dirName + "/" + AMA_FILE_NAMING
amaFiles = sorted( [f for f in listdir_fullpath(dirName) if re.match(AMA_FILE_NAMING, f)], key=os.path.getmtime)

for f in amaFiles:
    print f

@http://hilite.me/


For a simple match, glob can be used.

Wednesday, December 24, 2014

Find the Median - To be continued

The median is the "middle number" in a sorted list of numbers. If it's odd, then it is the middle value. If it's even, it's the average of the two middle values.

/* Problem:
 * How to find the median of a sorted array?
 * How to find the median of an unsorted array?
 * How to find the median of an unsorted array without sorting?
 *
 * How to find the median of two sorted array? - from LeetCode
 * How to find the median of given N sorted arrays?
 */




Reference:
    http://en.wikipedia.org/wiki/Median_of_medians

No Command Line Tools in Xcode 6.2 Beta 3?

I managed to install Xcode 6.2 Beta 3 in my OSX10.10. But I found the UNIX commands like g++/gcc/git/make... are still not available. Note that this is a completely new machine and I did not install any other version of Xcode before.

Usually the Command Line Tools are embedded within the Xcode IDE. There's no reason these commands are still missing since the Xcode has already been installed successfully. Finally I figured it out:

FOSs-MacBook-Pro:Applications fos$ ls -lrt |grep Xcode
drwxr-xr-x   3 fos   admin  102 Dec  5 08:21 Xcode-Beta.app

From the above output you can see that the Xcode 6.2 Beta are installed under /Applicatons/Xcode-Beta.app while the OSX is seeking these UNIX commands in Xcode.app by default. That's why it always prompt a window whenever I'm trying to run g++.

How to work around this? Well, a soft link can fix this problem:
$ ln -s Xcode-Beta.app Xcode.app

lrwxr-xr-x   1 fos   admin   14 Dec 24 23:14 Xcode.app -> Xcode-Beta.app

Note:
    The soft link Xcode.app must be deleted before installing the official release version of Xcode next time.