2015年9月29日 星期二

npm install 筆記

這是我用google 在別人的FB裡看到的教學,為了避免哪天不見,我還是整理一份好了


* package.json 是專案設定檔,可以用來設定 dependency (會用到哪些 npm ,分成專案用跟開發用兩種 dependency / dev-dependency)。
有設定的話剛 clone 別人專案回來時,可以用 "npm install" 直接安裝專案相依性到 local 。好處是不用把 node_modules commit 進版本控制,降低大小跟環境依賴。
* npm init  //初始化並產生 package.json
* npm install <package> //正常的安裝套件流程
如果沒有指定 package 的話,會自動安裝 package.json 裡面的相依性.
但如果你是裝專案需要的 npm package 的話,可以加上 "--save" ,如 npm install <package> --save ,會順便更新 package.json 檔加入這個 dependency。
如果你是給 gulp 或其他開發工具用的,可以用 --save-dev ,會寫入 dev-dependency 區。
另外跟這個沒有直接關係,但值得知道的事情是 "-g" 屬性,有支援 npm install -g <package> 的 npm ,會把該 package 直接變成 global 的 command。

 在 package.json 裡面有 scripts properties 。可以在裡面直接寫入你要執行的 "command line"(!!) 指令
ex:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "supervisor --extensions 'node,js,jsx' app.js"
},
就可以透過 npm run dev 來執行「supervisor --extensions 'node,js,jsx' app.js」

2015年9月27日 星期日

用兩百行 python實現換臉 - 環境建制

為了完整執行 如何用200行Python代码“换脸”  這篇文章裡的程式碼 必須照下列步驟建制環境

  dlib:
          1. download dlib
          2. unzip dlib 
          3. compile dlib 參照這篇官網裡的教學
              3.1 cd /path/to/your/dlib
              cd examples
              mkdir build
              cd build
              cmake ..
              cmake --build . --config Release
          3.2 cd /path/to/your/dlib
              cd python_examples
              ./compile_dlib_python_module.bat 
              (如果缺東西)
                       ubuntu  sudo apt-get install libboost-python-dev cmake 
                       arch    sudo pacman -S boost  
              Copy dlib.so to /usr/local/lib/python-2.7/site-packages.
      4. install opencv
         arch pacman -S opencv
 

2013年7月21日 星期日

[UVA]186 - Trip Routing

