愛伊米

Python面試基礎+演算法題精講

Python面試基礎+演算法題精講

第一題

def accum(s):

# TOD

pass

# accum(“abcd”) => “A-Bb-Ccc-Dddd”

# accum(“cwAt”) => “C-Ww-Aaa-Tttt”

這到題用到了字串的字母大寫、小寫、字串拼接、複製。用到的函式有 join 將列表中的內容按照指定字元連線成一個字串,

upper() 所有字母變大寫 和lower() 所有字母小寫

對於一個可迭代的(iterable)物件,enumerate將其組成一個索引序列,利用它可以同時獲得索引和值

enumerate多用於在for迴圈中得到計數

具體程式碼:

def asuum(s):

return ‘-’。join(y。upper() + y。lower()* x for x,y in enumerate(s))

a = asuum(‘abcd’)

print(a)

第二題

def duplicate_count():

# TODO

pass

# 實現將一串字串返回該字串有重複的的字母個數包括大小

# test。assert_equals(duplicate_count(“abcde”), 0)

# test。assert_equals(duplicate_count(“abcdea”), 1)

# test。assert_equals(duplicate_count(“indivisibility”), 1)

這裡用到了將所有字母都轉成小寫還有集合,和列表

具體程式碼:

def duplicate_count(text):

text = text。lower()

# 用以去重,和原文字進行對比

texts = set(text)

lists = []

for i in texts:

numbers = text。count(i)

if numbers != 1:

lists。append(numbers)

return len(lists)

第三題

def descending_order(num):

# TODO

pass

# descending_order(21445) => 54421

# descending_order(145263) => 654321

# descending_order(1254859723) => 9875543221

具體程式碼:

def descending_Order(num):

return int(“”。join(sorted(str(num), reverse=True)))

第四題

def error_printer(s):

# TODO

pass

# s=“aaabbbbhaijjjm”, error_printer(s) => “0/14”

# s=“aaaxbbbbyyhwawiwjjjwwm”, error_printer(s) => “8/22”

# 計算字串有多少個在‘abcdefghijkmlm’裡

具體程式碼:

from re import sub

def printer_error(s):

return “{}/{}”。format(len(sub(“[a-m]”,‘’,s)),len(s))

我們講一下sub 的用法

re。sub共有五個引數。

re。sub(pattern, repl, string, count=0, flags=0)

其中三個必選引數:pattern, repl, string

兩個可選引數:count, flags

第一個引數:pattern

pattern,表示正則中的模式字串,這個沒太多要解釋的。

第二個引數:repl

repl,就是replacement,被替換,的字串的意思。

repl可以是字串,也可以是函式。

repl是字串

如果repl是字串的話,其中的任何反斜槓跳脫字元,都會被處理的。

\n:會被處理為對應的換行符;

\r:會被處理為回車符;

其他不能識別的轉移字元,則只是被識別為普通的字元:

比如\j,會被處理為j這個字母本身;

反斜槓加g以及中括號內一個名字,即:\g,對應著命名捕獲

第三個引數:string

string,即表示要被處理,要被替換的那個string字串。

第五題

def expanded_form(num):

# TODO

pass

# expanded_form(12) => ‘10 + 2’

# expanded_form(42) => ‘40 + 2’

# expanded_form(70304) => ‘70000 + 300 + 4’

具體程式碼:

def expanded_form(num):

nums = str(num)

x = []

# 這裡也可以用enumerate來做

for i in range(0, len(nums)):

if int(nums[i]) != 0:

s = str(int(nums[i]) * (10 ** (len(nums) - i - 1)))

x。append(s)

return ‘ + ’。join(x)

第六題

def order_weight(s):

# TODO

pass

# 計算每一個數字的累加和

# order_weight(“2000 10003 1234000 44444444 9999 11 11 22 123”) => “11 11 2000 10003 22 123 1234000 44444444 9999”)

# 2000就是2, 10003就是4, 也就是這串數字的累加和

具體程式碼:

def order_weight(s):

return ‘ ’。join(sorted(sorted(s。split(‘ ’)),key = lambda x:sum(int(c) for c in x)))

首先把這個數進行切割,然後用lambda算出這個數的累加和,在根據累加和進行排序。

第七題

def valid_parentheses(s):

# TODO

pass

# “()” => True

# “)(()))” => False

# “(” => False

# “(())((()())())” => True

# 判斷是否是有效空號對

具體程式碼:

def valid_parentheses(string):

cnt = 0

for i in string:

if i == “(”:

cnt+=1

if i == “)”:

cnt-=1

if cnt < 0:

return False

return True if cnt == 0 else False

這道題目題解很多,這是最簡單的了,大家可以自行實現一個棧,當然也可以去我的github的exercise/python_data_structure/base_data_structure/stack。py下載,這道題目其實是一個標準的檢驗棧實現。

檢測到一個左括號就壓棧,檢測到一個右括號就出棧,最後檢查棧,若還有元素則False,沒有則True。

所有程式碼都已上傳:https://github。com/MiracleYoung/exercises/