# GitHub Copilot 使用 ![image](https://hackmd.io/_uploads/S1Du8YVER.png =30%x) ## 1. 申請 Apply [同學可參考](https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E7%94%A8%E8%80%81%E5%B8%AB-%E5%AD%B8%E7%94%9F%E8%BA%AB%E4%BB%BD%E5%85%8D%E8%B2%BB%E4%BD%BF%E7%94%A8-github-copilot-223236e0e0e8) ## 2. 安裝 Install 打開 Vscode 後,選擇 Extensions, 輸入 GitHub Copilot - 選擇 Extensions ![image](https://hackmd.io/_uploads/r1VcLFNEA.png) - 輸入 GitHub Copilot ![image](https://hackmd.io/_uploads/SJjAwTNVC.png) - 選擇 GitHub Copilot 並安裝 ![image](https://hackmd.io/_uploads/rk-iUYVN0.png) - 安裝 install ![image](https://hackmd.io/_uploads/ryHsIYVNR.png) ## 3. 功能 ### 再輸入時會透過程式碼給一些提示 按下 tab 鍵 灰色字體就會自動生成,省去打字過程 ![image](https://hackmd.io/_uploads/ByssUYVNR.png) ## 4. 實際使用 Example 這次以上禮拜的 LeetCode Weekly Contest 399 -> 1和3題 為示範 因為這兩題除了測資、答案大小不同,其餘皆為一樣的題目 ### [LeetCode Weekly Contest 399](https://leetcode.com/contest/weekly-contest-399/) - 第一題 - 給各位10分鐘試著寫出這題,Brute Force即可 - Code ```c= class Solution { public: int numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) { int res = 0; for (int i = 0; i < nums1.size(); i++) { for (int j = 0; j < nums2.size(); j++) { if (nums1[i] % (nums2[j]*k) == 0) { res++; } } } return res; } }; ``` - 第三題 - 試著運用 GitHub Copilot 詢問是否有更佳的解法 - 打開 chat ![image](https://hackmd.io/_uploads/rkEhIK4VC.png) - 可在聊天室輸入想詢問的資訊 ![image](https://hackmd.io/_uploads/BJ-T8KN4R.png =40%x) - 輸入以下 ``` 我有一個C++題目需要解決 我已經完成暴力解的部分,該如何更佳優化它 ===題目: You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k. A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1). Return the total number of good pairs. ``` (**如果給的是二分搜那需要告訴他說使用 Hash function**) - 因為不能只知道答案,我們可以透過詢問的方式,了解程式碼是做到了甚麼優化 在聊天室打上`因為我是程式小白,可以跟我解釋這段程式碼在做甚麼嗎以及與原本的暴力解有甚麼區別` ![image](https://hackmd.io/_uploads/HkTaUK4N0.png =85%x) - Code ```c= class Solution { public: long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); long long res = 0; unordered_map<int,int> mp; for (int num:nums2) { mp[num*k]++; } for (int num:nums1) { int sqrnum = sqrt(num); for (int i = 1; i <= sqrnum; i++) { if (num % i == 0) { if (mp.find(i) != mp.end()) { res += mp[i]; } if (num/i != i && num % (num/i) == 0 && mp.find(num/i) != mp.end()) { res += mp[num/i]; } } } } return res; } }; ``` ## 5. 參考 [能節省四成開發時間的工具? GitHub Copilot 的介紹與心得分享](https://useme.medium.com/%E8%83%BD%E7%AF%80%E7%9C%81%E5%9B%9B%E6%88%90%E9%96%8B%E7%99%BC%E6%99%82%E9%96%93%E7%9A%84%E5%B7%A5%E5%85%B7-github-copilot-%E7%9A%84%E4%BB%8B%E7%B4%B9%E8%88%87%E5%BF%83%E5%BE%97%E5%88%86%E4%BA%AB-2a07e8ce8f6d) ## 6. 重點 **語法交給 copilot ,但架構還是要交給人**