#include<iostream>
#include <map>
#include<climits>
using namespace std;
class des{
    public:
        des(){
            value=INT_MAX;
            fromID=-1;
            fConnect=false;
            passbyID=-1;
        };
        int value;
        int fromID;
        int passbyID;
        bool fConnect;
        string route;

};
int to_int(char const *s)
{
     if ( s == NULL || s[0] == '\0' )
     {
       return 0;
     }
     bool negate = (s[0] == '-');
     if ( *s == '+' || *s == '-' )
          ++s;
     int result = 0;
     while(*s)
     {
          if ( *s >= '0' && *s <= '9' )
          {
              result = result * 10  - (*s - '0');
          }
          else
              return 0;
          ++s;
     }
     return negate ? result : -result;
}
void Floyd_Warshall(des arrDes[101][101],int nAirportNum){
    for(int k=1;k<=nAirportNum;k++){
        for(int i=1;i<=nAirportNum;i++){
            for(int j=1;j<=nAirportNum;j++){
                    if(arrDes[i][k].fConnect&&arrDes[k][j].fConnect){
                        int temp=arrDes[i][k].value + arrDes[k][j].value;
                        if(temp<arrDes[i][j].value){
                            arrDes[i][j].value=temp;
                            arrDes[i][j].passbyID=k;
                            arrDes[i][j].fConnect=true;

                        }
                    }
            }
        }
    }
}
void printPath(des arrDes[101][101],int fromID,int toID,map<int,string> airport){
    if(arrDes[fromID][toID].passbyID==fromID){
        cout.flags(ios::left);
        string from=airport.find(fromID)->second;
        string to=airport.find(toID)->second;
        cout.width(21);
        cout<<from;
        cout.width(21);
        cout<<to;
        cout.width(11);
        cout<<arrDes[fromID][toID].route;
        cout.width(5);
        cout.flags(ios::right);
        cout<<arrDes[fromID][toID].value<<endl;
    }
    else{
        int passbyID=arrDes[fromID][toID].passbyID;
        printPath(arrDes,fromID,passbyID,airport);
        if(arrDes[passbyID][toID].passbyID==passbyID){
            cout.flags(ios::left);
            string from=airport.find(passbyID)->second;
            string to=airport.find(toID)->second;
            cout.width(21);
            cout<<from;
            cout.width(21);
            cout<<to;
            cout.width(11);
            cout<<arrDes[passbyID][toID].route;
            cout.flags(ios::right);
            cout.width(5);
            cout<<arrDes[passbyID][toID].value<<endl;
        }
        else
            printPath(arrDes,passbyID,toID,airport);
    }
}
int main(){
    char bufc[256];
    map<string,int> airportNameToID;
    map<int,string> airportIDToName;
    int nAirportNum=0;
    des arrDes[101][101];
    while(cin.getline(bufc,256)){
        string bufs(bufc);
        if(bufs=="")break;
        std::size_t found=bufs.find(",",0);
        string from=bufs.substr(0,found);
        bufs=bufs.substr(found+1,bufs.length());
        found=bufs.find(",",0);
        string to=bufs.substr(0,found);
        bufs=bufs.substr(found+1,bufs.length());
        found=bufs.find(",",0);
        string route=bufs.substr(0,found);
        bufs=bufs.substr(found+1,bufs.length());
        found=bufs.find(",",0);
        string miles=bufs.substr(0,found);
        bufs=bufs.substr(found+1,bufs.length());
        int fromID,toID;
        if(airportNameToID.find(from)!=airportNameToID.end()){
            fromID=airportNameToID.find(from)->second;
        }
        else{
            nAirportNum++;
            fromID=nAirportNum;
            airportNameToID.insert(pair<string,int>(from,fromID));
            airportIDToName.insert(pair<int,string>(fromID,from));
        }
        if(airportNameToID.find(to)!=airportNameToID.end()){
            toID=airportNameToID.find(to)->second;
        }
        else{
            nAirportNum++;
            toID=nAirportNum;
            airportNameToID.insert(pair<string,int>(to,toID));
            airportIDToName.insert(pair<int,string>(toID,to));
        }
        int nValue=to_int(miles.c_str());
        if(arrDes[fromID][toID].value>nValue){
            arrDes[fromID][toID].value=nValue;
            arrDes[fromID][toID].route=route;
            arrDes[fromID][toID].fromID=fromID;
            arrDes[fromID][toID].passbyID=fromID;
            arrDes[fromID][toID].fConnect=true;

            arrDes[toID][fromID].value=nValue;
            arrDes[toID][fromID].route=route;
            arrDes[toID][fromID].fromID=toID;
            arrDes[toID][fromID].passbyID=toID;
            arrDes[toID][fromID].fConnect=true;
        }

    }
    Floyd_Warshall(arrDes,nAirportNum);
    while(cin.getline(bufc,255)){
        string bufs(bufc);
        cout<<endl<<endl;
        std::size_t found=bufs.find(",",0);
        string from=bufs.substr(0,found);
        string to=bufs.substr(found+1,bufs.length());
        int fromID=airportNameToID.find(from)->second;
        int toID=airportNameToID.find(to)->second;
        cout<<"From                 To                   Route      Miles"<<endl;
        cout<<"-------------------- -------------------- ---------- -----"<<endl;
        printPath(arrDes,fromID,toID,airportIDToName);
        cout<<"                                                     -----"<<endl;
        cout.flags(ios::left);
        cout.width(42);
        cout<<"";
        cout.width(11);
        cout<<"Total";
        cout.flags(ios::right);
        cout.width(5);
        cout<<arrDes[fromID][toID].value<<endl;
    }
    return 0;
}

2013年7月18日 星期四

[UVA] 122 - Trees on the level solution

#include<iostream>
#include <string>
#include <queue>
#define Notc "not complete"
using namespace std;

