'2019下半年Python高頻面試題,第六彈'

Python 面試 Line 千鋒python學院 2019-09-15
"

2019下半年Python高頻面試題,第六彈出爐!

一. Given an array of integers

給定一個整數數組和一個目標值,找出數組中和為目標值的兩個數。你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。示例:給定nums = [2,7,11,15],target=9 因為 nums[0]+nums[1] = 2+7 =9,所以返回[0,1]

class Solution:
def twoSum(self,nums,target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
size = 0
while size < len(nums):
if target-nums[size] in d:
if d[target-nums[size]] <size:
return [d[target-nums[size]],size]
else:
d[nums[size]] = size
size = size +1
solution = Solution()
list = [2,7,11,15]
target = 9
nums = solution.twoSum(list,target)
print(nums)

給列表中的字典排序:假設有如下list對象,alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}],將alist中的元素按照age從大到小排序 alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}]

alist_sort = sorted(alist,key=lambda e: e.__getitem__('age'),reverse=True)
"

2019下半年Python高頻面試題,第六彈出爐!

一. Given an array of integers

給定一個整數數組和一個目標值,找出數組中和為目標值的兩個數。你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。示例:給定nums = [2,7,11,15],target=9 因為 nums[0]+nums[1] = 2+7 =9,所以返回[0,1]

class Solution:
def twoSum(self,nums,target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
size = 0
while size < len(nums):
if target-nums[size] in d:
if d[target-nums[size]] <size:
return [d[target-nums[size]],size]
else:
d[nums[size]] = size
size = size +1
solution = Solution()
list = [2,7,11,15]
target = 9
nums = solution.twoSum(list,target)
print(nums)

給列表中的字典排序:假設有如下list對象,alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}],將alist中的元素按照age從大到小排序 alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}]

alist_sort = sorted(alist,key=lambda e: e.__getitem__('age'),reverse=True)
2019下半年Python高頻面試題,第六彈

二. python代碼實現刪除一個list裡面的重複元素

def distFunc1(a):
"""使用集合去重"""
a = list(set(a))
print(a)
def distFunc2(a):
"""將一個列表的數據取出放到另一個列表中,中間作判斷"""
list = []
for i in a:
if i not in list:
list.append(i)
#如果需要排序的話用sort
list.sort()
print(list)
def distFunc3(a):
"""使用字典"""
b = {}
b = b.fromkeys(a)
c = list(b.keys())
print(c)
if __name__ == "__main__":
a = [1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
distFunc1(a)
distFunc2(a)
distFunc3(a)

三. 統計一個文本中單詞頻次最高的10個單詞?

import re
# 方法一
def test(filepath):

distone = {}
with open(filepath) as f:
for line in f:
line = re.sub("\\W+", " ", line)
lineone = line.split()
for keyone in lineone:
if not distone.get(keyone):
distone[keyone] = 1
else:
distone[keyone] += 1
num_ten = sorted(distone.items(), key=lambda x:x[1], reverse=True)[:10]
num_ten =[x[0] for x in num_ten]
return num_ten


# 方法二
# 使用 built-in 的 Counter 裡面的 most_common
import re
from collections import Counter
def test2(filepath):
with open(filepath) as f:
return list(map(lambda c: c[0], Counter(re.sub("\\W+", " ", f.read()).split()).most_common(10)))

四. 請寫出一個函數滿足以下條件

該函數的輸入是一個僅包含數字的list,輸出一個新的list,其中每一個元素要滿足以下條件:

1、該元素是偶數

2、該元素在原list中是在偶數的位置(index是偶數)

def num_list(num):
return [i for i in num if i %2 ==0 and num.index(i)%2==0]
num = [0,1,2,3,4,5,6,7,8,9,10]
result = num_list(num)
print(result)

五. 使用單一的列表生成式來產生一個新的列表

該列表只包含滿足以下條件的值,元素為原始列表中偶數切片

list_data = [1,2,5,8,10,3,18,6,20]
res = [x for x in list_data[::2] if x %2 ==0]
print(res)

六. 用一行代碼生成[1,4,9,16,25,36,49,64,81,100]

[x * x for x in range(1,11)]

七. 輸入某年某月某日,判斷這一天是這一年的第幾天?

import datetime
y = int(input("請輸入4位數字的年份:"))
m = int(input("請輸入月份:"))
d = int(input("請輸入是哪一天"))
targetDay = datetime.date(y,m,d)
dayCount = targetDay - datetime.date(targetDay.year -1,12,31)
print("%s是 %s年的第%s天。"%(targetDay,y,dayCount.days))

八. 兩個有序列表,l1,l2,對這兩個列表進行合併不可使用extend

def loop_merge_sort(l1,l2):
tmp = []
while len(l1)>0 and len(l2)>0:
if l1[0] <l2[0]:
tmp.append(l1[0])
del l1[0]
else:
tmp.append(l2[0])
del l2[0]
while len(l1)>0:
tmp.append(l1[0])
del l1[0]
while len(l2)>0:
tmp.append(l2[0])
del l2[0]
return tmp

九. 給定一個任意長度數組,實現一個函數

讓所有奇數都在偶數前面,而且奇數升序排列,偶數降序排序,如字符串’1982376455’,變成’1355798642’

# 方法一
def func1(l):
if isinstance(l, str):
l = [int(i) for i in l]
l.sort(reverse=True)
for i in range(len(l)):
if l[i] % 2 > 0:
l.insert(0, l.pop(i))
print(''.join(str(e) for e in l))
# 方法二
def func2(l):
print("".join(sorted(l, key=lambda x: int(x) % 2 == 0 and 20 - int(x) or int(x))))

十. 寫一個函數找出一個整數數組中,第二大的數

def find_second_large_num(num_list):
"""
找出數組第2大的數字
"""
# 方法一
# 直接排序,輸出倒數第二個數即可
tmp_list = sorted(num_list)
print("方法一\\nSecond_large_num is :", tmp_list[-2])

# 方法二
# 設置兩個標誌位一個存儲最大數一個存儲次大數
# two 存儲次大值,one 存儲最大值,遍歷一次數組即可,先判斷是否大於 one,若大於將 one 的值給 two,將 num_list[i] 的值給 one,否則比較是否大於two,若大於直接將 num_list[i] 的值給two,否則pass
one = num_list[0]
two = num_list[0]
for i in range(1, len(num_list)):
if num_list[i] > one:
two = one
one = num_list[i]
elif num_list[i] > two:
two = num_list[i]
print("方法二\\nSecond_large_num is :", two)

# 方法三
# 用 reduce 與邏輯符號 (and, or)
# 基本思路與方法二一樣,但是不需要用 if 進行判斷。
from functools import reduce
num = reduce(lambda ot, x: ot[1] < x and (ot[1], x) or ot[0] < x and (x, ot[1]) or ot, num_list, (0, 0))[0]
print("方法三\\nSecond_large_num is :", num)


if __name__ == '__main___':
num_list = [34, 11, 23, 56, 78, 0, 9, 12, 3, 7, 5]
find_second_large_num(num_list)
"

相關推薦

推薦中...