当前位置: 移动技术网 > IT编程>脚本编程>Python > 数百道BAT等大厂最新Python面试真题,学到你手软!

数百道BAT等大厂最新Python面试真题,学到你手软!

2019年12月19日  | 移动技术网IT编程  | 我要评论

和平分手简谱,mc527,霸少的专制宠爱

春招临近,无论是要找工作的准毕业生,还是身在职场想要提升自己的程序员,提升自己的算法内功心法、提升 python 编程能力,总是大有裨益的。今天,小编发现了一份好资源:python 实现的面试题集锦!

这份资源名为:interview-code-practice-python!包含了几百道算法面试题,而且全都使用 python 编写了答案。有问有答,学得岂不快哉~

好了,话不多说,平台不能放链接,可以直接去github上搜,或者找我要也可以!

这个项目资源总共包含了 5 个方面的真题,分别是:2017 校招真题、剑指 offer、华为机试、机试题、直通 bat 算法题。

接下来,我们分别来看一下具体内容。

1. 2017 校招真题

这部分包含了 37 道 2017 年的校招真题。

每个题目都配备相应的 python 实现。例如我们来看一个有趣的例子:餐厅.py

 1 # 在 m 批客人中选择 n 批上 n 个桌子
 2 n, m = [int(x) for x in input().split()]
 3 # 每个桌子可容纳的最大人数
 4 table = [int(x) for x in input().split()]
 5 # 分别表示第 i 批客人的人数和预计消费金额
 6 cus = [[int(x) for x in input().split()] for i in range(m)]
 7 
 8 # 将桌子按可容纳人数从小到大排列
 9 table.sort()
10 # 按每批顾客的人数从小到大排序
11 cus = sorted(cus, key=lambda x: x[0])
12 # 总金额
13 money = 0
14 
15 for i in range(n):
16 # 盛放第 i 个可接受的顾客批 j
17 temp = []
18 # 注意 range 中要用 cus 的 length 时更新
19 for j in range(len(cus)):
20 if cus[j][0] > table[i]:
21 break
22 temp.append(cus[j])
23 # 按消费金额排序
24 temp = sorted(temp, key=lambda x: x[1])
25 if temp:
26 money += temp[-1][1]
27 # 此批客人已就坐
28 cus.remove()
29 print(money)

 

2. 剑指 offer

这部分共包含了 68 道剑指真题。

请看示例:变态青蛙跳.py

 1 '''
 2 题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法
 3 '''
 4 
 5 '''
 6 因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
 7 跳1级,剩下n-1级,则剩下跳法是f(n-1)
 8 跳2级,剩下n-2级,则剩下跳法是f(n-2)
 9 所以f(n)=f(n-1)+f(n-2)+...+f(1)
10 因为f(n-1)=f(n-2)+f(n-3)+...+f(1)
11 所以f(n)=2*f(n-1)
12 然后求解这个无穷级数的和,正确答案应该是:每次至少跳一个,至多跳n个,一共有f(n)=2n-1种跳法
13 29ms
14 5632k
15 '''
16 
17 # -*- coding:utf-8 -*-
18 class solution:
19 def jumpfloorii(self, number):
20 # write code here
21 return 2**(number-1)

 

3. 华为机试

这部分包含 41 道华为机试题。

请看示例:密码验证合格程序.py

 1 '''
 2 1.长度超过8位
 3 2.包括大小写字母.数字.其它符号,以上四种至少三种
 4 3.不能有相同长度超2的子串重复
 5 说明:长度超过2的子串
 6 '''
 7 
 8 # import re, sys
 9 #
