# 2023 年「[資訊科技產業專案設計](https://hackmd.io/@sysprog/info2023)」作業 1 > 貢獻者: 張無忌-Scumbag > 🦸:interviewer (**藍色polo衫**)🤵:interviewee (**咖啡色外套**) > [錄影: 英](https://www.youtube.com/watch?v=xhHjegg9-Z8) > [錄影: 漢](https://www.youtube.com/watch?v=E4qJdJRH5bY) > [錄影: 漢](https://www.youtube.com/watch?v=IzjbLlweeNs) ## 自評-程式部分 * 需要多練習,或許不用一開始就撰寫簡潔或優化過的程式,循序遞進也助於增加面試的互動,進而增加交談的時間。 * 打字速度較慢,故在影片中以轉場帶過,需要慢慢訓練,讓語速跟手速能達到恰當的平衡。 * 除Python以外應多嘗試用其他語言去解題,畢竟Python實在太多外掛可以用了。 ## 自評-面試部分 * 英文口說不流暢,發音也不太標準,還得多加練習。 * 在擔任Interviewee身分時,如為線上面試,應避免椅子的晃動,否則會呈現出一種輕浮的觀感。 * 發現自己不太能邊撰寫程式邊說話,這在面試互動上會是個大硬傷。 * 會不自覺把本該用英文表達的專業用詞,都用中文說。 ## 作業心得 * 蠻有趣的體驗,雖然被剪片這件事弄得蠻躁的,不過在過程中透過自問自答的方式,一步步去設計問題及回答,或許未來進入職場,也有機會經歷這兩種身分,透過這次的作業能更清楚知道該問甚麼該答甚麼,並針對不足的地方進行加強。 ## [1768. Merge Strings Alternately [easy]](https://leetcode.com/problems/merge-strings-alternately/?envType=study-plan-v2&envId=leetcode-75) ###### REACTO * Repeat:輸出的首字選擇、兩輸入長度是否相同進行確認 * Example:'abc'與'123'交替合併後是'a1b2c3' or '1a2b3c'? * Approach: >case1: len(input1)>len(input1) ->line 11 case2: len(input1)=len(input1) ->line 19 case3: len(input1)<len(input1) ->line 24 * Code * Test * Optimization:優化 space complexity >由於 string 具 immutable 的性質,舉例來說: ```python my_string="Hello" my_string+="World" ``` >+=這個 operation 並不是對原始的 **my_string** 做修改,實際上創建了一個新的 string 物件,將 "Hello" 及 "World" 串接在一起成 "Hello World" 後再賦予給這個新的 string 物件。 解決方案:改用list來解決這個問題。 #### 尚未優化前的程式碼(python) ```python class Solution(object): def mergeAlternately(self, word1, word2): #宣告一個空字串,用以儲存及回傳result merged='' #word1 及 word2 的長度未必相同 須先作判別 #word1長度>word2長度 if (len(word1)>len(word2)): for i in range(len(word1)): if (i<(len(word2))): merged+=(word1[i]+word2[i]) else: merged+=word1[i] #word1長度=word2長度 elif(len(word1)==len(word2)): for i in range(len(word1)): merged+=(word1[i]+word2[i]) #word1長度<word2長度 else: for i in range(len(word2)): if (i<(len(word1))): print(i) merged+=(word1[i]+word2[i]) else: merged+=word2[i] return merged ``` #### 針對 sapce complexity 進行優化 ```python class Solution: def mergeAlternately(self, word1, word2): #透過max()先比較word1、word2長度 merged = [] len1, len2 = len(word1), len(word2) max_len = max(len1, len2) for i in range(max_len): if i < len1: merged.append(word1[i]) if i < len2: merged.append(word2[i]) return ''.join(merged) ``` * **參考資訊:[stack overflow](https://stackoverflow.com/questions/9097994/arent-python-strings-immutable-then-why-does-a-b-work)** ## [2215 Find the Difference of Two Arrays [easy]](https://leetcode.com/problems/find-the-difference-of-two-arrays/?envType=study-plan-v2&envId=leetcode-75) ###### REACTO * Repeat:輸入的list中,是否會有重複的元素;以及輸出結果是否可能為空。 * Example: ```python num1=[1,2,3,3] num2=[2,4,6] #輸出應該是[1,3] or [1,3,3]? ``` ```python num1=[2,3,4] num2=[2,3,4] #輸出為空 ``` * Approach:以雙層for loop逐一比對,比較兩者間差異並且依條件紀錄結果。 * Code * Test * Optimization:優化 time complexity,以集合下去討論 #### 尚未優化前的程式碼(python) ```python class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: answer=[[],[]] for item1 in nums1: found= False for item2 in nums2: if item1 == item2: found =True break if not found : if item1 not in answer[0]: answer[0].append(item1) for item2 in nums2: found= False for item1 in nums1: if item2 == item1: found =True break if not found : if item2 not in answer[1]: answer[1].append(item2) return answer ``` #### 針對 Time complexity 進行優化 ```python class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: #首先宣告一個二維的list answer[0]用於儲存第一筆回傳結果 也就是紀錄存在於nus1中 但不存在於nums2中的元素 answer = [[], []] set1 = set(nums1) set2 = set(nums2) diff1 = set1 - set2 diff2 = set2 - set1 return [list(diff1), list(diff2)] ``` * 參考資訊 [Python Wiki](https://wiki.python.org/moin/TimeComplexity) ## [151. Reverse Words in a String [medium]](https://leetcode.com/problems/reverse-words-in-a-string/?envType=study-plan-v2&envId=leetcode-75) ###### REACTO * Repeat: >Q1. 確定題目類型 >Q2. 單字與單字間的空白數? >Q3. 輸出的頭尾是否有空白? * Example: >A1. 是**apple** 變 **elppa** 還是 **who are you** 變 **you are who?** >A2. 輸入不拘,輸出只能一個。 >A3. 輸入:' Who are you ' 輸出:'you are Who' * Approach:遍歷輸入,逐一確認各個字元是不是空字元,以倒順序的方式從前端併入。 * Code * Test * Optimization:針對簡潔度、可讀性討論。 #### 尚未優化前的程式碼(python) ```python class Solution: def reverseWords(self, s: str) -> str: reversed_s = "" word = "" for char in s: if char != ' ': word += char elif word: reversed_s = word + ' ' + reversed_s word = "" if word: reversed_s = word + ' ' + reversed_s return reversed_s.strip() ``` #### 針對簡潔度可讀性進行優化 ```python class Solution: def reverseWords(self, s: str) -> str: words = s.split() # Reverse the order of words words.reverse() # Join the reversed words with a single space between them return ' '.join(words) ``` #### 雖然更簡潔,但我認為可讀性不好 ```python class Solution: def reverseWords(self, s: str) -> str: #以切片的方式作處理 return ' '.join(s.split()[::-1]) ``` #### 結論 >未必越簡潔越好,需合適的控制以免可讀性變糟。 > --- ## 第二次作業-他評01 ### 針對 interviewer - [ ] 優點 * 詢問 interviewee 改進方案,並針對改進方案點評 - [ ] 可改進的地方 * 直接用 leetcode 原題,容易讓 interviewee 背答案。也許解題過後可以加上一些變形或是問其它有連結性的問題會更好。 ## 針對 interviewee - [ ] 優點 * 咬字清晰,解釋有條理 - [ ] 可改進的地方 * 雖然邊想邊寫程式真的很難,但沒有拍出程式過程還是有點可惜(加油!) * 前面 "R", "E" 的步驟搭配畫面打字會比直接用講得更清楚 * 面試時應該會重在思考過程和相互溝通,應該不會依靠 leetcode 本身的執行來判斷程式的正確性 -- 1768. Merge Strings Alternately [easy] -- * [3:11](https://youtu.be/xhHjegg9-Z8?si=QpsMtRo2xeXVra79&t=191), [3:25](https://youtu.be/xhHjegg9-Z8?si=wYHPX7hvmBLJB6Ug&t=205) "completed" 和 "complexity" 發音怪怪的 -- 151. Reverse Words in a String [medium] -- * [1:29](https://youtu.be/IzjbLlweeNs?si=7syxXfZ4APl0cDet&t=89) "想聽聽你的看法"這句比較像是 interviewer 會對 interviewee 講的,也許可以改的比較委婉一點,例如 "我目前有些想法但不太確定,我可以請問你會從什麼地方去突破嗎?" * 自評的部分提到 `s.split()[::-1]` 的用法可讀性不佳,但這個寫法其實就是 python 內建用法,跟可讀性沒有太大關連。 ## 他評02 ### interviewer - [ ] 優點 * 語速均衡與咬字清楚 - [ ] 可改進的地方 * 隔著口罩與安全帽,收音的清晰度受到嚴重影響 ### interviewee - [ ] 優點 * 解說code時條理清晰 - [ ] 可改進的地方 * 與interviewer相同,隔著口罩與安全帽,收音的清晰度受到嚴重影響 ## 他評03 ### interviewer - [ ] 優點 * 開頭的開場白很好 * 口齒清晰 - [ ] 可改進的地方 * 對題目做延伸討論 ### interviewee - [ ] 優點 * 反覆提問確認對題目的掌握 * 對程式的解說清楚易懂 - [ ] 可改進的地方 * 可以嘗試講解的同時,一邊打程式