撩課-Python-每天5道面試題-第5天
阿新 • • 發佈:2018-12-05
oat pan eve 思路 radius file math res block
單詞:you的個數為:1
一. 給定一個圓心和半徑, 以及一個點坐標, 判定該點是否在圓內;
例如: 用戶輸入圓心: (1, 2) 半徑: 2.5 測試點為(2, 2)
結果: 判定測試點是在圓內
思路:
- 結合勾股定理, 計算測試點距離圓心的距離test_distance;
- 比對test_distance 與半徑的長短, 如果大於, 則不在圓內;
- 如果小於, 則在圓內
# 1. 獲取測試案例數據 circle_center_str = input("請輸入圓心坐標, 使用逗號分隔, 例如:1,2:") circle_radius_str = input("請輸入圓的半徑:") test_point_str = input("請輸入測試點坐標, 使用逗號分隔, 例如:2,2:") # 2. 對數據進行處理, 轉換成元組或者浮點數形式 circle_center = eval("({})".format(circle_center_str)) # 此步驟取巧, 你也可以拆解單獨轉化 circle_radius = float(circle_radius_str) test_point = eval("({})".format(test_point_str)) # 此步驟取巧, 你也可以拆解單獨轉化 # 3. 計算測試點距離圓心的距離 import math # 一般將模塊導入, 放在最上面 test_distance= math.sqrt(math.pow(test_point[0] - circle_center[0], 2) + math.pow(test_point[1] - circle_center[1], 2)) # 4. 判定, 打印結果 result = "點在圓內" if test_distance <= circle_radius else "點在圓外" print(result)
二. 代碼實現: 統計一篇文章中, 每個單詞出現的個數
例如: "I like IT, do you like it?"
結果:
單詞:i的個數為:1
單詞:like的個數為:2
單詞:it的個數為:2
單詞:do的個數為:1
單詞:you的個數為:1
註意: 不區分大小寫
思路:
- 分割出文章中所有的單詞
- 遍歷單詞列表, 通過字典記錄每個單詞個數{單詞: 個數}
- 讀取字典, 進行打印
# 0. 給定原始文章 content = "I like IT, do you like it?" # 1. 簡單的通過正則, 分割文章為獨立的單詞 # 並對結果單詞進行過濾, 單詞進行小寫, 壓縮處理 import re word_list = re.split("[ ,.?!]", content) word_list = [word.strip(‘ ‘).lower() for word in word_list if len(word) > 0] # 2. 遍歷單詞列表, 通過字典進行統計 resut_dic = {} for word in word_list: if word in resut_dic.keys(): resut_dic[word] += 1 else: resut_dic[word] = 1 # print(resut_dic) # 3. 遍歷字典鍵值對, 打印結果 for key, value in resut_dic.items(): print("單詞:{}的個數為:{}".format(key, value))
三. 代碼實現: 把一個列表,隨機打亂;手動實現!
例如:num_list = [1, 2, 3, 4, 5]
結果可能是:[1, 2, 5, 4, 3] [5, 3, 4, 2, 1] 等等
每次執行都是隨機結果;
思路:
- 獲取列表長度
- 隨機一個索引值
- 移除, 以及添加到另外一個列表中
import random num_list = [1, 2, 3, 4, 5] result = [] while len(num_list) > 0: idx = random.randint(0, len(num_list) - 1) result.append(num_list[idx]) del num_list[idx] print(result)
四. 用戶輸入一個字符串,判定是否為對稱字符串;
例如: “abcxcba”,"abcxxcba"均為對稱字符串;“abcb”則不是
思路:
方式1: 反轉字符串, 對比即可
方式2: 循環遍歷, 首尾字母進行比對
content = input("請輸入一個測試字符串:") reverse_content = content[::-1] result = "是對稱字符串" if content == reverse_content else "不是對稱字符串" print(result)
五. 代碼實現: 情報傳遞, 實現明文密文切換
切換規則為, 手機9宮格輸入法和數字鍵的對應關系
例如: a 在九宮格輸入法中的 2鍵位置的 索引為0的字符, 則可加密為: 20; 以此類推; 空格為 00
示例: "wo ai ni" ==> 9062002042006142
思路:
- 構造出明文到密文的破譯表
- 通過比對破譯表, 進行轉換密文或者明文
# 0. 構造出密文明文對照字典 map_dic = { 2: "abc", 3: "def", 4: "ghi", 5: "jkl", 6: "mno", 7: "pqrs", 8: "tuv", 9: "wxyz", 0: " " } # 1. 為了提高執行效率, 可以采用, "空間換時間的策略" # 1.1 構造明文到密文的轉換字典 # 1.2 構造密文到明文的轉換字典 mingwen_to_miwen_dic = {} miwen_to_mingwen_dic = {} for key, value in map_dic.items(): for idx, char in enumerate(value): code = "{}{}".format(key, idx) mingwen_to_miwen_dic[char] = code miwen_to_mingwen_dic[code] = char print(mingwen_to_miwen_dic) print(miwen_to_mingwen_dic) # 2. 給定明文,進行加密 mingwen = "wo ai she hui sz" miwen = "" for char in mingwen: miwen += mingwen_to_miwen_dic[char] print(miwen) # 90620020420073413100418142007393 # 3. 給定密文, 解密 miwen = "90620020420073413100418142007393" mingwen = "" for idx in range(0, len(miwen), 2): code = miwen[idx: idx + 2] mingwen += miwen_to_mingwen_dic[code] print(mingwen)
撩課-Python-每天5道面試題-第5天