10 # for i in sys.stdin.readlines():
11 # print("ok" if len(i.strip()) > 8 and sum(
12 # [1 if re.search(r"[a-z]", i.strip()) else 0, 1 if re.search(r"[a-z]", i.strip()) else 0,
13 # 1 if re.search(r"[0-9]", i.strip()) else 0, 1 if re.search(r"[^0-9a-za-z]", i.strip()) else 0]) > 2 and sum(
14 #         map(lambda c: i.strip().count(i.strip()[c:c + 3]) > 1, range(1, len(i.strip()) - 3))) == 0 else "ng")
15 
16 # 略微思考会发现,只需要判断长度为3的子串是否出现即可。因为假设子串长度为4的出现,则一定包括了长度为3的子串。同时需要注意,
17 # 本题说的子串指包括了部分子串重叠的情况,
18 # 例如qw11111*ed这个是不能通过的。再就是需要注意,判断子串的时候只需要判断到len(str)-3就行了。
19 
20 import sys
21 
22 try:
23 # 大小写,字母,
24 def panchar(sss):
25 standard = [0] * 4
26 for i in sss:
27 # print(i)
28 # 0
29 # 2
30 # 1
31 # a
32 # b
33 # print(len(sss))
34 # 数字
35 if i.isdigit():
36 standard[0] = 1
37 # print(i.isdigit())
38 # 小写
39 if i.islower():
40 standard[1] = 1
41 # 大写
42 if i.isupper():
43 standard[2] = 1
44 # 全都是字母,数字,空格
45 if not i.isalpha() and not i.isdigit() and not i.isspace():
46 standard[3] = 1
47 if sum(standard) >= 3:
48 return false
49 return true
50 
51 # 不能有相同长度超 2 的字串重复
52 def zichuan(sss):
53 for i in range(len(sss) - 3):
54             zichuan_1 = sss[i: i + 3]
55 zichuan_2 = sss[i + 1::]
56 if zichuan_1 in zichuan_2:
57 return true
58 return false
59 
60 result = []
61 while true:
62 line = sys.stdin.readline().strip()
63 if line == '':
64 break
65 if len(line) <= 8:
66 result.append('ng')
67 # 大小写字母.数字.其它符号
68 elif panchar(line):
69 result.append('ng')
70 elif zichuan(line):
71 result.append('ng')
72 else:
73 result.append('ok')
74 for i in result:
75 print(i)
76 except:
77 pass
78 
79 
80 # # 循环输入,try catch
81 # while true:
82 #     try:
83 # x = input().split()
84 #
85 #
86 #     except:
87 # pass

 

4. 机试题

这部分包含 3 道机试题。

