2013年10月14日 星期一

[FWD] TimeUnit


http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html



A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units.



這是很不錯的設計。

有時候寫程式常常忘記用 second 為單位,或者是用 millisecond 為單位。

何不讓 code 變成 oral language?

2013年10月11日 星期五

[FWD] Bloom filters

WIKI: http://en.wikipedia.org/wiki/Bloom_filter

Tutorial: http://billmill.org/bloomfilter-tutorial/

實作細節不外乎 bloom filter 變形以及 hash functions 的選擇。



為什麼會講到 bloom filters?

因為雲端病毒碼就是利用這個技術減少 signatures 的大小。

如果能夠穿過許多道 bloom filters 引擎才上網把完整的 signatures 抓下來。

2013年10月10日 星期四

解釋過後好像也沒有比較好


回頭看傻事好有感覺。

那是歷史痕跡不容扭曲。



早已經雲淡風輕。

骨頭裂掉再黏回去就會凸出一塊,轉轉骨頭喀喀啦啦響,那就是痕跡。

以前機車代步,現在公車、捷運、計程車是我的三寶,

每星期騎機車只是為了讓車可以發動。



我也不好意思說有的沒的,倒不是念舊,而是不想讓自己那麼仇恨。

《半生緣》兩人歷盡千帆後再相遇,卻惟有感慨萬千:「世鈞,我們回不過去了。」

這結局才是最美麗的!



至於最近發生的事,暫且不提,反正也是回不過去了。

至於現在發生的事,偷偷告訴大家,完全是個沒進展。

2013年10月9日 星期三

ProjectEuler 185 -- TLE on backtracking


Backtracking 只是比較聰明的暴力法,

所以 time complexity 大約還是 10^16。

import sys

DIGIT_COUNT = 5

#DIGIT_COUNT = 15

ANSWER = [0] * DIGIT_COUNT

GUESSES = { 
    "90342" : 2, 
    "70794" : 0, 
    "39458" : 2, 
    "34109" : 1, 
    "51545" : 2, 
    "12531" : 1,
}

#GUESSES = {
#    "5616185650518293" : 2,
#    "3847439647293047" : 1,
#    "5855462940810587" : 3,
#    "9742855507068353" : 3,
#    "4296849643607543" : 3,
#    "3174248439465858" : 1,
#    "4513559094146117" : 2,
#    "7890971548908067" : 3,
#    "8157356344118483" : 1,
#    "2615250744386899" : 2,
#    "8690095851526254" : 3,
#    "6375711915077050" : 1,
#    "6913859173121360" : 1,
#    "6442889055042768" : 2,
#    "2321386104303845" : 0,
#    "2326509471271448" : 2,
#    "5251583379644322" : 2,
#    "1748270476758276" : 3,
#    "4895722652190306" : 1,
#    "3041631117224635" : 3,
#    "1841236454324589" : 3,
#    "2659862637316867" : 2,
#}

def is_unique(digit, position):
    has_more = False

def check_guessing(dimension):
    for guess in GUESSES:
        correct_count = 0
        for i in range(dimension):
            if ANSWER[i] == int(guess[i]):
                correct_count += 1
            if correct_count > GUESSES[guess]:
                return False
        if correct_count + DIGIT_COUNT - dimension < GUESSES[guess]:
            return False
    return True

def backtracking(dimension):
    if not check_guessing(dimension):
        return
    if dimension == DIGIT_COUNT:
        print('Found =>', ANSWER[0:dimension])
        return
    for i in range(10):
        ANSWER[dimension] = i
        backtracking(dimension + 1)    
    
def solve():
    for i in range(10):
        ANSWER[0] = i
        backtracking(1)
    
def main():
    solve()
    
if __name__ == '__main__':
    sys.exit(main())

2013年10月6日 星期日

[Java] insertion sort & merge sort


要熟悉某個語言,最簡單的方式就是寫一次基本的演算法。

此外,將來要面試科技公司,基本演算法更要知道怎麼寫,

怎麼用原子筆寫,這很重要。



以某科技公司為例,主管當然知道考古題的存在

主管會認為題目已經外洩沒理由考不好,但偏偏就是有面試者考不好,

而且這間公司好的標準異常寬鬆,100分拿35分就可以了



有時候誠意真的很重要。

如果我是主管的話,正妹直接錄取 ((逃))((當作在選後宮))



正題:

Insertion sort,對於小的 array 可以用這個實做

merge sort,divide and conquer 基本應用



public class Sorting {
public static void insertionSort(int[] array) {
for (int j = 1; j < array.length; j++) {
int key = array[j];
int i = j - 1;
while (i >= 0 && array[i] > key) {
array[i+1] = array[i];
i--;
}
array[i + 1] = key;
}
}

public static void mergeSort(int[] array) {
mergeSort(array, 0, array.length - 1);
}

private static void mergeSort(int[] array, int p, int r) {
if (p >= r) {
return;
}
int q = (p + r)/2;
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}

private static void merge(int[] array, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;

int[] left = new int[n1 + 1];
int[] right = new int[n2 + 1];

for (int k = 0; k < n1; k++) {
left[k] = array[p + k];
}
for (int k = 0; k < n2; k++) {
right[k] = array[q + 1 + k];
}
left[n1] = Integer.MAX_VALUE;
right[n2] = Integer.MAX_VALUE;

int i = 0;
int j = 0;
for (int k = p; k <= r; k++) {
if (left[i] <= right[j]) {
array[k] = left[i];
i++;
}
else {
array[k] = right[j];
j++;
}
}
}
}

2013年10月1日 星期二

[Programming] TIPS TO BE COMPETITIVE

Question 1

There are n webpages (1 ≤ n ≤ 10M). Each webpage i has different page rank r_i. You want to pick top 10 pages with highest page ranks. Which method is more feasible?
(a) Load all n webpages’ page rank to memory, sort, and pick top 10.
(b) Use priority queue data structure (heap).

Question 2

Given a list L of up to 10K integers, you want to frequently ask the value of sum(i, j), i.e. the sum of L[i] + L[i+1] + ... + L[j]. Which data structure should you use?
(a) Simple Array.
(b) Balanced Binary Search Tree.
(c) Hash Table.
(d) Segment Tree.
(e) Suffix Tree.
(f) Simple Array that is pre-processed with Dynamic Programming.

Question 3

You have to compute the ‘shortest path’ between two vertices on a weighted Directed Acyclic Graph (DAG) with |V |, |E| ≤ 100K. Which algorithm(s) can be used?
(a) Dynamic Programming + Topological Sort.
(b) Breadth First Search.
(c) Dijkstra’s.
(d) Bellman Ford’s.
(e) Floyd Warshall’s.

Question 4

Which algorithm is faster (based on its time complexity) for producing a list of the first 10K prime numbers?
(a) Sieve of Eratosthenes.
(b) For each number i ∈ [1 − 10K], test if i is a prime with prime testing function.