int to_int(char const *s)
{
     if ( s == NULL || s[0] == '\0' )
     {
       return 0;
     }
     bool negate = (s[0] == '-');
     if ( *s == '+' || *s == '-' )
          ++s;
     int result = 0;
     while(*s)
     {
          if ( *s >= '0' && *s <= '9' )
          {
              result = result * 10  - (*s - '0');
          }
          else
              return 0;
          ++s;
     }
     return negate ? result : -result;
}
class node{
public:
    node(){
        value=-1;
        bLeft=0;
        bRight=0;
    }
    bool bLeft,bRight;
    node *left,*right;
    int value;

};
int LevelOrderTraversal(node *root){
    queue<node*> myqueue;
    queue<int> qAnswer;
    myqueue.push(root);
    while(!myqueue.empty()){
        node *ptr=myqueue.front();
        myqueue.pop();
        if(ptr->value==-1){
            cout<<Notc<<endl;
            return 1;

        }
        else{
            qAnswer.push(ptr->value);
            if(ptr->bLeft)
                myqueue.push(ptr->left);
            if(ptr->bRight)
                myqueue.push(ptr->right);
        }


    }
    bool whiteSpace=false;
    while(!qAnswer.empty()){
        if(whiteSpace) cout<<" ";
        cout<<qAnswer.front();
        qAnswer.pop();
        whiteSpace=true;
    }
    cout<<endl;

}
int main(){
    string tempV,tempP,bufs;


    int count_notc=0;
    node *root=new node();
    bool bRepeate=false;
    while(cin>>bufs){
        std::size_t comma=bufs.find(",",0);
       if(bufs.length()==2){
            if(bRepeate) {
                cout<<Notc<<endl;
                root->bLeft=false;
                root->bRight=false;
                root->value=-1;
                bRepeate=false;
                continue;
            }
            else{
                LevelOrderTraversal(root);
                root->bLeft=false;
                root->bRight=false;
                root->value=-1;
                bRepeate=false;
                continue;
            }
       }
       tempV=bufs.substr(1,comma-1);
       tempP=bufs.substr(comma+1,bufs.find(")",0)-comma-1);
       int value=to_int(tempV.c_str());
       node *ptr;
       ptr=root;
       if(tempP!=""){
            for(int i=0;i<tempP.length();i++){
               if(tempP[i]=='L'){
                    if(!ptr->bLeft){
                        ptr->left=new node();
                        ptr->bLeft=true;
                    }
                    ptr=ptr->left;
               }
                else if(tempP[i]=='R'){
                    if(!ptr->bRight){
                        ptr->right=new node();
                        ptr->bRight=true;
                    }
                    ptr=ptr->right;
               }
            }
       }
        if(ptr->value!=-1){
            bRepeate=true;
            continue;
        }
        else
            ptr->value=value;
   }
    return 0;
}

2012年11月23日 星期五

float和int 的比較

 當初學者在接觸C時,常常會覺得float這麼好用,可以表示整數,也可以表示小數點,是不是以後都用float就好呀?事實上,不是的!因為float會一些表示上的限制,還有電腦在計算float上的會比int慢,所以如果沒有要表示小數點,就還是乖乖用int吧!以下的範例,是用來示範float在表示數字和運算上的一些數字,當我們學會loop後,我會在用loop示範float和int在運算速度上的差異!


CODE:
#includ
main (){
        int i;
        float f;
        i=67108863;//i=2^26-1
        f=67108863;//i=2^26-1
        printf("interger of 2^26-1: %d\n",i);
        printf("floating number of 2^26-1: %.1f\n\n",f);
        i=100000000-1;
        f=100000000-1;
        printf("interger of 100000000-1: %d\n",i);
        printf("floating number of 100000000-1: %.1f\n",f);
        }
OUTPUT:
            interger of 2^26-1: 67108863
            floating number of 2^26-1: 67108864.0

            interger of 100000000-1: 99999999
            floating number of 100000000-1: 100000000.0


以上的結果,可以得知當數字比較大的時候float會有誤差,起因是因為,float在電腦的世界裡是用科學記號表示,也是像100=1.0*10^2的這種方式表示(詳見wiki),而single float(也就是float,相較於double float)可以表示的數字為1後面小數點23位(這裡是指2進位時,也就是1.1111111…),超過時會用一種「類似」四捨五入的方式處理(實際是電腦會選擇進會或捨去,讓數字是一個偶數),所以當我們要表示的數字大於2^24方時,就產生上面第一個例子的問題,莫名其妙被進位了....
另外float在計算的時候,會把次方小的對齊次大的(也就是1.0*10^3+1.0*10^2會把1.0*10^2變成0.1*10^3)然後做相加,於是就造成了第二種範例的結果!