请看示例:排序.py

  1 # # 冒泡排序
  2 # # 时间复杂度 o(n**2) 空间复杂度 o(1)
  3 # x = [int(i) for i in input().split(',')]
  4 #
  5 # # print(x)
  6 #
  7 # def mpsort(x):
  8 # n = len(x)
  9 # # print(n)
 10 # for i in range(n - 1):
 11 # for j in range(0, n - i - 1):
 12 # # print(x[j])
 13 # if x[j] > x[j + 1]:
 14 # x[j], x[j + 1] = x[j + 1], x[j]
 15 # return x
 16 #
 17 # print(mpsort(x))
 18 
 19 # # 选择排序
 20 # # 时间复杂度 o(n**2) 空间复杂度 o(1)
 21 # x = [int(i) for i in input().split(',')]
 22 #
 23 # def xzsort(x):
 24 # n = len(x)
 25 # for i in range(n - 1):
 26 # min = i
 27 # for j in range(i + 1, n):
 28 # if x[j] < x[min]:
 29 # min = j
 30 # x[i], x[min] = x[min], x[i]
 31 # return x
 32 #
 33 # print(xzsort(x))
 34 
 35 # # 插入排序
 36 # # 时间复杂度 o(n**2) 空间复杂度 o(1)
 37 # x = [int(i) for i in input().split(',')]
 38 #
 39 # def crsort(x):
 40 # n = len(x)
 41 # for i in range(1, n):
 42 # j = i
 43 # while j > 0:
 44 # if x[j] < x[j - 1]:
 45 # x[j], x[j - 1] = x[j - 1], x[j]
 46 # j -= 1
 47 # else:
 48 # break
 49 # return x
 50 #
 51 # print(crsort(x))
 52 
 53 # # 希尔排序
 54 # # 时间复杂度 o(nlogn)-o(n**2) 空间复杂度 o(1)
 55 # x = [int(i) for i in input().split(',')]
 56 #
 57 # def shellsort(x):
 58 # n = len(x)
 59 # gap = n // 2
 60 #
 61 # while gap > 0:
 62 # for i in range(gap, n):
 63 # j = i
 64 # while j > 0:
 65 # if x[j] < x[j - gap]:
 66 # x[j], x[j - gap] = x[j - gap], x[j]
 67 # j -= gap
 68 # else:
 69 # break
 70 # gap //= 2
 71 # return x
 72 #
 73 # print(shellsort(x))
 74 
 75 # # 快速排序
 76 # # 时间复杂度 o(nlogn) 空间复杂度 o(logn)-o(n)
 77 # x = [int(i) for i in input().split(',')]
 78 #
 79 # def kpsort(x, first, last):
 80 # font = first
 81 # end = last
 82 # middle = x[first]
 83 #
 84 # if first >= last:
 85 # return
 86 #
 87 # while font < end:
 88 # while font < end and x[font] <= middle:
 89 # font += 1
 90 # x[end] = x[font]
 91 #
 92 # while font < end and x[end] > middle:
 93 # end -= 1
 94 # x[font] = x[end]
 95 #
 96 # x[font] = middle
 97 #
 98 # kpsort(x, first, font - 1)
 99 # kpsort(x, font + 1, last)
100 
101 # 归并排序
102 # 时间复杂度 o(nlogn) 空间复杂度 o(n)
103 x = [int(i) for i in input().split(',')]
104 
105 def gbsort(x):
106 length = len(x)
107     if length <= 1:
108 return x
109 mid = length // 2
110 
111     left = gbsort(x[:mid])
112     right = gbsort(x[mid:])
113 
114 left_point, right_pointer = 0, 0
115 result = []
116 
117     while left_point < len(left) and right_pointer < len(right):
118         if left[left_point] <= right[right_pointer]:
119 result.append(left[left_point])
120 left_point += 1
121         else:
122 result.append(right_pointer)
123 right_pointer += 1
124 
125     result += left[left_point:]
126 result += right[right_pointer]
127 
128 return result
129 
130 print(gbsort(x))

 

5. 直通 bat 算法题

这部分又包含三大块:

  • 二叉树

  • 栈和队列

  • 链表

在学习python的过程中,往往因为没有资料或者没人指导从而导致自己不想学下去了,因此我特意准备了个群 592539176 ,群里有大量的pdf书籍、教程都给大家免费使用!不管是学习到哪个阶段的小伙伴都可以获取到自己相对应的资料!

我们来看一个示例:向有环的环形链表中插入新节点.py

 1 # 指针给的是节点值
 2 class node():
 3 def __init__(self, value=none):
 4 self.value = value
 5 self.next = none
 6 
 7 def insertnum(head, num):
 8 node = node(num)
 9 if head == none:
10 node.next = node
11 return node
12 node = head
13 pre = node
14 cur = node.next
15 while cur != head:
16 if pre.value > num and cur.value <= num:
17 break
18 pre = pre.next
19 cur = cur.next
20 # num 小于节点值,pre只跑到最后一个节点,node跑道头结点
21 pre.next = node
22 node.next = cur
23 # 是按顺序来的,返回的是 head 或者 node ,是有顺序决定的
24 return head if head.value < num else node
25 
26 node = node()
27 node.insertnum([[1, 2, 3], 5])

 

最后,一般学习 python 的最好方法就是从底层算法开始实现一遍,过一遍基本函数与结构。有了充足的理解之后,就可以直接刷 leetcode 等。希望这份 python 面试题集锦对你有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网