2023年6月23日 星期五

Conda 指令筆記

建創環境

conda create -n my_env python=3.7 numpy Keras


啟動環境

# For  conda 4.6 and later versions on Linux/macOS/Windows, use

conda activate my_env

#For conda versions prior to 4.6 on Linux/macOS, use 

source activate my_env

#For conda versions prior to 4.6 on Windows, use 

activate my_env


列出套件

# If the environment is not activated

conda list -n env_name

# If the environment is activated

conda list

# To see if a specific package, say `scipy` is installed in an environment

conda list -n env_name scipy


關閉環境

# For  conda 4.6 and later versions on Linux/macOS/Windows, use

conda deactivate

#For conda versions prior to 4.6 on Linux/macOS, use 

source deactivate

#For conda versions prior to 4.6 on Windows, use 

deactivate


移除環境

conda env remove -n env_name



其他

pip freeze > requirements.txt

pip install -r requirements.txt



2023年6月21日 星期三

[Python] Command Line arguments 使用範例

get_input_args.py

import argparse

def get_input_args():

    # Create Parse using ArgumentParser
    parser = argparse.ArgumentParser()

    # Create 3 command line arguments as mentioned above using add_argument() from ArguementParser method
    parser.add_argument('--dir', type = str, default = 'pet_images/', 
                    help = 'path to the folder of pet images') 
    parser.add_argument('--arch', type = str, default = 'vgg',
                        help = 'which CNN Model Architecture')
    parser.add_argument('--dogfile', type = str, default = 'dognames.txt',
                        help = 'Text File with Dog Names')
    
    # Replace None with parser.parse_args() parsed argument collection that 
    # you created with this function 
    return parser.parse_args()


main.py

in_arg = get_input_args()

print(in_arg.dir)

2023年6月18日 星期日

在 pythonanywhere 上部署 Flask web + install package

雖然有內建先在環境裝好常見的套件,但仍有些要手動安裝。


1. Dashboard > new console

2. Create a virtualenv

ex:

mkvirtualenv myvirtualenv --python=/usr/bin/python3.10

3. install packages

ex:

pip install -r requirements.txt

4. update Configuration 

要讓 web app 知道要在我們指定的環境上運行。

Web > Virtualenv > "your path"

ex:

/home/myusername/.virtualenvs/myvirtualenv

5. Reload


如果無法順利打開,可以看 Log。


ref

https://help.pythonanywhere.com/pages/Virtualenvs/

2023年6月17日 星期六

VirtualBox Ubuntu 全螢幕時黑屏 ( black screen when full screen)

Take the VRAM to the max of 128 MB (VM Settings » Display » Screen).

機器關機時,回到 virtual box

設定 > 顯示 > 視訊記憶體 調到  128 MB。


ref

https://forums.virtualbox.org/viewtopic.php?t=91084

2023年6月16日 星期五

Unity 專案上傳至 GitHub

整包專案全部上傳太大,用 gitignore 選擇只將需要的 push 上去。

參考下方連結內的 .gitignore。

https://douduck08.wordpress.com/2017/01/01/gitignore-setting-for-unity/

安裝 Unity Recorder

 新版本的 Unity 無法直接安裝 Unity Recorder。


方法:

1. Open Project Manager

2.  clicking  "+" > "Add package by name..."

3.  type "com.unity.recorder"


等待一段時間後即可。


ref

https://forum.unity.com/threads/cant-find-unity-recorder.908690/

在 VirtualBox 上安裝 Ubuntu 與共享資料夾、調整螢幕大小、雙向剪貼簿

簡易步驟:

1. 裝 VirtualBox

2. 下載 Ubuntu iso 檔

3. 新增虛擬機與設定


共享資料夾:

1.  把資料夾加入共用 (記得選取「自動掛載」)

2.  試試看打開資料夾


https://ubuntu.com/tutorials/how-to-run-ubuntu-desktop-on-a-virtual-machine-using-virtualbox#2-create-a-new-virtual-machine

設定與在 virtualbox 的 ubuntu 共享資料夾所遇問題 " You do not have the permissions necessary to view the contents of "

打開 terminal,輸入

sudo adduser $USER vboxsf

後重啟即可解決。


ref

https://askubuntu.com/questions/890729/this-location-could-not-be-displayed-you-do-not-have-the-permissions-necessary

2023年6月8日 星期四

1351. Count Negative Numbers in a Sorted Matrix

解題思路

先以單一個 row 來看,要怎麼找有幾個負數?

根據題目說明的特性,只要找到從哪裡開始都是負數的位置就好。那可以用 binary search。

譬如:[4,3,2,-1],最後 left 會停在 -1 的位置,那就可以算有幾個負數。

另外,因為下面的 row 肯定負數/正數的邊界只會越靠左,所以可以先存這邊以加快速度。

程式碼

class Solution {
public:
    int helper(vector<vector<int>>& grid, int& right, int row)
    {
        int left = 0;
        while(left <= right)
        {
            int mid = (left + right) / 2;
            if(grid[row][mid] >= 0)
                left++;
            else
                right--;
        }
        right = min(right, left);
        return grid[row].size() - left;
    }
    int countNegatives(vector<vector<int>>& grid) {
        int ans = 0, right = grid[0].size() - 1;
        for(int i=0; i<grid.size(); i++)
        {
            ans += helper(grid, right, i);
        }
        return ans;
    }
};

2023年6月7日 星期三

1318. Minimum Flips to Make a OR b Equal to c

解題思路

每次都把 a, b, c 的LSB 抓出來看,如果 a, b 的 bit OR 不等於 c,就要進一步檢查。

如果 c 是 0,代表 a,b 一定要都是0,所以算 a, b是1的數量。

如果 c 是 1,那就只需要改一個bit,直接加一就好。


程式碼

class Solution {
public:
    int minFlips(int a, int b, int c) {
        int ans = 0;
        while(a != 0 || b!= 0 || c != 0)
        {
            int m1 = a % 2, m2 = b % 2, m3 = c % 2;
            if((m1 | m2) != m3)
            {
                if(m3 == 0)
                    ans += (m1 == 1) + (m2 == 1);
                else
                    ans += 1;
            }
            a /= 2;
            b /= 2;
            c /= 2;
        }
        return ans;
    }
};