面试题汇总——编程篇
一、排序
1 ls=[3,2,5,1] 2 # 方法一: 3 def bubble(ls): 4 for i in range(0,len(ls)): 5 for j in range(i+1,len(ls)): 6 if ls[i]>ls[j]: 7 ls[i],ls[j]=ls[j],ls[i] 8 return ls 9 # print(bubble(ls)) 10 # 方法二: 11 print(sorted(ls,reverse=True))
二、求最大值、最小值
1 ls=[3,2,5,1,5] 2 # 方法一 3 # print(max(ls)) 4 # 方法二 5 max_val=0 6 for i in ls: 7 if i>max_val: 8 max_val=i 9 print(max_val)
三、去重
1 ls=[3,2,3,5,1,5,5] 2 # 方法一: 3 # print(set(ls)) 4 # 方法二: 5 ls2=[] 6 for i in ls: 7 if i not in ls2: 8 ls2.append(i) 9 print(ls2)
四、统计出现的次数
1 ls=[3,2,3,5,1,5,5] 2 # 方法一: 3 d={} 4 for i in ls: 5 d[i]=ls.count(i) 6 print(d)
方法二:
ls2=list(set(ls))
for i in ls2:
ls.count(i)
五、倒序
1 str="tom" 2 str2='' 3 c = list(str) 4 # 方法一 5 print(sorted(list(str),reverse=False)) 6 # 方法二 7 for i in range(len(c)): 8 str2+=c.pop() 9 # 方法三 10 leng=len(str) 11 leng2=len(str)-1 12 for i in range(leng): 13 str2+=c[leng2-i]
六、递归
1 p = r"D:\测试用例" 2 3 import os 4 def getFile(path): 5 p = os.listdir(path) 6 for i in p: 7 f = os.path.join(path,i) 8 if os.path.isdir(f): 9 getFile(f) 10 else: 11 print(f) 12 getFile(p)
七、装饰器
带参数的装饰器
1 def outer2(val): 2 def outer(fun): 3 def inner(*args,**kwargs): 4 if val==1: 5 print("decoration1..") 6 if val==2: 7 print("decoration2..") 8 fun(*args,**kwargs) 9 return inner 10 return outer 11 12 @outer2(2) 13 def one(): 14 print('ok...') 15 one()
八、字典排序
1 d = {"1":"tom","3":"Lily","2":"Jim"} 2 # 方法一: 3 c = sorted(d.items(),key=lambda x:x[0]) 4 # print(c) 5 # 方法二: 6 keys = [i for i in d.keys()] 7 skeys = sorted(keys) 8 vals=[] 9 for i in skeys: 10 for k,v in d.items(): 11 if i==k: 12 vals.append(v) 13 # cc = map(lambda x,y:{x:y},skeys,vals)
d={}
for i in range(len(ls)):
d[ls[i]]=ls2[i]
print(d)
九、最长的公共前缀
1 ls=["floght","floor","floy","flower"] 2 min_lenght = len(ls[0]) 3 for i in ls: 4 if min_lenght>len(i): 5 min_lenght=len(i) 6 ls2=[] 7 str2='' 8 for i in range(min_lenght): 9 for u in ls: 10 ls2.append(u[i]) 11 if ls2.count(u[i])==min_lenght: 12 str2+=u[i] 13 ls2=[] 14 print(str2)
十、递归求和
1 def get_sum(num): 2 if num<=1: 3 return 1 4 return num+get_sum(num-1) 5 print(get_sum(100))
十、递归树
1 menu_items = [ 2 {'id': 1, 'name': '一级菜单', 'status': 1, 'parent_id': 0}, 3 {'id': 2, 'name': '二级菜单', 'status': 1, 'parent_id': 1}, 4 {'id': 3, 'name': '三级菜单', 'status': 1, 'parent_id': 2}, 5 # 其他菜单项... 6 ] 7 8 def build_menu(menu_items, parent_id=0): 9 menu = [] 10 for item in menu_items: 11 if item['parent_id'] == parent_id: 12 child_menu = build_menu(menu_items, item['id']) 13 cc = child_menu 14 menu.append({ 15 'name': item['name'], 16 'child': child_menu 17 }) 18 return menu 19 print(build_menu(menu_items))
十一、找出连续的值
ls=[1,2,3,5,6,8] ls2=[] ls3=[] for i in ls: ls2.append(i) if i+1 not in ls: if ls2[0]==ls2[-1]: ls3.append(ls2[0]) else: ls3.append([ls2[0],ls2[-1]]) ls2=[] print(ls3)
十二、LRU 算法
https://www.jb51.net/article/249285.htm?tdsourcetag=s_pcqq_aiomsg
https://www.jianshu.com/p/e41ec08e4aa6
1 from collections import OrderedDict 2 class LRUCache: 3 def __init__(self,size): 4 self.size = size 5 self.dic = OrderedDict() 6 def set(self,key,val): 7 if key in self.dic: 8 self.dic.pop(key) 9 if self.size == len(self.dic): 10 self.dic.popitem(last=False) 11 self.dic.update({key:val}) 12 def get(self,key): 13 val = self.dic.get(key) 14 self.dic.pop(key) 15 self.dic.update({key: val}) 16 return val 17 18 r = LRUCache(3) 19 r.set("1", "1") 20 r.set("2", "2") 21 r.set("1", "3") 22 # r.set("1", "4") 23 # r.set("1", "5") 24 print(r.dic) 25 r.get("1") 26 print(r.dic) 27 # r.set("4", "4") 28 # print(r.dic)
from collections import OrderedDict
class LRUCache:
def __init__(self,size):
self.size = size
self.dic = OrderedDict()
def set(self,key,val):
if key in self.dic:
self.dic.pop(key)
if self.size == len(self.dic):
self.dic.popitem(last=False)
self.dic.update({key:val})
def get(self,key):
val = self.dic.get(key)
self.dic.pop(key)
self.dic.update({key: val})
return val
r = LRUCache(3)
r.set("1", "1")
r.set("2", "2")
r.set("1", "3")
# r.set("1", "4")
# r.set("1", "5")
print(r.dic)
r.get("1")
print(r.dic)
# r.set("4", "4")
# print(r.dic)