# 資訊科技產業專案設計課程作業 5 > 貢獻者:李馬克 :::warning 無法開啟給定的影片! :notes: jserv ::: ## [Validate IP Address](https://leetcode.com/problems/validate-ip-address/) >時間軸: [00:00:30](https://youtu.be/e43GVaMoLbY?t=30)-00:25:00 ### Question description Validate an IP address (IPv4). An address is valid if and only if it is in the form "X.X.X.X", where each X is a number from 0 to 255. For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid IP addresses, while "12.34.56.oops", "1.2.3.4.5", and "123.235.153.425" are invalid IP addresses. Examples: ``` ip = '192.168.0.1' output: true ip = '0.0.0.0' output: true ip = '123.24.59.99' output: true ip = '192.168.123.456' output: false ``` Constraints: - [time limit] 5000ms - [input] string ip - [output] boolean ### Solution 1. 利用'.'切割ip字串,並變成一個陣列 2. 檢查陣列元素是否是4個,若不為4個則回傳False 3. 檢查陣列中各字串組成是否為空字串、0開頭或是含有非數字在字串中,若有則回傳False 4. 將陣列中各字串轉為Int並解查是否在0~255之間,若沒有則回傳False ```python= def validateIP(ip): # split string by '.' split_str = ip.split('.') #['192', '168', '0', '1'] # check the number of sub-string if(len(split_str) != 4): return False # check the length of each sub-string, if more than one char check condition of next line for sub_str in split_str: if(sub_str == ''): return False if len(sub_str) != 1: # check each sub-string did contain the 0 at the first, if have contain then return false if(sub_str[0] == 0): return False for c in sub_str: if(c<'0' or c>'9'): return False # convert all the substring to int and check is between 0 to 255 if(int(sub_str)<0 or int(sub_str)>255): return False return True ``` Time complexity: 根據檢查陣列中每個元素以及每個元素的每個字元,因此為```O(n^2)``` Space complexity: 會根據切割ip字串後,行程的陣列有多大而定,因此為```O(n)``` ### Self-Criticism REACTO - Repeat: 重新描述題目,但缺乏詢問interviewer更多的例外狀況,導致test時需要重複的去修改 - Examples: 針對題目條件,舉出符合的案例 - Approach: 說明自己打算採用的方案 - 在列出Example時就已經想好一部分的Approach - 在描述Approach時盡可能的把狀況以註解列點寫下來,有利於Coding時讓自己以及Interviewer知道在幹麻 - Code: 撰寫程式 - 因為不確定一些function的用法還要偷偷去查,丟臉 - Test: 測試案例 - 使用題目案例驗證及說明程式碼運作 - Optimize: 優化程式碼 - 通常是優化時間或空間複查度,但因為測試案例沒過,所以花比較多時間在優化條件,導致後面沒有更多follow up ### 程式碼改進與討論 1. 應該在Repeat和Example的過程中進可能去思考或接觸例外狀況,才能和Interviewer有更多討論和互動,之後的實做也比較能快速做對 2. 應該在更了解特定程式語言的函式用法,避免在過程中會因為知道如何做但忘記該怎麼做,造成時間浪費 --- ## [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) >時間軸: [00:25:08](https://youtu.be/e43GVaMoLbY?t=1508)-01:00:00 ### Question description A Toeplitz matrix is a matrix where every left-to-right-descending diagonal has the same element. Given a non-empty matrix arr, write a function that returns true if and only if it is a Toeplitz matrix. The matrix can be any dimensions, not necessarily square. For example, ``` [[1,2,3,4], [5,1,2,3], [6,5,1,2]] ``` is a Toeplitz matrix, so we should return true, while ``` [[1,2,3,4], [5,1,9,3], [6,5,1,2]] ``` isn’t a Toeplitz matrix, so we should return false. Constraints: - time limit] 5000ms - [input] array.array.integer arr - 0 ≤ arr.length ≤ 20 - 0 ≤ arr[i].length ≤ 20 - 0 ≤ arr[i][j] ≤ 20 - [output] boolean ### Solution #### Approach 1 掃描矩陣中的每個元素,並檢查元素(n, m)與(n+1, m+1)是否相同,相同集回傳true ```python= def isToeplitz(arr): for row in range(len(arr)-1): for col in range(len(arr[0])-1): print(arr[row][col]) print(arr[row+1][col+1]) if(arr[row][col] != arr[row+1][col+1]): return False return True ``` Time complexity: 走訪矩陣的每個元素,因此為O(n^2) Space complexity: 沒有建立其他空間,因此為O(1) #### Approach 2 一次檢查每個元素從top-left到bottom-right是否皆相同 ## Peer FeedBack ![](https://i.imgur.com/qDQzHLj.png) ## 檢討學期的表現 雖然以前有一些面試的經驗,但在過程中和Interviewer的互動比較像是一問一答,像是在考試比誰答對分數高,過程中時常覺得沒答對就失去機會,但透過這學期學到其實過程中更重要的是互動和討論,面試就是一場表演,誰演的好看就更有機會,回顧我這學期進步的部份以及需要持續改進的點,我認為我練習比較多的還是在白板題的表現,而在概念性的回答以及behavior question需要再更多練習,才能讓整個過程表演的更順暢也更專業。 進步的部份 - 作業和Mock Interview都有採用REACTO答題,不只能釐清自己的思路,也能夠和Interviewer有更多互動 - Coding過程中要能夠一直說明在幹麻,不會讓會議只有打字聲 持續改進 - 概念性的東西要簡答,控制時間在30~45秒內清楚描述 - 要盡可能把問題與自己實做過得東西連結在一起 - 表演!表演!表演!!!