撩課 Python 每天5道面試題 第5天

2022-07-09 06:24:12 字數 4535 閱讀 9994

一. 給定乙個圓心和半徑, 以及乙個點座標, 判定該點是否在圓內;

例如: 使用者輸入圓心: (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

) 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 =

# 1. 為了提高執行效率, 可以採用, "

空間換時間的策略"#

1.1構造明文到密文的轉換字典

# 1.2

構造密文到明文的轉換字典

mingwen_to_miwen_dic ={}

miwen_to_mingwen_dic ={}

for key, value in

map_dic.items():

for idx, char

inenumerate(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 = ""

forchar

inmingwen:

miwen += mingwen_to_miwen_dic[char

]print(miwen) #

90620020420073413100418142007393# 3

. 給定密文, 解密

撩課 Python 每天5道面試題 第2天

一.簡述程式設計過程中,注釋的作用?1 方便開發人員自己理清楚 思路 因為開發人員在拿到乙個需求時,首先應該思考的是如何將需求問題,分解成具體的實施步驟 第一步幹啥,第二步幹啥,第三步幹啥,安排的明明白白 這一步也是零基礎學員欠缺的步驟,而且是非常重要的步驟 這都理不清,絕對寫不出來 為了方便記錄和...

撩課 Java每天5道面試題第25天

則會對配置檔案當中匹配mvc view controller 注意點 使用時要新增後面的內容 複製 是一種簡寫形式 會自動註冊三個bean exceptionhandlerexceptionresolver 並提供了 資料繫結支援,numberformatannotation支援,datetimef...

撩課 Web大前端每天5道面試題 Day27

瀏覽器快取分為強快取和協商快取。當客戶端請求某個資源時,獲取快取的流程如下 先根據這個資源的一些 http header 判斷它是否命中強快取,如果命中,則直接從本地獲取快取資源,不會發請求到伺服器 當強快取沒有命中時,客戶端會傳送請求到伺服器,伺服器通過另一些request header驗證這個資...