google code prettify

2016年9月30日 星期五

用docker啟動redis服務


1、先至docker hub上找到redis頁面
https://hub.docker.com/_/redis/

2、依網頁上的指令pull redis
開啟Terminal,輸入「docker pull redis」










3、找到image id
輸入「docker images」















4、啟動redis
輸入「docker run --name redis -d -p 6379:6379 ${imageID}」
指令中的${imageID}記得要換成自己的image id,像圖片中的redis image id是1aa84b1b434e
所以我本機輸入的指令為「docker run --name redis -d -p 6379:6379 1aa84b1b434e」





Reference:
https://hub.docker.com/_/redis/
http://stackoverflow.com/questions/31889163/cannot-connect-to-redis-running-as-container-with-boot2docker

Mac安裝docker-compose

1、安裝setuptools
輸入「brew install python」




2、透過easy_install安裝pip
輸入「sudo easy_install pip」



3、透過pip安裝docker-compose
輸入「pip install docker-compose」



4、驗証docker-compose是否安裝成功
輸入「docker-compose」






















Reference:
http://docs.python-guide.org/en/latest/starting/install/osx/
https://docs.docker.com/compose/install/

2016年7月16日 星期六

LeetCode - 371. Sum of Two Integers

Question:

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.

Solution:


public class Solution {
    public int getSum(int a, int b) {
        return a+b;
    }
}

LeetCode - 374. Guess Number Higher or Lower

Question:

We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num) which returns 3 possible results (-11, or 0):

Solution:


public class Solution extends GuessGame {
    
    public int guessNumber(int n) {
        Long R = new Long(n);
  Long L = 1L;
   
  if(R!=L){
   while(R>=L){
    Long mid = ((L+R)>>1);

    int result = guess(mid.intValue());
    //System.out.println("mid:" + mid + " ,result:" + result);
    if(result ==1){
     L = mid+1;
     
    }else if(result ==-1){
     R = mid-1;
    }else{
        return mid.intValue();
    }
   }
  }else{
      return R.intValue();
  }
  
  
  return L.intValue();
  
 }
}

LeetCode - 20. Valid Parentheses

Question:

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Solution:


public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
  
  char[] array = s.toCharArray();
  if (array.length % 2 != 0) {
   return false;
  }
  
  for(int i =0;i<array.length;i++){
   char c = array[i];
   if(c=='(' || c=='[' || c=='{'){
    stack.push(c);
    continue;
   }
   
   if(stack.isEmpty()==true){
    return false;
   }
   
   if(c==')'){
    if (stack.pop()!='(')
     return false;
   }else if(c==']'){
    if(stack.pop()!='[')
     return false;
   }else if(c=='}'){
    if(stack.pop()!='{')
     return false;
   }else{
    return false;
   }
  }
  
  if(stack.isEmpty()==false){
   return false;
  }else{
   return true;
  }
    }
}

2016年7月2日 星期六

在Mac上安裝Docker

關於Docker就不多說了,Docker現在出了Docker for Mac Beta
因此就不用再使用Docker Toolbox了
下載位置:https://docs.docker.com/docker-for-mac/

1、安裝非常簡單,從左邊拉到右邊就好了


2、安裝後執行,就可以在右上角看到執行中的圖示


















3、就開始開終端機開始使用docker了,用「docker --version」確認版本資訊






4、安裝nginx,輸入指令「docker run -d -p 80:80 --name webserver nginx」



5、打開瀏覽器輸入「http://localhost/」就可以看到已經建立nginx服務了












Reference:
https://docs.docker.com/docker-for-mac/

2016年3月19日 星期六

運用Jsoup取得網頁資訊

如果要使用Java去parse Web page, 可以使用Jsoup,簡單好用
以下範例為使用jsoup抓取yahoo 首頁的新聞標題



package test.charles.JsoupExample;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class App 
{
    public static void main( String[] args ) throws IOException
    {
     //read yahoo home page
     String url  = "https://tw.yahoo.com/";
     Document doc = Jsoup.connect(url).get();
     
     //get first news tab one
     Element t1 = doc.getElementById("t1");
     
     //get news title
     Elements newsTitle = t1.select("a[href] > span");
     
     //print size
        System.out.println("size:" + newsTitle.size());
        
        //print news title
        for(Element e:newsTitle){
         System.out.println("title:" + e.text());
        }
        
    }
}




程式碼可以至github下載